lecture 6 - nus computing - homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. people want to get...

56
Lecture 6 Performance 17 February 2017

Upload: others

Post on 05-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Lecture 6 Performance

17 February 2017

Page 2: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Software Resources• CPU

• Memory

• Network

• Battery

• Disk

Page 3: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits
Page 6: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits
Page 7: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits
Page 8: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

“People will visit a Web site less often if it is slower than a close

competitor by more than 250 ms.”

Page 9: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits
Page 10: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

“Half a second delay caused a 20% drop in

traffic”

Page 11: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

“We tried delaying the page in increments of 100 milliseconds and found that even

very small delays would result in substantial and costly drops in revenue”

Page 12: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

“We tried delaying the page in increments of 100 milliseconds and found that even

very small delays would result in substantial and costly drops in revenue”

Page 13: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Page load time of <1s feels immediate

Response time of <200ms feels instantaneous

Page 14: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Reduce size of Web page• Optimize the images

• Load only necessary scripts

• Minimize the scripts / CSS / HTML

Easy. Not focus of this lecture.

Page 15: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Improve Performance• Faster DB accesses

• Better data structures and algorithms

• Fewer connections / downloads

• Etc.

Need Deep CS Knowledge

Page 16: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

“Premature optimization is the root of all evil”

- Don Knuth

Page 17: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

“Make it run Make it right Make it fast”

— Kent Beck

Page 18: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Tips

• If you can’t stand how slow your code is, optimize now (after making your code works correctly)

• Otherwise, wait until you have most of the features done

Page 19: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

while (performance is bad) find bottleneck remove bottlebeck

Page 20: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Use code profiler (there is one for every language I used)

Page 21: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Typical Profiler Output• function name

• absolute time spent in function

• % of time spent in function

• how many times the function is called

• parent functions, etc.

Page 22: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Example of Performance Issues

Page 23: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unnecessary Disk I/O

Page 25: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unnecessary Memory Copies

Page 26: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unnecessary Format Conversion

Page 27: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unexpected Interactions Between Components

Page 28: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unexpected Locking

Page 29: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unexpected SQL / database access

Page 30: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Slow SQL Queries (esp using ORM)

Page 31: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Unnecessary Network Communications

Page 32: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Bad data structures and algorithms

Page 33: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

def concat1(): out_str = '' for num in xrange(loop_count): out_str += ‘a ‘ return out_str

def concat2(): return ‘ ‘.join([‘a’ for num in xrange(loop_count)])

Page 34: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Memory leaks

Page 35: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Garbage Collection

Page 36: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Common Techniques for Improving Performance

Page 37: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Pre-computation

Page 38: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Caching

Page 39: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Pre-fetching

Page 40: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Pipelining Threading

Parallelizing

Page 41: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Your Task

• Find performance bottleneck (if any)

• Optimize it away

• Report what you did and the performance gain

• Example: https://nus-mtp.github.io/1516/images/polaris-proj-report-6.1.pdf

Page 42: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Sometime, you can make things feel faster

Page 43: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

The Psychology of Waiting Line

(David Maister)

Page 44: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

1. Occupied time feels shorter than unoccupied time

Page 46: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Example: Amazon, Facebook

Page 47: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

2. People want to get started

Page 48: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

Example: YouTube

Page 49: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

3. Uncertain waits are longer than known, finite waits

Page 51: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

4. Unexplained waits are longer than explained waits

Page 52: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits
Page 53: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

5. Unfair waits are longer than equitable waits

Page 54: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

6. The more valuable the service, the longer the customer will wait

Page 55: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

7. Solo wait feel longer than group waits

Page 56: Lecture 6 - NUS Computing - Homeooiwt/cs3283/lect6.pdf · 2017-02-17 · 2. People want to get started. Example: YouTube. 3. Uncertain waits are longer than known, finite waits

The case of fake progress bar..