$30 off During Our Annual Pro Sale. View Details »

Performance Tools on Android

Performance Tools on Android

The Android ecosystem has a lot of tools to help us out when it comes to development. We all use Android Studio, gradle and adb, for instance. We need to, as these are essential tools for building our products. But what about the tools that help us diagnose problems in our apps? This session is all about asking 2 very common questions:

Why is my app stuttering or dropping below 60fps?

Why are my users telling me that my app drains their batteries?

Most of the times solving a problem means asking the right questions in order to find the right answers. So starting from these 2 questions, with concrete problem-code examples we will be using tools from Google to find our answers.

I don't want it to be a secret, so I'm telling you that the tools we will be using the most will be Traceview and Battery Historian. We will also be looking at Profiling GPU Rendering and Debugging Overdraw.

I know that these tools might look a bit scary or dull but I promise to keep it simple so that you walk away with information you can use at work the next day.

Presented at:
- Droidcon Romania 2016
- Codecamp Cluj Spring 2016

Andrei Diaconu

November 26, 2016
Tweet

More Decks by Andrei Diaconu

Other Decks in Programming

Transcript

  1. Performance Tools
    Andrei Diaconu

    View Slide

  2. Andrei Diaconu

    View Slide

  3. Andrei Diaconu

    View Slide

  4. Andrei Diaconu

    View Slide

  5. Andrei Diaconu
    • Cofounder of Android Iași

    View Slide

  6. Andrei Diaconu
    • Cofounder of Android Iași
    • 5 years of Android

    View Slide

  7. Andrei Diaconu
    • Cofounder of Android Iași
    • 5 years of Android
    • Trainer & Speaker

    View Slide

  8. View Slide

  9. Why is my app dropping below 60fps?

    View Slide

  10. Why is my app dropping below 60fps?
    Why is my app draining the battery?

    View Slide

  11. Why is my app dropping below 60fps?

    View Slide

  12. Why is my app dropping below 60fps?

    View Slide

  13. Why is my app dropping below 60fps?
    • Simple list

    View Slide

  14. Why is my app dropping below 60fps?
    • Simple list
    • Items have some text

    View Slide

  15. Why is my app dropping below 60fps?
    • Simple list
    • Items have some text

    View Slide

  16. Why is my app dropping below 60fps?
    • Simple list
    • Items have some text
    • Scroll is not smooth

    View Slide

  17. • Scroll is not smooth

    View Slide

  18. • Scroll is not smooth
    Why?

    View Slide

  19. • Scroll is not smooth
    Why?
    Can we make sure?

    View Slide

  20. • Scroll is not smooth
    Why?
    Can we make sure?

    View Slide

  21. Can we make sure?

    View Slide

  22. Can we make sure?

    View Slide

  23. Can we make sure?

    View Slide

  24. Can we make sure?

    View Slide

  25. View Slide

  26. Frames

    View Slide

  27. Frames
    Time

    View Slide

  28. Frames
    16ms line
    Time

    View Slide

  29. Frames
    16ms line
    Time
    Choreographer: Skipped 34 frames! The application
    may be doing too much work on its main thread.

    View Slide

  30. View Slide

  31. Traceview

    View Slide

  32. Traceview
    • Measure how long each method takes to execute

    View Slide

  33. Traceview
    • Measure how long each method takes to execute
    • Analyse measurements using Traceview

    View Slide

  34. Traceview
    • Measure how long each method takes to execute

    View Slide

  35. Traceview
    //Start in OnResume

    Debug.startMethodTracing("bad_list");
    • Measure how long each method takes to execute

    View Slide

  36. Traceview
    //Start in OnResume

    Debug.startMethodTracing("bad_list");
    //Stop in OnPause

    Debug.stopMethodTracing();
    • Measure how long each method takes to execute

    View Slide

  37. Traceview
    //Start in OnResume

    Debug.startMethodTracing("bad_list");
    //Stop in OnPause

    Debug.stopMethodTracing();
    //Grab using adb

    adb pull
    • Measure how long each method takes to execute

    View Slide

  38. Traceview
    //Start in OnResume

    Debug.startMethodTracing("bad_list");
    //Stop in OnPause

    Debug.stopMethodTracing();
    //Grab using adb

    adb pull
    • Measure how long each method takes to execute
    //Grab using adb

    adb pull /sdcard/bad_list.trace bad_list.trace

    View Slide

  39. Traceview
    //Start in OnResume

    Debug.startMethodTracing("bad_list");
    //Stop in OnPause

    Debug.stopMethodTracing();
    //Grab using adb

    adb pull
    • Measure how long each method takes to execute
    //Needs permission
    WRITE_EXTERNAL_STORAGE
    //Grab using adb

    adb pull /sdcard/bad_list.trace bad_list.trace

    View Slide

  40. Traceview
    //Start in OnResume

    Debug.startMethodTracing("bad_list");
    //Stop in OnPause

    Debug.stopMethodTracing();
    //Grab using adb

    adb pull
    • Measure how long each method takes to execute
    //Needs permission
    WRITE_EXTERNAL_STORAGE
    //Grab using adb

    adb pull /sdcard/bad_list.trace bad_list.trace

    View Slide

  41. Traceview
    • Measure how long each method takes to execute

    View Slide

  42. Traceview
    • Measure how long each method takes to execute

    View Slide

  43. Traceview
    • Measure how long each method takes to execute
    • Analyse measurements using Traceview
    • Analyse measurements using Traceview

    View Slide

  44. Traceview
    • Measure how long each method takes to execute
    • Analyse measurements using Traceview
    • Analyse measurements using Traceview

    View Slide

  45. Traceview
    • Measure how long each method takes to execute
    • Analyse measurements using Traceview
    • Analyse measurements using Traceview

    View Slide

  46. Traceview
    • Measure how long each method takes to execute
    • Analyse measurements using Traceview
    • Analyse measurements using Traceview

    View Slide

  47. View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. View Slide

  52. View Slide

  53. ~ 2 seconds

    View Slide

  54. ~ 200 ms

    View Slide

  55. Adaptor -> getView()
    ~ 200 ms

    View Slide

  56. View Slide

  57. inflate()

    View Slide

  58. inflate() 4 x Html.fromHtml()

    View Slide

  59. @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.item_jumpy_list, parent, false);


    TextView text1 = (TextView) view.findViewById(R.id.item_text1);

    TextView text2 = (TextView) view.findViewById(R.id.item_text2);

    TextView text3 = (TextView) view.findViewById(R.id.item_text3);

    TextView text4 = (TextView) view.findViewById(R.id.item_text4);


    String string1 = parent.getContext().getString(R.string.item_number_x, position);

    String string2 = parent.getContext().getString(R.string.item_description);

    String string3 = parent.getContext().getString(R.string.item_details);

    String string4 = parent.getContext().getString(R.string.item_related);


    text1.setText(Html.fromHtml(string1));

    text2.setText(Html.fromHtml(string2));

    text3.setText(Html.fromHtml(string3));

    text4.setText(Html.fromHtml(string4));

    return view;

    }

    View Slide

  60. @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.item_jumpy_list, parent, false);


    TextView text1 = (TextView) view.findViewById(R.id.item_text1);

    TextView text2 = (TextView) view.findViewById(R.id.item_text2);

    TextView text3 = (TextView) view.findViewById(R.id.item_text3);

    TextView text4 = (TextView) view.findViewById(R.id.item_text4);


    String string1 = parent.getContext().getString(R.string.item_number_x, position);

    String string2 = parent.getContext().getString(R.string.item_description);

    String string3 = parent.getContext().getString(R.string.item_details);

    String string4 = parent.getContext().getString(R.string.item_related);


    text1.setText(Html.fromHtml(string1));

    text2.setText(Html.fromHtml(string2));

    text3.setText(Html.fromHtml(string3));

    text4.setText(Html.fromHtml(string4));

    return view;

    }

    View Slide

  61. @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.item_jumpy_list, parent, false);


    TextView text1 = (TextView) view.findViewById(R.id.item_text1);

    TextView text2 = (TextView) view.findViewById(R.id.item_text2);

    TextView text3 = (TextView) view.findViewById(R.id.item_text3);

    TextView text4 = (TextView) view.findViewById(R.id.item_text4);


    String string1 = parent.getContext().getString(R.string.item_number_x, position);

    String string2 = parent.getContext().getString(R.string.item_description);

    String string3 = parent.getContext().getString(R.string.item_details);

    String string4 = parent.getContext().getString(R.string.item_related);


    text1.setText(Html.fromHtml(string1));

    text2.setText(Html.fromHtml(string2));

    text3.setText(Html.fromHtml(string3));

    text4.setText(Html.fromHtml(string4));

    return view;

    }

    View Slide

  62. @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.item_jumpy_list, parent, false);


    TextView text1 = (TextView) view.findViewById(R.id.item_text1);

    TextView text2 = (TextView) view.findViewById(R.id.item_text2);

    TextView text3 = (TextView) view.findViewById(R.id.item_text3);

    TextView text4 = (TextView) view.findViewById(R.id.item_text4);


    String string1 = parent.getContext().getString(R.string.item_number_x, position);

    String string2 = parent.getContext().getString(R.string.item_description);

    String string3 = parent.getContext().getString(R.string.item_details);

    String string4 = parent.getContext().getString(R.string.item_related);


    text1.setText(Html.fromHtml(string1));

    text2.setText(Html.fromHtml(string2));

    text3.setText(Html.fromHtml(string3));

    text4.setText(Html.fromHtml(string4));

    return view;

    }

    View Slide

  63. @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    No recycling


    No View Holder



    Html.fromHtml is slow

    return view;

    }

    View Slide

  64. View Slide

  65. But 200ms?
    Isn't this a bit much?

    View Slide

  66. But 200ms?
    Isn't this a bit much?
    Method tracking has a big impact itself

    View Slide

  67. View Slide

  68. Systrace
    measure at kernel level

    View Slide

  69. Systrace

    View Slide

  70. Systrace

    View Slide

  71. Systrace

    View Slide

  72. Systrace

    View Slide

  73. Systrace
    It will run for 5 seconds
    Hit Ok and scroll the list

    View Slide

  74. View Slide

  75. Alert: Inflation during ListView recycling
    Time spent: 103.545 ms
    ListView items inflated: 20

    View Slide

  76. View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. @Override

    public View getView(int position, View convertView, ViewGroup parent) {


    View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.item_jumpy_list, parent, false);




    TextView text1 = (TextView) view.findViewById(R.id.item_text1);

    TextView text2 = (TextView) view.findViewById(R.id.item_text2);

    TextView text3 = (TextView) view.findViewById(R.id.item_text3);

    TextView text4 = (TextView) view.findViewById(R.id.item_text4);



    String string1 = parent.getContext().getString(R.string.item_number_x, position);

    String string2 = parent.getContext().getString(R.string.item_description);

    String string3 = parent.getContext().getString(R.string.item_details);

    String string4 = parent.getContext().getString(R.string.item_related);



    text1.setText(Html.fromHtml(string1));

    text2.setText(Html.fromHtml(string2));

    text3.setText(Html.fromHtml(string3));

    text4.setText(Html.fromHtml(string4));



    return view;

    }

    View Slide

  82. @Override

    public View getView(int position, View convertView, ViewGroup parent) {

    Trace.beginSection("inflating");

    View view = LayoutInflater.from(parent.getContext())
    .inflate(R.layout.item_jumpy_list, parent, false);

    Trace.endSection();


    Trace.beginSection("noViewHolder");

    TextView text1 = (TextView) view.findViewById(R.id.item_text1);

    TextView text2 = (TextView) view.findViewById(R.id.item_text2);

    TextView text3 = (TextView) view.findViewById(R.id.item_text3);

    TextView text4 = (TextView) view.findViewById(R.id.item_text4);

    Trace.endSection();


    String string1 = parent.getContext().getString(R.string.item_number_x, position);

    String string2 = parent.getContext().getString(R.string.item_description);

    String string3 = parent.getContext().getString(R.string.item_details);

    String string4 = parent.getContext().getString(R.string.item_related);


    Trace.beginSection("fromHtml");

    text1.setText(Html.fromHtml(string1));

    text2.setText(Html.fromHtml(string2));

    text3.setText(Html.fromHtml(string3));

    text4.setText(Html.fromHtml(string4));

    Trace.endSection();


    return view;

    }

    View Slide

  83. View Slide

  84. View Slide

  85. inflating

    View Slide

  86. inflating noViewHolder

    View Slide

  87. inflating noViewHolder
    fromHtml

    View Slide

  88. View Slide

  89. • obtainView = ~5ms

    View Slide

  90. • obtainView = ~5ms
    • doFrame = ~20 * obtainView

    View Slide

  91. • obtainView = ~5ms
    • doFrame = ~20 * obtainView
    • each frame = ~100ms

    View Slide

  92. • obtainView = ~5ms
    • doFrame = ~20 * obtainView
    • each frame = ~100ms
    Alert: Inflation during ListView recycling
    Time spent: 103.545 ms
    ListView items inflated: 20

    View Slide

  93. View Slide

  94. Why is my app dropping below 60fps?

    View Slide

  95. Why is my app dropping below 60fps?
    Why is my app draining the battery?

    View Slide

  96. Why is my app draining the battery?

    View Slide

  97. Why is my app draining the battery?
    What is a wake lock?

    View Slide

  98. Why is my app draining the battery?

    View Slide

  99. Why is my app draining the battery?
    Battery Historian 2.0

    View Slide

  100. Why is my app draining the battery?
    Battery Historian 2.0

    View Slide

  101. Why is my app draining the battery?
    Battery Historian 2.0
    adb bugreport > bugreport.txt

    View Slide

  102. Why is my app draining the battery?
    Battery Historian 2.0
    adb bugreport > bugreport.txt
    go run battery-historian.go

    View Slide

  103. Why is my app draining the battery?
    Battery Historian 2.0
    adb bugreport > bugreport.txt
    go run battery-historian.go
    open http://127.0.0.1:9999

    View Slide

  104. View Slide

  105. View Slide

  106. View Slide

  107. View Slide

  108. View Slide

  109. View Slide

  110. View Slide

  111. View Slide

  112. View Slide

  113. View Slide

  114. View Slide

  115. View Slide

  116. View Slide

  117. View Slide

  118. View Slide

  119. View Slide

  120. public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }
    public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }

    View Slide

  121. public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }
    public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }

    View Slide

  122. public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }
    public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }

    View Slide

  123. public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }
    public static class UselessWorker {

    Runnable uselessWorker = new Runnable() {

    @Override

    public void run() {

    PowerManager pm = (PowerManager) BadApplication.instance

    .getSystemService(Context.POWER_SERVICE);

    PowerManager.WakeLock wakeLock = 

    pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "cool wakelock tag");

    wakeLock.acquire();


    //Some useless, but hard work


    wakeLock.release();

    try {

    Thread.sleep(45 * 1000);

    } catch (InterruptedException e) {

    e.printStackTrace();

    } finally {

    work();

    }

    }

    };


    private void work() {

    new Thread(uselessWorker, "cool thread name").start();

    }

    }

    View Slide

  124. ?
    ?
    Questions

    View Slide