vietnam mobile day 2013: for higher performance mobile applications

28
For Higher Performance Mobile Applications Tran Vu Tat Binh

Upload: gamelandvn

Post on 20-Jun-2015

1.086 views

Category:

Technology


4 download

DESCRIPTION

Bài chia sẻ của anh Trần Vũ Tất Bình tại hội thảo Vietnam Mobile Day 2013 tổ chức tại TP.Hồ Chí Minh vào ngày 18/05/2013.

TRANSCRIPT

Page 1: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

For Higher Performance Mobile Applications

Tran Vu Tat Binh

Page 2: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

I’ve done all kind of micro-optimizations, use good algorithms, choose the right data structures but still got serious performance issues in my mobile apps.

Our online game took about 10 minutes to download 25 MB resources using 3G in the old version

Page 3: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Performance issues?

The overall application runs slow. The application makes other

applications on phone run slower (yes, it can)

Users feel the application slow. Be strict: it’s also performance issue if

we can do much better but we don’t

Page 4: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Some interesting points

Number of threads Organizing threads Multi-threading and network Mobile network under the hood

Page 5: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Number of threads

Using too many threads

After launch Rush hour

Page 6: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Number of threads

Using too many threads:OverheadHard to controlLess time for each threadConcurrency

Page 7: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Number of threads

Using too few threads:Not take advantage of system resourcesNot so responsive

Page 8: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Number of threads

So how many?

Flexible and should not be a fixed number, see the next “Organizing threads” for more details.

Page 9: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Usually, there are 3 kinds of taskBlocking UI taskUpdater taskLazy media task

Page 10: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads Blocking UI task

○ When there are no other data on screen

○ When user definitely have to wait for updated data only

Should be a single thread with high priority since user can not proceed if task is not done.

Page 11: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads Updater task

○ Updating / refreshing○ Loading more○ User can still interact with

existed data on screen○ Can move to other screen

without finishing task

Should be a thread pool (suggest size 2-4)

Page 12: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads Lazy media task

○ Thumbnails for existed data○ A list/grid of images

Should be 2 thread pools: one for downloading and one for decoding

Page 13: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Lazy media task (con.)Usually most heavy task focus optimizingWhy separating into downloading and

decoding○ Images might be in local cache already and

decoding usually much faster than downloading, so don’t let downloading slow down the progress

○ Decoding pool size should be based on CPU cores

○ Downloading pool size should be based on network status

Page 14: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Decoding pool size

NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();

mDecodeThreadPool = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, mDecodeWorkQueue);

Page 15: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Downloading pool sizeNormal:

○ Fixed size (may be 8 like in some tutorials)More optimized:

○ 2G – size 4○ 3G – size 8○ 4G – size 16 (or more)○ WIFI – size 8

Much more optimized:○ Based on network speed, increasing size if

can still download faster, decreasing size if still not slower, until get the best size.

Page 16: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Why variable size is better choice: 2G pool size 2

2G pool size 4

2G pool size 8

Page 17: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Why variable size is better choice:3G pool size 4 3G pool size 8

3G pool size 24

Page 18: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Facts: Each connection has a limited bandwidthThe phone’s network connection has a

overall limited bandwidthThere are network latency causing “gaps”

while downloading

Page 19: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Downloading pool sizeToo small size

○ Waste network bandwidth○ Drain battery life if using mobile network○ Slow

Too large size ○ Take more time to show individual image (not overall

images on screen) users feel slow○ TCP congestion control○ Slower to start downloading new images when

scrollingVariable size based on network status is good

choice but need more time to implement

Page 20: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Organizing threads

Conclusion:Blocking UI task – 1 threadUpdater task – 1 thread pool size 2 - 4Lazy media task – 2 thread pools

○ 1 thread pool for decoding size = available cores○ 1 thread pool for downloading size based on

network status

Page 21: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Mobile network under the hood

Online applications like messenger, news reader, social app or online game… some got serious issues when using mobile network

○ Battery drain○ Slow response

Page 22: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Mobile network

Typical 3G wireless radio state machine (source)

Page 23: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Mobile network

Messenger application○ Small data message○ Transferring more

frequently○ No fixed schedule

Page 24: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Mobile network

Online game○ Small data message○ Transferring much more

frequently○ Usually fixed schedule○ Responsiveness is

importance

Page 25: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Mobile network

Social app News reader Photo sharing Music app

Page 26: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Mobile network

Optimize data to transfer (as always) Batch operation Prefetch When receive, if need to transmit then

do quick If possible, do at the same time with

other apps

Page 27: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Q&A

Page 28: Vietnam Mobile Day 2013: For Higher Performance Mobile Applications

Thank you!