Share
Embed
Coldstart - why should you care and how? (DroidCon 2016, NY)
How can you quantify, measuring and analyzing startup time on a continuous basis? In this talk, we start up with a concrete definition of coldstart and describe techniques to measure it both as a part of continuous integration and from the field.
We then look at how to visualize and analyze this data using standard Android performance tools and visualization tools we built. And to end it off, we talk about some strategies we've used to optimize the startup process.
Vikram Bodicherla
September 29, 2016
Transcript
177 slides
-
Users want fast • 20% ➔ apps should load instantaneously
• 36% ➔ slow performance = company sucks AppDynamics -
Users want fast • 20% ➔ apps should load instantaneously
• 36% ➔ slow performance = company sucks • 86% ➔ uninstall at least 1 sluggish app after one use AppDynamics -
What’s fast enough? • > 3 seconds ➔ 53% drop-off
(Google) • < 2 seconds ➔ top 40% of the apps (NimbleDroid) -
MIP first • First most important pixel • Your MIP
definition is built into your UI design -
Our goal Median coldstart of 2000ms in production on your
most popular device on WiFi -
Our goal Median coldstart of 2000ms in production on your
most popular device on WiFi -
Our goal Median coldstart of 2000ms in production on your
most popular device on WiFi Median -
Our goal Median coldstart of 2000ms in production on your
most popular device on WiFi -
Our goal Median coldstart of 2000ms in production on your
most popular device on WiFi -
Our goal Median coldstart of 2000ms in production on your
most popular device on WiFi -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
What’s coldstart? • User taps on the application to •
no content • cached data* • fresh data -
What’s coldstart? • Apps can startup in multiple ways •
cold starts, warm starts, notifications, deep links etc -
What’s coldstart? • Apps can startup in multiple ways •
cold starts, warm starts, notifications, deep links etc • Keep special cases in mind • first launch, on-boarding flows, network failures -
Time First line of code Cached data Fresh content Icon
tap Layout loaded -
Time First line of code Cached data Fresh content Icon
tap Layout loaded -
Time First line of code Cached data Fresh content Icon
tap Layout loaded -
appInteractive = layoutLoaded - firstLineOfCode; Time First line of
code Cached data Fresh content Icon tap Layout loaded -
Time First line of code Cached data Fresh content Icon
tap Layout loaded -
cachedDataLoaded = cachedDataLoaded - firstLineOfCode; Time First line of
code Cached data Fresh content Icon tap Layout loaded -
Time First line of code Cached data Fresh content Icon
tap Layout loaded -
coldstart = freshContent - firstLineOfCode; Time First line of code
Cached data Fresh content Icon tap Layout loaded -
Phases • Section of code • Network calls • Initializing
SDKs • Data processing -
public class Stopwatch { public void markEvent(String event) { //Record
event time SystemClock.elapsedRealtime(); } } -
public class Stopwatch { public void markEvent(String event) { //Record
event time SystemClock.elapsedRealtime(); } } SystemClock.currentTimeMillis(); SystemClock.uptimeMillis(); SystemClock.elapsedRealtimeNanos(); -
public class Stopwatch { public void markEvent(String event) { //Record
event time SystemClock.elapsedRealtime(); } } SystemClock.currentTimeMillis(); SystemClock.uptimeMillis(); SystemClock.elapsedRealtimeNanos(); -
public class Stopwatch { public void markEvent(String event) { //Record
event time SystemClock.elapsedRealtime(); } } SystemClock.currentTimeMillis(); SystemClock.uptimeMillis(); SystemClock.elapsedRealtimeNanos(); -
public class Stopwatch { public void markEvent(String event) { //Record
event time SystemClock.elapsedRealtime(); } } SystemClock.currentTimeMillis(); SystemClock.uptimeMillis(); SystemClock.elapsedRealtimeNanos(); -
public class ColdstartRegister { public void updateState(Event event) { stopwatch.markEvent(event.name());
if(stopwatch.isMarked(Event.TICKERS_LOADED) && stopwatch.isMarked(Event.QUOTES_LOADED)){ //Report cachedData } } } -
public class ColdstartRegister { public void updateState(Event event) { stopwatch.markEvent(event.name());
if(stopwatch.isMarked(Event.TICKERS_LOADED) && stopwatch.isMarked(Event.QUOTES_LOADED)){ //Report cachedData } } } -
public class ColdstartRegister { public void updateState(Event event) { stopwatch.markEvent(event.name());
if(stopwatch.isMarked(Event.TICKERS_LOADED) && stopwatch.isMarked(Event.QUOTES_LOADED)){ //Report cachedData } } } -
public class MyApp extends Application { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); } } Application Activity TrendingTickers Quotes -
Application Activity TrendingTickers Quotes public class MyApp extends Application {
private ColdstartRegister register; @Override public void onCreate() { super.onCreate(); reporter.updateState(Event.APP_ON_CREATE_BEGIN); } } -
public class MyApp extends Application { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); reporter.updateState(Event.APP_ON_CREATE_BEGIN); //Do your work } } Application Activity TrendingTickers Quotes -
public class MyApp extends Application { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); reporter.updateState(Event.APP_ON_CREATE_BEGIN); //Do your work reporter.updateState(Event.APP_ON_CREATE_END); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); reporter.updateState(Event.ACT_ON_CREATE_BEGIN); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); reporter.updateState(Event.ACT_ON_CREATE_BEGIN); fetchTrendingTickers(…, new Listener() { @Override public void onResponse(final Response response) { showTrendingTickers(response.getTickers()); } }); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); reporter.updateState(Event.ACT_ON_CREATE_BEGIN); fetchTrendingTickers(…, new Listener() { @Override public void onResponse(final Response response) { showTrendingTickers(response.getTickers()); reporter.updateState(Event.TICKERS_LOADED); } }); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { … } @Override public void onResume() { super.onResume(); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { … } @Override public void onResume() { super.onResume(); reporter.updateState(Event.APP_INTERACTIVE); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
public void loadQuotes(List<String> symbols) { fetchQuotes(…, new Listener() { @Override public void onResponse(final Response response) { updateQuotes(response.getQuotes()); reporter.updateState(Event.QUOTES_LOADED); } }); } } Application Activity TrendingTickers Quotes -
Capturing phase information public class Stopwatch { public void startPhase(String
phase) { } public void endPhase(String phase) { } public void markEvent(String phase, String event) { } } -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); … fetchTrendingTickers(…, new Listener() { @Override public void onResponse(final Response response) { showTrendingTickers(response.getTickers()); … } }); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); … reporter.startPhase(“fetchTrendingTickers”); fetchTrendingTickers(…, new Listener() { @Override public void onResponse(final Response response) { showTrendingTickers(response.getTickers()); … reporter.endPhase(“fetchTrendingTickers”); } }); } } Application Activity TrendingTickers Quotes -
public class MyActivity extends Activity { private ColdstartRegister register;
@Override public void onCreate() { super.onCreate(); … reporter.startPhase(“fetchTrendingTickers”); fetchTrendingTickers(…, new Listener() { @Override public void onResponse(final Response response) { showTrendingTickers(response.getTickers()); … reporter.endPhase(“fetchTrendingTickers”); } }); } } Application Activity TrendingTickers Quotes -
Extracting data from the Stopwatch public class Stopwatch { …
} public interface Reporter { public void reportAll(…) { } } -
Extracting data from the Stopwatch public class Stopwatch { …
public void reportAll(Reporter dumper) { //Dump all events } } public interface Reporter { … } -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
… POST /performance Telemetry { "coldstart": 600, "model": “SM-G900P", "version":
“0.1", "phases": [ { "name": "phase1", "start": 3, "dur": 45 } ... { "name": "phase2", "start": 78, "dur": 331 } ] } -
… Telemetry POST /performance { "coldstart": 720, "model": “LG-H811", "version":
“0.1", "phases": [ { "name": "phase1", "start": 8, "dur": 33 } ... { "name": "phase2", "start": 64, "dur": 298 } ] } -
… { "coldstart": 600, "model": “LG-E977", "version": “0.1", "phases": [
{ "name": "phase1", "start": 11, "dur": 84 } ... { "name": "phase2", "start": 66, "dur": 293 } ] } Telemetry POST /performance -
{ "agg_phases": [ { "name": "phase1", "agg_start": 3, "agg_dur": 45
} ... { "name": "phase2", "agg_start": 78, "agg_dur": 331 } ], "version": “0.1” } Telemetry GET /aggregate_phases?v=0.1 -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> https://developers.google.com/chart/interactive/docs/gallery/timeline -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> var chart = new google.visualization.Timeline(container); https://developers.google.com/chart/interactive/docs/gallery/timeline -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); https://developers.google.com/chart/interactive/docs/gallery/timeline -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); https://developers.google.com/chart/interactive/docs/gallery/timeline -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); dataTable.addRows([ [ 'Phase1', start, end], [ 'Phase2', start, end]]); https://developers.google.com/chart/interactive/docs/gallery/timeline -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); dataTable.addRows([ [ 'Phase1', start, end], [ 'Phase2', start, end]]); chart.draw(dataTable); https://developers.google.com/chart/interactive/docs/gallery/timeline -
Build your own phase visualizer <html> <body> <div id="timeline" style="height:
180px;”/> </body> </html> var chart = new google.visualization.Timeline(container); var dataTable = new google.visualization.DataTable(); dataTable.addRows([ [ 'Phase1', start, end], [ 'Phase2', start, end]]); chart.draw(dataTable); https://developers.google.com/chart/interactive/docs/gallery/timeline -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
Alerting on issues Telemetry Yes No GET /aggregate_coldstart > 2000ms?
Slack Email Text Pigeon -
Responding to alerts Telemetry Remote fix? Fix it! Yes GET
/aggregate_coldstart -
Responding to alerts Telemetry Remote fix? Fix it! Yes No
GET /aggregate_coldstart -
Responding to alerts Telemetry Remote fix? Fix it! Yes Cancel
that vacation No GET /aggregate_coldstart -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
Fail early • Catch regressions as you write code on
your CI • Regression = considerable uptick in coldstart -
Fail early • Catch regressions as you write code on
your CI • Regression = considerable uptick in coldstart • Remove variances - server latency -
Fail early • Catch regressions as you write code on
your CI • Regression = considerable uptick in coldstart • Remove variances - server latency ./gradlew checkForColdstartRegression -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? yes Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job yes Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job yes no Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history Write commit:coldstart coldstart - previousColdstart > threshold? Fail job Pass job yes no Get coldstart:commit -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no -
Fail early ./gradlew checkForColdstartRegression App code App HTTP server Stetho
coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no Write commit:coldstart Get coldstart:commit -
A few moving parts here ./gradlew checkForColdstartRegression App code App
HTTP server Stetho coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no -
A few moving parts here ./gradlew checkForColdstartRegression App code App
HTTP server Stetho coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no Write commit:coldstart Get coldstart:commit -
A few moving parts here ./gradlew checkForColdstartRegression App code App
HTTP server Stetho coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no -
A few moving parts here ./gradlew checkForColdstartRegression App code App
HTTP server Stetho coldstart Coldstart history coldstart - previousColdstart > threshold? Fail job Pass job yes no Write commit:coldstart Get coldstart:commit -
Local HTTP server android { buildTypes { production { buildConfigField
“String", ‘“URL_HOST”’,“myhost.com” } coldstartTest { buildConfigField “String", ‘“URL_HOST”’,“127.0.0.1” } } } dependencies { coldstartTestCompile ‘com.nanohttpd:nanohttpd-webserver:2.+’ } -
Local HTTP server android { buildTypes { production { buildConfigField
“String", ‘“URL_HOST”’,“myhost.com” } coldstartTest { buildConfigField “String", ‘“URL_HOST”’,“127.0.0.1” } } } dependencies { coldstartTestCompile ‘com.nanohttpd:nanohttpd-webserver:2.+’ } -
Local HTTP server android { buildTypes { production { buildConfigField
“String", ‘“URL_HOST”’,“myhost.com” } coldstartTest { buildConfigField “String", ‘“URL_HOST”’,“127.0.0.1” } } } dependencies { coldstartTestCompile ‘com.nanohttpd:nanohttpd-webserver:2.+’ } -
Local HTTP server android { buildTypes { production { buildConfigField
“String", ‘“URL_HOST”’,“myhost.com” } coldstartTest { buildConfigField “String", ‘“URL_HOST”’,“127.0.0.1” } } } dependencies { coldstartTestCompile ‘com.nanohttpd:nanohttpd-webserver:2.+’ } -
Local HTTP server public class ColdStartApp extends MyApp {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } } -
Local HTTP server public class ColdStartApp extends MyApp {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new MyHTTPD().start(); } private class MyHTTPD extends NanoHTTPD { public MyHTTPD() throws IOException { super(PORT, null); } @Override public Response serve(String uri, …) { serveFileBasedOnUri(uri); } } } -
Local HTTP server public class ColdStartApp extends MyApp {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new MyHTTPD().start(); } private class MyHTTPD extends NanoHTTPD { public MyHTTPD() throws IOException { super(PORT, null); } @Override public Response serve(String uri, …) { serveFileBasedOnUri(uri); } } } -
Local HTTP server public class ColdStartApp extends MyApp {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); new MyHTTPD().start(); } private class MyHTTPD extends NanoHTTPD { public MyHTTPD() throws IOException { super(PORT, null); } @Override public Response serve(String uri, …) { serveFileBasedOnUri(uri); } } } -
The plan • Define • Measure • A DIY performance-degradation
alerting system • Fail early • Performance catalog -
Your APIs • Make your APIs fast • Get a
first-page API built • Enable data compression on the server-side • Take a look at your caching policies -
Some of the more obvious ones • Avoid reflection at
all costs • Batch network calls • Know what your SDKs do -
Some more of the more obvious ones • Avoid resource
loading • Ensure that your UI thread and workers are working in sync • Avoid image manipulation and processing -
Some micro-optimizations • Flatten your UI • Be careful with
handler.postAtFrontOfQueue(new Runnable()) • Try and stay under the 65K method limit