automated historical performance analysis with kmemtracer

184
Automated Historical Performance Analysis with kmemtracer LG전자 이경민

Upload: kyungmin-lee

Post on 11-Jun-2015

574 views

Category:

Technology


6 download

DESCRIPTION

Material presented at the 12th Korea Android Conference, Oct. 2013.

TRANSCRIPT

Page 1: Automated Historical Performance Analysis with kmemtracer

Automated Historical

Performance Analysis

with kmemtracer

LG전자 이경민

Page 2: Automated Historical Performance Analysis with kmemtracer

2 12th Kandroid Conference - www.kandroid.org

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 3: Automated Historical Performance Analysis with kmemtracer

3 12th Kandroid Conference - www.kandroid.org

If you can’t measure it, you can’t manage it.

Peter F. Drucker (1909~2005)

Page 4: Automated Historical Performance Analysis with kmemtracer

4 12th Kandroid Conference - www.kandroid.org

If you can’t measure it, you can’t manage it.

Peter F. Drucker (1909~2005)

If you can’t automate the measurement of it,

it is difficult to improve it.

Page 5: Automated Historical Performance Analysis with kmemtracer

5 12th Kandroid Conference - www.kandroid.org

http://developer.android.com/about/dashboards/index.html

Platform Versions

Diversity or Fragmentation

Page 6: Automated Historical Performance Analysis with kmemtracer

6 12th Kandroid Conference - www.kandroid.org

http://developer.android.com/about/dashboards/index.html

Platform Versions

Screen Sizes and Densities

Diversity or Fragmentation

Page 7: Automated Historical Performance Analysis with kmemtracer

7 12th Kandroid Conference - www.kandroid.org

http://www.mactrast.com/2012/05/visualized-the-truth-about-android-fragmentation/

Page 8: Automated Historical Performance Analysis with kmemtracer

8 12th Kandroid Conference - www.kandroid.org

Why Historical Analysis?

Source: Memory and Performance at the 11th Korea Android Conference

Historical Performance Analysis

Page 9: Automated Historical Performance Analysis with kmemtracer

9 12th Kandroid Conference - www.kandroid.org

Why Historical Analysis?

Source: Memory and Performance at the 11th Korea Android Conference

Historical Performance Analysis

Page 10: Automated Historical Performance Analysis with kmemtracer

10 12th Kandroid Conference - www.kandroid.org

Why Historical Analysis?

Source: Memory and Performance at the 11th Korea Android Conference

Activity Navigation & Lifecycle

Historical Performance Analysis

Page 11: Automated Historical Performance Analysis with kmemtracer

11 12th Kandroid Conference - www.kandroid.org

How to Track Activity Navigation & Lifecycle?

Page 12: Automated Historical Performance Analysis with kmemtracer

12 12th Kandroid Conference - www.kandroid.org

How to Track Activity Navigation & Lifecycle?

Override onCreate, onStart, … of your Activity classes

Page 13: Automated Historical Performance Analysis with kmemtracer

13 12th Kandroid Conference - www.kandroid.org

How to Track Activity Navigation & Lifecycle?

Modify android.app.Activity

in the framework

Page 14: Automated Historical Performance Analysis with kmemtracer

14 12th Kandroid Conference - www.kandroid.org

How to Track Activity Navigation & Lifecycle?

Is there a way that does not require the modification of

an application or framework?

Page 15: Automated Historical Performance Analysis with kmemtracer

15 12th Kandroid Conference - www.kandroid.org

How to Track Activity Navigation & Lifecycle?

Is there a way that does not require the modification of

an application or framework?

Instrumentation

Page 16: Automated Historical Performance Analysis with kmemtracer

16 12th Kandroid Conference - www.kandroid.org

Page 17: Automated Historical Performance Analysis with kmemtracer

17 12th Kandroid Conference - www.kandroid.org

Android instrumentation is a set of control methods or "hooks" in the Android system. These hooks control an Android component independently of its normal lifecycle. They also control how Android loads applications. (http://developer.android.com/tools/testing/testing_android.html)

Page 18: Automated Historical Performance Analysis with kmemtracer

18 12th Kandroid Conference - www.kandroid.org

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 19: Automated Historical Performance Analysis with kmemtracer

19 12th Kandroid Conference - www.kandroid.org

Historical Analysis w/ kmemtracer

Test Package

Application Package

android.test. Instrumentation

TestRunner

Control

Page 20: Automated Historical Performance Analysis with kmemtracer

20 12th Kandroid Conference - www.kandroid.org

Historical Analysis w/ kmemtracer

https://github.com/snailee/kmemtracer-libs

Test Package

Application Package

android.test. Instrumentation

TestRunner

org.kandroid. memtracer.Memory

Instrumentation

Control Track

Trace Package

Page 21: Automated Historical Performance Analysis with kmemtracer

21 12th Kandroid Conference - www.kandroid.org

android.app.Instrumentation

void callActivityOnCreate(Activity, Bundle)

void callActivityOnDestroy(Activity)

void callActivityOnNewIntent(Activity, Intent)

void callActivityOnPause(Activity)

void callActivityOnPostCreate(Activity, Bundle)

void callActivityOnRestart(Activity)

void callActivityOnRestoreInstanceState(Activity, Bundle)

void callActivityOnResume(Activity)

void callActivityOnSaveInstanceState(Activity, Bundle)

void callActivityOnStart(Activity)

void callActivityOnStop(Activity)

void callActivityOnUserLeaving(Activity)

void callApplicationOnCreate(Application)

Page 22: Automated Historical Performance Analysis with kmemtracer

22 12th Kandroid Conference - www.kandroid.org

android.app.Instrumentation

void callActivityOnCreate(Activity, Bundle)

void callActivityOnDestroy(Activity)

void callActivityOnNewIntent(Activity, Intent)

void callActivityOnPause(Activity)

void callActivityOnPostCreate(Activity, Bundle)

void callActivityOnRestart(Activity)

void callActivityOnRestoreInstanceState(Activity, Bundle)

void callActivityOnResume(Activity)

void callActivityOnSaveInstanceState(Activity, Bundle)

void callActivityOnStart(Activity)

void callActivityOnStop(Activity)

void callActivityOnUserLeaving(Activity)

void callApplicationOnCreate(Application)

Call for Control

Page 23: Automated Historical Performance Analysis with kmemtracer

23 12th Kandroid Conference - www.kandroid.org

android.app.Instrumentation

void callActivityOnCreate(Activity, Bundle)

void callActivityOnDestroy(Activity)

void callActivityOnNewIntent(Activity, Intent)

void callActivityOnPause(Activity)

void callActivityOnPostCreate(Activity, Bundle)

void callActivityOnRestart(Activity)

void callActivityOnRestoreInstanceState(Activity, Bundle)

void callActivityOnResume(Activity)

void callActivityOnSaveInstanceState(Activity, Bundle)

void callActivityOnStart(Activity)

void callActivityOnStop(Activity)

void callActivityOnUserLeaving(Activity)

void callApplicationOnCreate(Application)

Call for Control

Override for Track

Page 24: Automated Historical Performance Analysis with kmemtracer

24 12th Kandroid Conference - www.kandroid.org

org.kandroid.memtracer.MemoryInstrumentation

public class MemoryInstrumentation extends Instrumentation { private static final String ARGUMENT_INSTRUMENT_CLASS = "class"; private MemoryTracer mMemoryTracer; private String mMainActivityName; ... @Override public void onCreate(Bundle arguments) { mMainActivityName = arguments.getString(ARGUMENT_INSTRUMENT_CLASS); mMemoryTracer = createMemoryTracer(); mMemoryTracer.startTracing(getTargetContext().getPackageName()); start(); } @Override public void callActivityOnCreate(Activity activity, Bundle icicle) { String tag = activity.getLocalClassName()+"-OnCreate"; Bundle snapshot = mMemoryTracer.addSnapshot(tag); super.callActivityOnCreate(activity, icicle); } }

Page 25: Automated Historical Performance Analysis with kmemtracer

25 12th Kandroid Conference - www.kandroid.org

Tracing Performance Snapshots in kmemtracer

public class MemoryInstrumentation extends Instrumentation { ... ... }

Page 26: Automated Historical Performance Analysis with kmemtracer

26 12th Kandroid Conference - www.kandroid.org

Tracing Performance Snapshots in kmemtracer

public class MemoryInstrumentation extends Instrumentation { ... private MemoryTracer mMemoryTracer; ... @Override public void onCreate(Bundle arguments) { ... mMemoryTracer = createMemoryTracer(); ... } ... ... }

Create an instance of MemoryTracer

Page 27: Automated Historical Performance Analysis with kmemtracer

27 12th Kandroid Conference - www.kandroid.org

Tracing Performance Snapshots in kmemtracer

public class MemoryInstrumentation extends Instrumentation { ... private MemoryTracer mMemoryTracer; ... @Override public void onCreate(Bundle arguments) { ... mMemoryTracer = createMemoryTracer(); ... } ... @Override public void callActivityOnCreate(Activity activity, Bundle icicle) { String tag = activity.getLocalClassName()+"-OnCreate"; Bundle snapshot = mMemoryTracer.addSnapshot(tag); ... } ... }

Create an instance of MemoryTracer

Add performance snapshot

Page 28: Automated Historical Performance Analysis with kmemtracer

28 12th Kandroid Conference - www.kandroid.org

MemoryTracer.addSnapshot

public Bundle addSnapshot(String label) { ...

Page 29: Automated Historical Performance Analysis with kmemtracer

29 12th Kandroid Conference - www.kandroid.org

MemoryTracer.addSnapshot

public Bundle addSnapshot(String label) { ... long nativeMax = Debug.getNativeHeapSize() / 1024; long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; long nativeFree = Debug.getNativeHeapFreeSize() / 1024; Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memInfo); ...

Get usage data

Page 30: Automated Historical Performance Analysis with kmemtracer

30 12th Kandroid Conference - www.kandroid.org

MemoryTracer.addSnapshot

public Bundle addSnapshot(String label) { ... long nativeMax = Debug.getNativeHeapSize() / 1024; long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; long nativeFree = Debug.getNativeHeapFreeSize() / 1024; Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memInfo); ... Bundle snapshot = new Bundle(); snapshot.putString(METRIC_KEY_LABEL, label); ... snapshot.putLong(METRIC_KEY_NATIVE_SIZE, nativeMax); snapshot.putLong(METRIC_KEY_NATIVE_ALLOCATED, nativeAllocated); snapshot.putLong(METRIC_KEY_NATIVE_FREE, nativeFree); ...

Get usage data

Save data to a bundle

Page 31: Automated Historical Performance Analysis with kmemtracer

31 12th Kandroid Conference - www.kandroid.org

MemoryTracer.addSnapshot

public Bundle addSnapshot(String label) { ... long nativeMax = Debug.getNativeHeapSize() / 1024; long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; long nativeFree = Debug.getNativeHeapFreeSize() / 1024; Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memInfo); ... Bundle snapshot = new Bundle(); snapshot.putString(METRIC_KEY_LABEL, label); ... snapshot.putLong(METRIC_KEY_NATIVE_SIZE, nativeMax); snapshot.putLong(METRIC_KEY_NATIVE_ALLOCATED, nativeAllocated); snapshot.putLong(METRIC_KEY_NATIVE_FREE, nativeFree); ... if (mResultsWriter != null) { mResultsWriter.writeTraceSnapshot(snapshot); } return snapshot; }

Get usage data

Write date using ResultsWriter

Save data to a bundle

Page 32: Automated Historical Performance Analysis with kmemtracer

32 12th Kandroid Conference - www.kandroid.org

MemoryTracerCsvWriter$writeTraceSnapshot

public class MemoryTraceCsvWriter implements MemoryTracer.ResultsWriter { ...

Page 33: Automated Historical Performance Analysis with kmemtracer

33 12th Kandroid Conference - www.kandroid.org

MemoryTracerCsvWriter$writeTraceSnapshot

public class MemoryTraceCsvWriter implements MemoryTracer.ResultsWriter { ... public MemoryTraceCsvWriter(String[] metricKeys) { mMetricKeys = metricKeys; } ...

Create an instance with metrics to print

Page 34: Automated Historical Performance Analysis with kmemtracer

34 12th Kandroid Conference - www.kandroid.org

MemoryTracerCsvWriter$writeTraceSnapshot

public class MemoryTraceCsvWriter implements MemoryTracer.ResultsWriter { ... public MemoryTraceCsvWriter(String[] metricKeys) { mMetricKeys = metricKeys; } ... private void openTraceFile(String filename) { ... mTraceOut = new PrintStream(new FileOutputStream(traceFile)); ... } ...

Create an instance with metrics to print

Open a trace file in the sdcard at init

Page 35: Automated Historical Performance Analysis with kmemtracer

35 12th Kandroid Conference - www.kandroid.org

MemoryTracerCsvWriter$writeTraceSnapshot

public class MemoryTraceCsvWriter implements MemoryTracer.ResultsWriter { ... public MemoryTraceCsvWriter(String[] metricKeys) { mMetricKeys = metricKeys; } ... private void openTraceFile(String filename) { ... mTraceOut = new PrintStream(new FileOutputStream(traceFile)); ... } ... @Override public void writeTraceSnapshot(Bundle snapshot) { PrintStream out = mTraceOut; for (String key : mMetricKeys) { out.print(snapshot.get(key)); out.print(','); } out.println(); out.flush(); } ... }

Create an instance with metrics to print

Write snapshot to a line of the file

Open a trace file in the sdcard at init

Page 36: Automated Historical Performance Analysis with kmemtracer

36 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

Page 37: Automated Historical Performance Analysis with kmemtracer

37 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

1. Create an Android Test Project for the trace package.

Page 38: Automated Historical Performance Analysis with kmemtracer

38 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

1. Create an Android Test Project for the trace package.

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

Page 39: Automated Historical Performance Analysis with kmemtracer

39 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

1. Create an Android Test Project for the trace package.

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

3. Edit the <instrumentation> element in the Manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.apis.test" android:versionCode="1" android:versionName="1.0"> <instrumentation android:name="org.kandroid.memtracer.MemoryInstrumentation" android:targetPackage="com.example.android.apis" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher“> <uses-library android:theme="android.text.runner“> </application> </manifest>

Page 40: Automated Historical Performance Analysis with kmemtracer

40 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

4. Install the trace package as well as the application package.

$ adb install –r ApiDemos.apk $ adb install –r ApiDemosTest.apk

Page 41: Automated Historical Performance Analysis with kmemtracer

41 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

4. Install the trace package as well as the application package.

5. Start the instrumentation with ‘am instrument’ in the shell.

$ adb install –r ApiDemos.apk $ adb install –r ApiDemosTest.apk

$ adb shell am instrument -e class \ > com.example.android.apis.ApiDemos \ > com.example.android.apis.tests/\ > org.kandroid.memtracer.MemoryInstrumentation

Page 42: Automated Historical Performance Analysis with kmemtracer

42 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

4. Install the trace package as well as the application package.

5. Start the instrumentation with ‘am instrument’ in the shell.

6. Do interact with the application.

$ adb install –r ApiDemos.apk $ adb install –r ApiDemosTest.apk

$ adb shell am instrument -e class \ > com.example.android.apis.ApiDemos \ > com.example.android.apis.tests/\ > org.kandroid.memtracer.MemoryInstrumentation

Page 43: Automated Historical Performance Analysis with kmemtracer

43 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

4. Install the trace package as well as the application package.

5. Start the instrumentation with ‘am instrument’ in the shell.

6. Do interact with the application.

7. Pull the trace file ‘kmemtrace.csv’ from /sdcard/kmemtracer.

$ adb install –r ApiDemos.apk $ adb install –r ApiDemosTest.apk

$ adb shell am instrument -e class \ > com.example.android.apis.ApiDemos \ > com.example.android.apis.tests/\ > org.kandroid.memtracer.MemoryInstrumentation

$ adb pull /sdcard/kmemtracer/kmemtrace.csv .

Page 44: Automated Historical Performance Analysis with kmemtracer

44 12th Kandroid Conference - www.kandroid.org

How to Use kmemtracer?

8. Open the trace file with Excel and create charts.

Page 45: Automated Historical Performance Analysis with kmemtracer

45 12th Kandroid Conference - www.kandroid.org

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 46: Automated Historical Performance Analysis with kmemtracer

46 12th Kandroid Conference - www.kandroid.org

Automate Controlling UI

Test Package

Application Package

android.test. Instrumentation

TestRunner

org.kandroid. memtracer.Memory

Instrumentation

Control Track

Trace Package

Page 47: Automated Historical Performance Analysis with kmemtracer

47 12th Kandroid Conference - www.kandroid.org

Automate Controlling UI

Test Package

Application Package

android.test. Instrumentation

TestRunner

org.kandroid. memtracer.Memory

Instrumentation

Control Track

Trace Package

Page 48: Automated Historical Performance Analysis with kmemtracer

48 12th Kandroid Conference - www.kandroid.org

Automate Controlling UI

Test Package

Application Package

android.test. Instrumentation

TestRunner

org.kandroid. memtracer.Memory

Instrumentation

Control Track

Trace Package

Page 49: Automated Historical Performance Analysis with kmemtracer

49 12th Kandroid Conference - www.kandroid.org

Three Alternatives

Page 50: Automated Historical Performance Analysis with kmemtracer

50 12th Kandroid Conference - www.kandroid.org

Three Alternatives

Instrumentation

API Level 1 ~ Single activity Framework APIs White-box

http://developer.android.com/tools/testing/testing_android.html

Page 51: Automated Historical Performance Analysis with kmemtracer

51 12th Kandroid Conference - www.kandroid.org

Three Alternatives

Instrumentation Robotium

API Level 1 ~ Single activity Framework APIs White-box

2010/01 ~ Multiple activities High-level APIs White-box & black-box

http://developer.android.com/tools/testing/testing_android.html https://code.google.com/p/robotium/

Page 52: Automated Historical Performance Analysis with kmemtracer

52 12th Kandroid Conference - www.kandroid.org

Three Alternatives

Instrumentation Robotium uiautomator

API Level 1 ~ Single activity Framework APIs White-box

2010/01 ~ Multiple activities High-level APIs White-box & black-box

API Level 17 ~ Multiple processes Simple APIs Black-box

http://developer.android.com/tools/testing/testing_android.html https://code.google.com/p/robotium/ http://developer.android.com/tools/testing/testing_ui.html

Page 53: Automated Historical Performance Analysis with kmemtracer

53 12th Kandroid Conference - www.kandroid.org

Case Study: ApiDemos

https://github.com/snailee/deview-2013-samples

Page 54: Automated Historical Performance Analysis with kmemtracer

54 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

https://github.com/snailee/deview-2013-samples

Page 55: Automated Historical Performance Analysis with kmemtracer

55 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

https://github.com/snailee/deview-2013-samples

Page 56: Automated Historical Performance Analysis with kmemtracer

56 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

Scroll down

https://github.com/snailee/deview-2013-samples

Page 57: Automated Historical Performance Analysis with kmemtracer

57 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

Scroll down

https://github.com/snailee/deview-2013-samples

Page 58: Automated Historical Performance Analysis with kmemtracer

58 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

Scroll down

Click

https://github.com/snailee/deview-2013-samples

Page 59: Automated Historical Performance Analysis with kmemtracer

59 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

Scroll down

Click

https://github.com/snailee/deview-2013-samples

Page 60: Automated Historical Performance Analysis with kmemtracer

60 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

Scroll down

Click Click

https://github.com/snailee/deview-2013-samples

Page 61: Automated Historical Performance Analysis with kmemtracer

61 12th Kandroid Conference - www.kandroid.org

Click

Case Study: ApiDemos

Scroll down

Click Click

https://github.com/snailee/deview-2013-samples

Page 62: Automated Historical Performance Analysis with kmemtracer

62 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

Page 63: Automated Historical Performance Analysis with kmemtracer

63 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

1. Create an Android Test Project for the test package.

$ android create test-project --main ../ApiDemos \ > –-name ApiDemosInstrumentTest --path ApiDemosInstrumentTest

Page 64: Automated Historical Performance Analysis with kmemtracer

64 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

1. Create an Android Test Project for the test package.

2. Add a test class inheriting ActivityInstrumentationTestCase2.

$ android create test-project --main ../ApiDemos \ > –-name ApiDemosInstrumentTest --path ApiDemosInstrumentTest

public class ApiDemosInstrumentTest extends ActivityInstrumentationTestCase2<ApiDemos> { ... public ApiDemosInstrumentTest() { super(ApiDemos.class); }

Page 65: Automated Historical Performance Analysis with kmemtracer

65 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

1. Create an Android Test Project for the test package.

2. Add a test class inheriting ActivityInstrumentationTestCase2.

3. Add a test method and launch the activity.

$ android create test-project --main ../ApiDemos \ > –-name ApiDemosInstrumentTest --path ApiDemosInstrumentTest

public class ApiDemosInstrumentTest extends ActivityInstrumentationTestCase2<ApiDemos> { ... public ApiDemosInstrumentTest() { super(ApiDemos.class); }

public void testNavigate() { final ApiDemos apiDemos = getActivity(); ... }

Page 66: Automated Historical Performance Analysis with kmemtracer

66 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

public void testNavigate() { ...

4. Find and act on a control.

Page 67: Automated Historical Performance Analysis with kmemtracer

67 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

public void testNavigate() { ... final ListView demoList = apiDemos.getListView(); final View graphicsDemo = getChildViewByText(demoList, "Graphics"); assertNotNull("Graphics demo should exist", graphicsDemo);

Find a view to control

4. Find and act on a control.

Page 68: Automated Historical Performance Analysis with kmemtracer

68 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

public void testNavigate() { ... final ListView demoList = apiDemos.getListView(); final View graphicsDemo = getChildViewByText(demoList, "Graphics"); assertNotNull("Graphics demo should exist", graphicsDemo); apiDemos.runOnUiThread(new Runnable() { public void run() { int pos = demoList.getPositionForView(graphicsDemo); demoList.smoothScrollToPosition(pos); demoList.setSelection(pos); demoList.performItemClick(graphicsDemo, pos, graphicsDemo.getId()); } }); ...

Find a view to control

Act on the view

4. Find and act on a control.

Page 69: Automated Historical Performance Analysis with kmemtracer

69 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

public void testNavigate() { ... final ListView demoList = apiDemos.getListView(); final View graphicsDemo = getChildViewByText(demoList, "Graphics"); assertNotNull("Graphics demo should exist", graphicsDemo); apiDemos.runOnUiThread(new Runnable() { public void run() { int pos = demoList.getPositionForView(graphicsDemo); demoList.smoothScrollToPosition(pos); demoList.setSelection(pos); demoList.performItemClick(graphicsDemo, pos, graphicsDemo.getId()); } }); ...

Find a view to control

Act on the view

4. Find and act on a control.

Page 70: Automated Historical Performance Analysis with kmemtracer

70 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private View getChildViewByText(ListView listView, String text) {

4. Find and act on a control: getChildViewByText.

Page 71: Automated Historical Performance Analysis with kmemtracer

71 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private View getChildViewByText(ListView listView, String text) { View view = null; int count = listView.getCount(); Log.e(TAG, “no. children = " + count); for (int i = 0; i < count; i++) { TextView textView = (TextView) listView.getChildAt(i);

Iterate through child views

4. Find and act on a control: getChildViewByText.

Page 72: Automated Historical Performance Analysis with kmemtracer

72 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private View getChildViewByText(ListView listView, String text) { View view = null; int count = listView.getCount(); Log.e(TAG, “no. children = " + count); for (int i = 0; i < count; i++) { TextView textView = (TextView) listView.getChildAt(i); Log.e(TAG, i + "-th text view is " + textView); if (textView != null && textView.getText().equals(text)) { view = textView; break; } } return view; }

Iterate through child views

Compare texts

4. Find and act on a control: getChildViewByText.

Page 73: Automated Historical Performance Analysis with kmemtracer

73 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private View getChildViewByText(ListView listView, String text) { View view = null; int count = listView.getCount(); Log.e(TAG, “no. children = " + count); for (int i = 0; i < count; i++) { TextView textView = (TextView) listView.getChildAt(i); Log.e(TAG, i + "-th text view is " + textView); if (textView != null && textView.getText().equals(text)) { view = textView; break; } } return view; }

Iterate through child views

Compare texts

4. Find and act on a control: getChildViewByText.

com.example.android.apis.instrument.test.ApiDemosInstrumentTest: INSTRUMENTATION_RESULT: shortMsg=junit.framework.AssertionFailedError INSTRUMENTATION_RESULT: longMsg= junit.framework.AssertionFailedError: OpenGL ES demo should exist INSTRUMENTATION_CODE: 0

Page 74: Automated Historical Performance Analysis with kmemtracer

74 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private View getChildViewByText(final ListView listView, String text) { ... for (int i = 0; i < count; i++) { TextView textView = (TextView) listView.getChildAt(i); if (textView != null && textView.getText().equals(text)) { view = textView; break; } if (textView == null) { final int lastPos = listView.getLastVisiblePosition(); getInstrumentation().runOnMainSync(new Runnable() { public void run() { listView.smoothScrollToPosition(lastPos + 5, lastPos); } }); getInstrumentation().waitForIdleSync(); i = listView.getFirstVisiblePosition(); } } return view; }

4. Find and act on a control: Revised getChildViewByText.

Page 75: Automated Historical Performance Analysis with kmemtracer

75 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private View getChildViewByText(final ListView listView, String text) { ... for (int i = 0; i < count; i++) { TextView textView = (TextView) listView.getChildAt(i); if (textView != null && textView.getText().equals(text)) { view = textView; break; } if (textView == null) { final int lastPos = listView.getLastVisiblePosition(); getInstrumentation().runOnMainSync(new Runnable() { public void run() { listView.smoothScrollToPosition(lastPos + 5, lastPos); } }); getInstrumentation().waitForIdleSync(); i = listView.getFirstVisiblePosition(); } } return view; }

Scroll down

4. Find and act on a control: Revised getChildViewByText.

Page 76: Automated Historical Performance Analysis with kmemtracer

76 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private int getPositionForChildViewByText(ListView listView, String text) { ListAdapter listAdapter = listView.getAdapter(); int count = listAdapter.getCount(); Log.e(TAG, "children: " + count); for (int i = 0; i < count; i++) { String itemText = (String) ((Map)listAdapter.getItem(i)).get("title"); Log.e(TAG, i + "-th text item " + itemText); if (itemText != null && itemText.equals(text)) { return i; } } return -1; }

4. Find and act on a control: getPositionForChildViewByText.

... final ListView graphicsDemoList = graphicsDemoActivity.getListView(); int pos = getPositionForChildViewByText(graphicsDemoList, "OpenGL ES"); assertTrue("Cannot find OpenGL ES", pos != -1); ...

Page 77: Automated Historical Performance Analysis with kmemtracer

77 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

private int getPositionForChildViewByText(ListView listView, String text) { ListAdapter listAdapter = listView.getAdapter(); int count = listAdapter.getCount(); Log.e(TAG, "children: " + count); for (int i = 0; i < count; i++) { String itemText = (String) ((Map)listAdapter.getItem(i)).get("title"); Log.e(TAG, i + "-th text item " + itemText); if (itemText != null && itemText.equals(text)) { return i; } } return -1; }

4. Find and act on a control: getPositionForChildViewByText.

... final ListView graphicsDemoList = graphicsDemoActivity.getListView(); int pos = getPositionForChildViewByText(graphicsDemoList, "OpenGL ES"); assertTrue("Cannot find OpenGL ES", pos != -1); ...

Page 78: Automated Historical Performance Analysis with kmemtracer

78 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

5. Monitor a new Activity to launch.

public void testNavigate() { ... Instrumentation.ActivityMonitor graphicsDemoActivityMonitor = getInstrumentation().addMonitor( ApiDemos.class.getName(), null, false); final ApiDemos graphicsDemoActivity = (ApiDemos) graphicsDemoActivityMonitor.waitForActivity(); assertNotNull("Graphics Demo (ApiDemos) activity should not be null", graphicsDemoActivity); ...

Page 79: Automated Historical Performance Analysis with kmemtracer

79 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

5. Monitor a new Activity to launch.

6. Iterate step 4 & 5.

public void testNavigate() { ... Instrumentation.ActivityMonitor graphicsDemoActivityMonitor = getInstrumentation().addMonitor( ApiDemos.class.getName(), null, false); final ApiDemos graphicsDemoActivity = (ApiDemos) graphicsDemoActivityMonitor.waitForActivity(); assertNotNull("Graphics Demo (ApiDemos) activity should not be null", graphicsDemoActivity); ...

Page 80: Automated Historical Performance Analysis with kmemtracer

80 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

7. Build and Install the test package.

$ cd ApiDemosInstrumentTest $ ant debug $ adb install –r bin/ApiDemosInstrumentTest-debug.apk

Page 81: Automated Historical Performance Analysis with kmemtracer

81 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

7. Build and Install the test package.

8. Run the test and see the result.

$ adb shell am instrument -w \ > com.example.android.apis.instrument.test/\ > android.test.InstrumentationTestRunner

$ cd ApiDemosInstrumentTest $ ant debug $ adb install –r bin/ApiDemosInstrumentTest-debug.apk

Page 82: Automated Historical Performance Analysis with kmemtracer

82 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Instrumentation

7. Build and Install the test package.

8. Run the test and see the result.

$ adb shell am instrument -w \ > com.example.android.apis.instrument.test/\ > android.test.InstrumentationTestRunner

$ cd ApiDemosInstrumentTest $ ant debug $ adb install –r bin/ApiDemosInstrumentTest-debug.apk

$ ant test

Page 83: Automated Historical Performance Analysis with kmemtracer

83 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

Page 84: Automated Historical Performance Analysis with kmemtracer

84 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

1. Create an Android Test Project for the test package.

$ android create test-project --main ../ApiDemos \ > –-name ApiDemosRobotiumTest --path ApiDemosRobotiumTest

Page 85: Automated Historical Performance Analysis with kmemtracer

85 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

1. Create an Android Test Project for the test package.

2. Add ‘robotium-solo-<ver>.jar’ in the ‘libs’ directory.

$ android create test-project --main ../ApiDemos \ > –-name ApiDemosRobotiumTest --path ApiDemosRobotiumTest

Page 86: Automated Historical Performance Analysis with kmemtracer

86 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

1. Create an Android Test Project for the test package.

2. Add ‘robotium-solo-<ver>.jar’ in the ‘libs’ directory.

3. Add a test class inheriting ActivityInstrumentationTestCase2.

$ android create test-project --main ../ApiDemos \ > –-name ApiDemosRobotiumTest --path ApiDemosRobotiumTest

public class ApiDemosRobotiumTest extends ActivityInstrumentationTestCase2<ApiDemos> { ... public ApiDemosRobotiumTest() { super(ApiDemos.class); } ...

Page 87: Automated Historical Performance Analysis with kmemtracer

87 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

4. Create an instance of Solo in the setUp method and

close open activities in the tearDown method.

... private Solo solo; ... @Override public void setUp() throws Exception { solo = new Solo(getInstrumentation(), getActivity()); } @Override public void tearDown() throws Exception { solo.finishOpenedActivities(); } ...

Page 88: Automated Historical Performance Analysis with kmemtracer

88 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

4. Find and act on a control.

public void testNavigate() { solo.assertCurrentActivity("Should be on ApiDemos", ApiDemos.class); solo.clickOnText("^Graphics$", 1, true); solo.assertCurrentActivity("Should be on a new ApiDemos", ApiDemos.class, true); solo.clickOnText("^OpenGL ES$", 1, true); solo.assertCurrentActivity("Should be on a new ApiDemos", ApiDemos.class, true); solo.clickOnText("^Kube$", 1, true); solo.assertCurrentActivity("Should be on a Kube", Kube.class, true); solo.sleep(3000); }

Page 89: Automated Historical Performance Analysis with kmemtracer

89 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

6. Build and Install the test package.

$ cd ApiDemosRobotiumTest $ ant debug $ adb install –r bin/ApiDemosRobotiumTest-debug.apk

Page 90: Automated Historical Performance Analysis with kmemtracer

90 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

6. Build and Install the test package.

7. Run the test and see the result.

$ adb shell am instrument -w \ > com.example.android.apis.robotium.test/\ > android.test.InstrumentationTestRunner

$ cd ApiDemosRobotiumTest $ ant debug $ adb install –r bin/ApiDemosRobotiumTest-debug.apk

Page 91: Automated Historical Performance Analysis with kmemtracer

91 12th Kandroid Conference - www.kandroid.org

Controlling UI Using Robotium

6. Build and Install the test package.

7. Run the test and see the result.

$ adb shell am instrument -w \ > com.example.android.apis.robotium.test/\ > android.test.InstrumentationTestRunner

$ cd ApiDemosRobotiumTest $ ant debug $ adb install –r bin/ApiDemosRobotiumTest-debug.apk

$ ant test

Page 92: Automated Historical Performance Analysis with kmemtracer

92 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

1. Create an Java Project for the test jar.

Page 93: Automated Historical Performance Analysis with kmemtracer

93 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

1. Create an Java Project for the test jar.

2. Add ‘android.jar’ and ‘uiautomator.jar’ in the build path.

Page 94: Automated Historical Performance Analysis with kmemtracer

94 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

1. Create an Java Project for the test jar.

2. Add ‘android.jar’ and ‘uiautomator.jar’ in the build path.

3. Add a test class inheriting UiAutomatorTestCase.

public class ApiDemosUiAutoTest extends UiAutomatorTestCase { ... }

Page 95: Automated Historical Performance Analysis with kmemtracer

95 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

4. Launch ApiDemos.

public void testNavigate() throws UiObjectNotFoundException {

Page 96: Automated Historical Performance Analysis with kmemtracer

96 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

4. Launch ApiDemos.

public void testNavigate() throws UiObjectNotFoundException { getUiDevice().pressHome();

Press home button

Page 97: Automated Historical Performance Analysis with kmemtracer

97 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

4. Launch ApiDemos.

public void testNavigate() throws UiObjectNotFoundException { getUiDevice().pressHome(); UiObject allAppsButton = new UiObject( new UiSelector().description("Apps")); allAppsButton.clickAndWaitForNewWindow();

Click ‘All Apps’ button

Press home button

Page 98: Automated Historical Performance Analysis with kmemtracer

98 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

4. Launch ApiDemos.

public void testNavigate() throws UiObjectNotFoundException { getUiDevice().pressHome(); UiObject allAppsButton = new UiObject( new UiSelector().description("Apps")); allAppsButton.clickAndWaitForNewWindow(); UiObject appsTab = new UiObject(new UiSelector().text("Apps")); appsTab.click();

Click ‘All Apps’ button

Press home button

Click ‘Apps’ tab

Page 99: Automated Historical Performance Analysis with kmemtracer

99 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

4. Launch ApiDemos.

public void testNavigate() throws UiObjectNotFoundException { getUiDevice().pressHome(); UiObject allAppsButton = new UiObject( new UiSelector().description("Apps")); allAppsButton.clickAndWaitForNewWindow(); UiObject appsTab = new UiObject(new UiSelector().text("Apps")); appsTab.click(); UiScrollable appList = new UiScrollable( new UiSelector().scrollable(true)); appList.setAsHorizontalList(); UiObject apiDemos = appList.getChildByText( new UiSelector().className(TextView.class.getName()), "API Demos"); apiDemos.clickAndWaitForNewWindow();

Click ‘All Apps’ button

Press home button

Click ‘Apps’ tab

Click ‘ApiDemos’ icon

Page 100: Automated Historical Performance Analysis with kmemtracer

100 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

4. Launch ApiDemos.

public void testNavigate() throws UiObjectNotFoundException { getUiDevice().pressHome(); UiObject allAppsButton = new UiObject( new UiSelector().description("Apps")); allAppsButton.clickAndWaitForNewWindow(); UiObject appsTab = new UiObject(new UiSelector().text("Apps")); appsTab.click(); UiScrollable appList = new UiScrollable( new UiSelector().scrollable(true)); appList.setAsHorizontalList(); UiObject apiDemos = appList.getChildByText( new UiSelector().className(TextView.class.getName()), "API Demos"); apiDemos.clickAndWaitForNewWindow(); UiObject apiDemosPackage = new UiObject( new UiSelector().packageName("com.example.android.apis")); assertTrue("Should be on ApiDemos", apiDemosPackage.exists()); ...

Click ‘All Apps’ button

Press home button

Click ‘Apps’ tab

Click ‘ApiDemos’ icon

Validate the launch of ‘ApiDemos’

Page 101: Automated Historical Performance Analysis with kmemtracer

101 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

5. Find and act on a control.

... UiScrollable demoList = new UiScrollable( new UiSelector().className("android.widget.ListView")); UiObject graphicsDemo = demoList.getChildByText( new UiSelector().className(TextView.class.getName()), “Graphics"); graphicsDemo.clickAndWaitForNewWindow(); UiObject alphaBitmapDemo = new UiObject( new UiSelector().text(“AlphaBitmap")); assertTrue(“AlphaBitmap should be visible", alpahBitmapDemo.exists()); ...

Page 102: Automated Historical Performance Analysis with kmemtracer

102 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

5. Find and act on a control.

6. Iterate step 5.

... UiScrollable demoList = new UiScrollable( new UiSelector().className("android.widget.ListView")); UiObject graphicsDemo = demoList.getChildByText( new UiSelector().className(TextView.class.getName()), “Graphics"); graphicsDemo.clickAndWaitForNewWindow(); UiObject alphaBitmapDemo = new UiObject( new UiSelector().text(“AlphaBitmap")); assertTrue(“AlphaBitmap should be visible", alpahBitmapDemo.exists()); ...

Page 103: Automated Historical Performance Analysis with kmemtracer

103 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

7. Compile your test case into a JAR file.

$ android create uitest-project --name ApiDemosUiAutoTest \ > --target 1 --path ApiDemosUiAutoTest $ cd ApiDemosUiAutoTest $ ant build

Page 104: Automated Historical Performance Analysis with kmemtracer

104 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

7. Compile your test case into a JAR file.

8. Install the jar on your test device.

$ android create uitest-project --name ApiDemosUiAutoTest \ > --target 1 --path ApiDemosUiAutoTest $ cd ApiDemosUiAutoTest $ ant build

$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/

Page 105: Automated Historical Performance Analysis with kmemtracer

105 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

7. Compile your test case into a JAR file.

8. Install the jar on your test device.

9. Run the test and view the test result.

$ android create uitest-project --name ApiDemosUiAutoTest \ > --target 1 --path ApiDemosUiAutoTest $ cd ApiDemosUiAutoTest $ ant build

$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/

$ adb shell uiautomator runtest ApiDemosUiAutoTest.jar \ > –c com.example.android.apis.uiauto.test.ApiDemosUiAutoTest

Page 106: Automated Historical Performance Analysis with kmemtracer

106 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

7. Compile your test case into a JAR file.

8. Install the jar on your test device.

9. Run the test and view the test result.

$ android create uitest-project --name ApiDemosUiAutoTest \ > --target 1 --path ApiDemosUiAutoTest $ cd ApiDemosUiAutoTest $ ant build

$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/

$ adb shell uiautomator runtest ApiDemosUiAutoTest.jar \ > –c com.example.android.apis.uiauto.test.ApiDemosUiAutoTest

$ ant test

Page 107: Automated Historical Performance Analysis with kmemtracer

107 12th Kandroid Conference - www.kandroid.org

Controlling UI Using uiautomator

7. Compile your test case into a JAR file.

8. Install the jar on your test device.

9. Run the test and view the test result.

$ android create uitest-project --name ApiDemosUiAutoTest \ > --target 1 --path ApiDemosUiAutoTest $ cd ApiDemosUiAutoTest $ ant build

$ adb push bin/ApiDemosUiAutoTest.jar /data/local/tmp/

$ adb shell uiautomator runtest ApiDemosUiAutoTest.jar \ > –c com.example.android.apis.uiauto.test.ApiDemosUiAutoTest

$ ant test

Page 108: Automated Historical Performance Analysis with kmemtracer

108 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

Page 109: Automated Historical Performance Analysis with kmemtracer

109 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

1. Modify org.kandroid.memtracer.MemoryInstrumentation.

public class MemoryInstrumentation extends InstrumentationTestRunner { ... @Override public void onCreate(Bundle arguments) { ... start(); super.onCreate(arguments); } @Override public void onStart() { String mainActivityName = getMainActivityName(); if (mainActivityName != null && mainActivityName.length() > 0) { launchMainActivity(getTargetContext().getPackageName(), getMainActivityName()); } else { super.onStart(); } } ...

Page 110: Automated Historical Performance Analysis with kmemtracer

110 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

1. Modify org.kandroid.memtracer.MemoryInstrumentation.

public class MemoryInstrumentation extends InstrumentationTestRunner { ... @Override public void onCreate(Bundle arguments) { ... start(); super.onCreate(arguments); } @Override public void onStart() { String mainActivityName = getMainActivityName(); if (mainActivityName != null && mainActivityName.length() > 0) { launchMainActivity(getTargetContext().getPackageName(), getMainActivityName()); } else { super.onStart(); } } ...

Page 111: Automated Historical Performance Analysis with kmemtracer

111 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

1. Modify org.kandroid.memtracer.MemoryInstrumentation.

public class MemoryInstrumentation extends InstrumentationTestRunner { ... @Override public void onCreate(Bundle arguments) { ... start(); super.onCreate(arguments); } @Override public void onStart() { String mainActivityName = getMainActivityName(); if (mainActivityName != null && mainActivityName.length() > 0) { launchMainActivity(getTargetContext().getPackageName(), getMainActivityName()); } else { super.onStart(); } } ...

Page 112: Automated Historical Performance Analysis with kmemtracer

112 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

1. Modify org.kandroid.memtracer.MemoryInstrumentation.

public class MemoryInstrumentation extends InstrumentationTestRunner { ... @Override public void onCreate(Bundle arguments) { ... start(); super.onCreate(arguments); } @Override public void onStart() { String mainActivityName = getMainActivityName(); if (mainActivityName != null && mainActivityName.length() > 0) { launchMainActivity(getTargetContext().getPackageName(), getMainActivityName()); } else { super.onStart(); } } ...

Page 113: Automated Historical Performance Analysis with kmemtracer

113 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

Page 114: Automated Historical Performance Analysis with kmemtracer

114 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

3. Edit the <instrumentation> element in the manifest file.

Page 115: Automated Historical Performance Analysis with kmemtracer

115 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

3. Edit the <instrumentation> element in the manifest file.

4. Build and Install the test package.

$ cd ApiDemosInstrumentTest $ ant debug $ adb install –r bin/ApiDemosInstrumentTest-debug.apk

Page 116: Automated Historical Performance Analysis with kmemtracer

116 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

3. Edit the <instrumentation> element in the manifest file.

4. Build and Install the test package.

5. Run the test and see the result.

$ adb shell am instrument -w \ > com.example.android.apis.instrument.test/\ > org.kandroid.memtracer.MemoryInstrumentation

$ cd ApiDemosInstrumentTest $ ant debug $ adb install –r bin/ApiDemosInstrumentTest-debug.apk

Page 117: Automated Historical Performance Analysis with kmemtracer

117 12th Kandroid Conference - www.kandroid.org

Doing both Controlling and Tracking

2. Add ‘kmemtracer-libs.jar’ in the ‘libs’ directory.

3. Edit the <instrumentation> element in the manifest file.

4. Build and Install the test package.

5. Run the test and see the result.

6. Pull the trace file ‘kmemtrace.csv’ from /sdcard/kmemtracer.

$ adb shell am instrument -w \ > com.example.android.apis.instrument.test/\ > org.kandroid.memtracer.MemoryInstrumentation

$ cd ApiDemosInstrumentTest $ ant debug $ adb install –r bin/ApiDemosInstrumentTest-debug.apk

$ adb pull /sdcard/kmemtracer/kmemtrace.csv .

Page 118: Automated Historical Performance Analysis with kmemtracer

118 12th Kandroid Conference - www.kandroid.org

Instrument Robotium uiautomator

Compatibility O O X

Multi-activities O O

Multi-processes X X O

Ease of APIs X O

Black box X O

Comparison

Page 119: Automated Historical Performance Analysis with kmemtracer

119 12th Kandroid Conference - www.kandroid.org

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 120: Automated Historical Performance Analysis with kmemtracer

120 12th Kandroid Conference - www.kandroid.org

What to Measure?

Rendering (or GUI) Performance Frame Rate (FPS), Frame Time , Jankiness, Input Latency, …

Resource Consumption Memory, CPU, I/O (Disk, Network), Battery, …

Network Performance Response time, Throughput, … …

Page 121: Automated Historical Performance Analysis with kmemtracer

121 12th Kandroid Conference - www.kandroid.org

What to Measure?

Rendering (or GUI) Performance Frame Rate (FPS), Frame Time , Jankiness, Input Latency, …

Resource Consumption Memory, CPU, I/O (Disk, Network), Battery, …

Network Performance Response time, Throughput, … …

Page 122: Automated Historical Performance Analysis with kmemtracer

122 12th Kandroid Conference - www.kandroid.org

How to Measure Resource Consumption?

Page 123: Automated Historical Performance Analysis with kmemtracer

123 12th Kandroid Conference - www.kandroid.org

How to Measure Resource Consumption?

API cmds

Page 124: Automated Historical Performance Analysis with kmemtracer

124 12th Kandroid Conference - www.kandroid.org

How to Measure Resource Consumption?

API cmds

Public Internal

Page 125: Automated Historical Performance Analysis with kmemtracer

125 12th Kandroid Conference - www.kandroid.org

How to Measure Resource Consumption?

API cmds

Public Internal Android Linux

Page 126: Automated Historical Performance Analysis with kmemtracer

126 12th Kandroid Conference - www.kandroid.org

How to Measure Resource Consumption?

API cmds

Public Internal Android Linux

Easy Flexible

Page 127: Automated Historical Performance Analysis with kmemtracer

127 12th Kandroid Conference - www.kandroid.org

Measuring Memory Usage

Page 128: Automated Historical Performance Analysis with kmemtracer

128 12th Kandroid Conference - www.kandroid.org

Measuring Memory Usage

Public

android.app.ActivityManager.MemoryInfo android.os.Debug android.os.Debug.MemoryInfo

Page 129: Automated Historical Performance Analysis with kmemtracer

129 12th Kandroid Conference - www.kandroid.org

Measuring Memory Usage

Public Internal

android.app.ActivityManager.MemoryInfo android.os.Debug android.os.Debug.MemoryInfo

dalvik.system.VMDebug

Page 130: Automated Historical Performance Analysis with kmemtracer

130 12th Kandroid Conference - www.kandroid.org

Measuring Memory Usage

Public Internal Android

android.app.ActivityManager.MemoryInfo android.os.Debug android.os.Debug.MemoryInfo

adb shell dumpsys meminfo [pid|pname] adb shell dumpsys gfxinfo [pid|pname] adb shell procrank

dalvik.system.VMDebug

Page 131: Automated Historical Performance Analysis with kmemtracer

131 12th Kandroid Conference - www.kandroid.org

Measuring Memory Usage

Public Internal Android Linux

android.app.ActivityManager.MemoryInfo android.os.Debug android.os.Debug.MemoryInfo

adb shell dumpsys meminfo [pid|pname] adb shell dumpsys gfxinfo [pid|pname] adb shell procrank

adb shell cat /proc/meminfo adb shell cat /proc/<pid>/[s]maps adb shell cat /proc/vmstat adb shell top adb shell ps

dalvik.system.VMDebug

Page 132: Automated Historical Performance Analysis with kmemtracer

132 12th Kandroid Conference - www.kandroid.org

$ adb shell dumpsys meminfo com.example.android.apis Applications Memory Usage (kB): Uptime: 93065 Realtime: 93065 ** MEMINFO in pid 1259 [com.example.android.apis] ** native dalvik other total size: 3548 5831 N/A 9379 allocated: 3475 3335 N/A 6810 free: 48 2496 N/A 2544 (Pss): 699 382 4889 5970 (shared dirty): 1500 1876 8356 11732 (priv dirty): 628 40 2472 3140 ...

GB (API Level 10)

Measuring Memory Usage: dumpsys meminfo

Page 133: Automated Historical Performance Analysis with kmemtracer

133 12th Kandroid Conference - www.kandroid.org

$ adb shell dumpsys meminfo com.example.android.apis Applications Memory Usage (kB): Uptime: 93065 Realtime: 93065 ** MEMINFO in pid 1259 [com.example.android.apis] ** native dalvik other total size: 3548 5831 N/A 9379 allocated: 3475 3335 N/A 6810 free: 48 2496 N/A 2544 (Pss): 699 382 4889 5970 (shared dirty): 1500 1876 8356 11732 (priv dirty): 628 40 2472 3140 ...

GB (API Level 10)

Measuring Memory Usage: dumpsys meminfo

Page 134: Automated Historical Performance Analysis with kmemtracer

134 12th Kandroid Conference - www.kandroid.org

$ adb shell dumpsys meminfo com.example.android.apis ... ** MEMINFO in pid 1875 [com.example.android.apis] ** Shared Private Heap Heap Heap Pss Dirty Dirty Size Alloc Free ------ ------ ------ ------ ------ ------ Native 1377 1256 1300 12084 7332 567 Dalvik 5274 5476 4952 6260 5712 548 Stack 76 12 76 Cursor 0 0 0 Ashmem 0 0 0 Other dev 4 20 0 .so mmap 1526 4836 592 .jar mmap 0 0 0 .apk mmap 267 0 0 .ttf mmap 5 0 0 .dex mmap 1085 0 0 Other mmap 18 8 4 Unknown 824 0 824 TOTAL 10456 11608 7748 18344 13044 1115 ...

JB (API Level 18)

Measuring Memory Usage: dumpsys meminfo

Page 135: Automated Historical Performance Analysis with kmemtracer

135 12th Kandroid Conference - www.kandroid.org

$ adb shell dumpsys meminfo com.example.android.apis ... ** MEMINFO in pid 1875 [com.example.android.apis] ** Shared Private Heap Heap Heap Pss Dirty Dirty Size Alloc Free ------ ------ ------ ------ ------ ------ Native 1377 1256 1300 12084 7332 567 Dalvik 5274 5476 4952 6260 5712 548 Stack 76 12 76 Cursor 0 0 0 Ashmem 0 0 0 Other dev 4 20 0 .so mmap 1526 4836 592 .jar mmap 0 0 0 .apk mmap 267 0 0 .ttf mmap 5 0 0 .dex mmap 1085 0 0 Other mmap 18 8 4 Unknown 824 0 824 TOTAL 10456 11608 7748 18344 13044 1115 ...

JB (API Level 18)

Measuring Memory Usage: dumpsys meminfo

Page 136: Automated Historical Performance Analysis with kmemtracer

136 12th Kandroid Conference - www.kandroid.org

https://android.googlesource.com/platform/frameworks/base.git/+/0e3328fbdd3845b0e2bec364e951498eaee6b079

Page 137: Automated Historical Performance Analysis with kmemtracer

137 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys meminfo’ works?

adb shell dumpsys meminfo [-a] [-oom] [-process]

Page 138: Automated Historical Performance Analysis with kmemtracer

138 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys meminfo’ works?

com.android.server.am. ActivityManagerService

adb shell dumpsys meminfo [-a] [-oom] [-process]

Page 139: Automated Historical Performance Analysis with kmemtracer

139 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys meminfo’ works?

com.android.server.am. ActivityManagerService

android.os Debug

android.app.ActivityThread. ApplicationThread

adb shell dumpsys meminfo [-a] [-oom] [-process]

android.app.ActivityThread. ApplicationThread

android.app.ActivityThread. ApplicationThread

Page 140: Automated Historical Performance Analysis with kmemtracer

140 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys meminfo’ works?

com.android.server.am. ActivityManagerService

android.os Debug

android.app.ActivityThread. ApplicationThread

adb shell dumpsys meminfo [-a] [-oom] [-process]

android.app.ActivityThread. ApplicationThread

android.app.ActivityThread. ApplicationThread

Page 141: Automated Historical Performance Analysis with kmemtracer

141 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys meminfo’ works?

com.android.server.am. ActivityManagerService

android.os Debug

android.app.ActivityThread. ApplicationThread

/proc/<pid>/smaps

adb shell dumpsys meminfo [-a] [-oom] [-process]

mallinfo()

getNativeHeap*() getMemoryInfo()

android.app.ActivityThread. ApplicationThread

android.app.ActivityThread. ApplicationThread

Page 142: Automated Historical Performance Analysis with kmemtracer

142 12th Kandroid Conference - www.kandroid.org

android.app.ActivityThread.ApplicationThread.dumpMemInfo()

long nativeMax = Debug.getNativeHeapSize() / 1024; long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; long nativeFree = Debug.getNativeHeapFreeSize() / 1024; Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memInfo); Runtime runtime = Runtime.getRuntime(); long dalvikMax = runtime.totalMemory() / 1024; long dalvikFree = runtime.freeMemory() / 1024; long dalvikAllocated = dalvikMax - dalvikFree;

How ‘dumpsys meminfo’ works?

Page 143: Automated Historical Performance Analysis with kmemtracer

143 12th Kandroid Conference - www.kandroid.org

android.app.ActivityThread.ApplicationThread.dumpMemInfo()

long nativeMax = Debug.getNativeHeapSize() / 1024; long nativeAllocated = Debug.getNativeHeapAllocatedSize() / 1024; long nativeFree = Debug.getNativeHeapFreeSize() / 1024; Debug.MemoryInfo memInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memInfo); Runtime runtime = Runtime.getRuntime(); long dalvikMax = runtime.totalMemory() / 1024; long dalvikFree = runtime.freeMemory() / 1024; long dalvikAllocated = dalvikMax - dalvikFree;

struct mallinfo info = mallinfo(); (jlong) info.usmblks; // Debug.getNativeHeapSize() (jlong) info.uordblks; // Debug.getNativeHeapAllocatedSize() (jlong) info.fordblks; // Debug.getNativeHeapFreeSize()

android.os.Debug.getNativeHeap*()

How ‘dumpsys meminfo’ works?

Page 144: Automated Historical Performance Analysis with kmemtracer

144 12th Kandroid Conference - www.kandroid.org

struct mallinfo { MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ MALLINFO_FIELD_TYPE smblks; /* always 0 */ MALLINFO_FIELD_TYPE hblks; /* always 0 */ MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ MALLINFO_FIELD_TYPE fordblks; /* total free space */ MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ };

bionic\libc\upstream-dlmalloc\malloc.c

① Total allocated = uordblks ② Total free = fordblks ③ Footprint = uordblks + fordblks ④ Max footprint = usmblks

2

1

3

4

How ‘dumpsys meminfo’ works?

Page 145: Automated Historical Performance Analysis with kmemtracer

145 12th Kandroid Conference - www.kandroid.org

Measuring CPU Usage

Page 146: Automated Historical Performance Analysis with kmemtracer

146 12th Kandroid Conference - www.kandroid.org

Measuring CPU Usage

Public

android.os.Process android.os.Debug.InstructionCount

Page 147: Automated Historical Performance Analysis with kmemtracer

147 12th Kandroid Conference - www.kandroid.org

Measuring CPU Usage

Public Internal

android.os.Process android.os.Debug.InstructionCount

com.android.internal.os.ProcessStats

Page 148: Automated Historical Performance Analysis with kmemtracer

148 12th Kandroid Conference - www.kandroid.org

Measuring CPU Usage

Public Internal Android

android.os.Process android.os.Debug.InstructionCount

adb shell dumpsys cpuinfo

com.android.internal.os.ProcessStats

Page 149: Automated Historical Performance Analysis with kmemtracer

149 12th Kandroid Conference - www.kandroid.org

Measuring CPU Usage

Public Internal Android Linux

android.os.Process android.os.Debug.InstructionCount

adb shell dumpsys cpuinfo

adb shell cat /proc/stat adb shell cat /proc/<pid>/stat adb shell top

com.android.internal.os.ProcessStats

Page 150: Automated Historical Performance Analysis with kmemtracer

150 12th Kandroid Conference - www.kandroid.org

Measuring CPU Usage: dumpsys cpuinfo

$ adb shell dumpsys cpuinfo Load: 0.0 / 0.01 / 0.05 CPU usage from 27006ms to 5405ms ago: 1.1% 1421/com.android.launcher: 0.3% user + 0.8% kernel / faults: 2187 minor 2.4% 1225/system_server: 0.4% user + 1.9% kernel / faults: 2030 minor 0.1% 949/surfaceflinger: 0% user + 0.1% kernel / faults: 3 minor 0% 952/mediaserver: 0% user + 0% kernel / faults: 8 minor 0% 959/adbd: 0% user + 0% kernel / faults: 11 minor 0% 1295/com.android.systemui: 0% user + 0% kernel / faults: 5 minor +0% 1901/com.example.android.apis: 0% user + 0% kernel 56% TOTAL: 10% user + 45% kernel + 0.7% softirq

Page 151: Automated Historical Performance Analysis with kmemtracer

151 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys cpuinfo’ works?

adb shell dumpsys cpuinfo

Page 152: Automated Historical Performance Analysis with kmemtracer

152 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys cpuinfo’ works?

adb shell dumpsys cpuinfo

com.android.server.am. ActivityManagerService

Page 153: Automated Historical Performance Analysis with kmemtracer

153 12th Kandroid Conference - www.kandroid.org

com.android.internal.os ProcessStats

How ‘dumpsys cpuinfo’ works?

adb shell dumpsys cpuinfo

com.android.server.am. ActivityManagerService

Page 154: Automated Historical Performance Analysis with kmemtracer

154 12th Kandroid Conference - www.kandroid.org

com.android.internal.os ProcessStats

com.android.internal.os ProcessStats.Stats

com.android.internal.os ProcessStats.Stats

com.android.internal.os ProcessStats.Stats

How ‘dumpsys cpuinfo’ works?

adb shell dumpsys cpuinfo

com.android.server.am. ActivityManagerService

Page 155: Automated Historical Performance Analysis with kmemtracer

155 12th Kandroid Conference - www.kandroid.org

com.android.internal.os ProcessStats

com.android.internal.os ProcessStats.Stats

android.os Process

com.android.internal.os ProcessStats.Stats

com.android.internal.os ProcessStats.Stats

android_util_Process.cpp

How ‘dumpsys cpuinfo’ works?

adb shell dumpsys cpuinfo

/proc/stat /proc/loadavg /proc/<pid>/stat /proc/<ppid>/task/<pid>/stat

com.android.server.am. ActivityManagerService

Page 156: Automated Historical Performance Analysis with kmemtracer

156 12th Kandroid Conference - www.kandroid.org

How ‘dumpsys cpuinfo’ works?

procfs (or the proc filesystem) is a special filesystem in UNIX-

like operating systems that presents information about processes

and other system information in a hierarchical file-like structure,

providing a more convenient and standardized method for

dynamically accessing process data held in the kernel than

traditional tracing methods or direct access to kernel memory.

http://en.wikipedia.org/wiki/Procfs

Page 157: Automated Historical Performance Analysis with kmemtracer

157 12th Kandroid Conference - www.kandroid.org

http://manpages.courier-mta.org/htmlman5/proc.5.html

How ‘dumpsys cpuinfo’ works?

procfs (or the proc filesystem) is a special filesystem in UNIX-

like operating systems that presents information about processes

and other system information in a hierarchical file-like structure,

providing a more convenient and standardized method for

dynamically accessing process data held in the kernel than

traditional tracing methods or direct access to kernel memory.

http://en.wikipedia.org/wiki/Procfs

Page 158: Automated Historical Performance Analysis with kmemtracer

158 12th Kandroid Conference - www.kandroid.org

Measuring I/O Usage

Page 159: Automated Historical Performance Analysis with kmemtracer

159 12th Kandroid Conference - www.kandroid.org

Measuring I/O Usage

Public

android.net.TrafficStats

Page 160: Automated Historical Performance Analysis with kmemtracer

160 12th Kandroid Conference - www.kandroid.org

Measuring I/O Usage

Public Internal

android.net.TrafficStats

android.net.NetworkStats android.net.NetworkStatsHistory com.android.internal.net.NetworkStatsFactory

Page 161: Automated Historical Performance Analysis with kmemtracer

161 12th Kandroid Conference - www.kandroid.org

Measuring I/O Usage

Public Internal Android

android.net.TrafficStats

adb shell dumpsys netstats adb shell dumpsys diskstats

android.net.NetworkStats android.net.NetworkStatsHistory com.android.internal.net.NetworkStatsFactory

Page 162: Automated Historical Performance Analysis with kmemtracer

162 12th Kandroid Conference - www.kandroid.org

Measuring I/O Usage

Public Internal Android Linux

android.net.TrafficStats

adb shell dumpsys netstats adb shell dumpsys diskstats

/proc/net/*

android.net.NetworkStats android.net.NetworkStatsHistory com.android.internal.net.NetworkStatsFactory

Page 163: Automated Historical Performance Analysis with kmemtracer

163 12th Kandroid Conference - www.kandroid.org

Measuring Network IO Usage: dumpsys netstats

$ adb shell dumpsys netstats Active interfaces: iface=eth0 ident=[[type=MOBILE, subType=COMBINED, subscriberId=310260000000000]] Dev stats: Pending bytes: 43783 History since boot: ident=[[type=MOBILE_HIPRI, subType=COMBINED, subscriberId=310260000000000], [type=MOBILE, subType=COMBINED, subscriberId=310260000000000]] uid=-1 set=ALL tag=0x0 NetworkStatsHistory: bucketDuration=3600000 bucketStart=1382443200000 activeTime=4813 rxBytes=17410 rxPackets=203 txBytes=25579 txPackets=173 operations=0 ... Xt stats: Pending bytes: 37557 History since boot: ident=[[type=MOBILE_HIPRI, subType=COMBINED, subscriberId=310260000000000], [type=MOBILE, subType=COMBINED, subscriberId=310260000000000]] uid=-1 set=ALL tag=0x0 ... UID stats: ... UID tag stats: ...

Page 164: Automated Historical Performance Analysis with kmemtracer

164 12th Kandroid Conference - www.kandroid.org

adb shell dumpsys netstats [--full] [--uid] [--tag]

How ‘dumpsys netstats’ works?

Page 165: Automated Historical Performance Analysis with kmemtracer

165 12th Kandroid Conference - www.kandroid.org

com.android.server.net. NetworkStatsService

adb shell dumpsys netstats [--full] [--uid] [--tag]

How ‘dumpsys netstats’ works?

Page 166: Automated Historical Performance Analysis with kmemtracer

166 12th Kandroid Conference - www.kandroid.org

com.android.server.net. NetworkStatsService

com.android.server.net NetworkStatsRecorder

adb shell dumpsys netstats [--full] [--uid] [--tag]

How ‘dumpsys netstats’ works?

com.android.server.net NetworkStatsRecorder

com.android.server.net NetworkStatsRecorder

com.android.server.net NetworkStatsRecorder

Page 167: Automated Historical Performance Analysis with kmemtracer

167 12th Kandroid Conference - www.kandroid.org

com.android.server.net. NetworkStatsService

android.net NetworkStatsHistory

com.android.server.net NetworkStatsCollection

com.android.server.net NetworkStatsRecorder

adb shell dumpsys netstats [--full] [--uid] [--tag]

How ‘dumpsys netstats’ works?

com.android.server.net NetworkStatsRecorder

com.android.server.net NetworkStatsRecorder

com.android.server.net NetworkStatsRecorder

android.net NetworkStatsHistory

android.net NetworkStatsHistory

Page 168: Automated Historical Performance Analysis with kmemtracer

168 12th Kandroid Conference - www.kandroid.org

com.android.internal.net NetworkStatsFactory

com.android.server.net. NetworkStatsService

android.net NetworkStats

android.net NetworkStatsHistory

com.android.server.net NetworkStatsCollection

com.android.server.net NetworkStatsRecorder

adb shell dumpsys netstats [--full] [--uid] [--tag]

How ‘dumpsys netstats’ works?

com.android.server.net NetworkStatsRecorder

com.android.server.net NetworkStatsRecorder

com.android.server.net NetworkStatsRecorder

android.net NetworkStatsHistory

android.net NetworkStatsHistory

/proc/net/xt_qtaguid/iface_stat_all /proc/net/xt_qtaguid/iface_stat_fmt /proc/net/xt_qtaguid/stats

com.android.server NetworkManagementService

Page 169: Automated Historical Performance Analysis with kmemtracer

169 12th Kandroid Conference - www.kandroid.org

@Override public Config getDevConfig() { return new Config(getSecureLong(NETSTATS_DEV_BUCKET_DURATION, HOUR_IN_MILLIS), getSecureLong(NETSTATS_DEV_ROTATE_AGE, 15 * DAY_IN_MILLIS), getSecureLong(NETSTATS_DEV_DELETE_AGE, 90 * DAY_IN_MILLIS)); } @Override public Config getXtConfig() { return getDevConfig(); } @Override public Config getUidConfig() { return new Config(getSecureLong(NETSTATS_UID_BUCKET_DURATION, 2 * HOUR_IN_MILLIS), getSecureLong(NETSTATS_UID_ROTATE_AGE, 15 * DAY_IN_MILLIS), getSecureLong(NETSTATS_UID_DELETE_AGE, 90 * DAY_IN_MILLIS)); } @Override public Config getUidTagConfig() { return new Config(getSecureLong(NETSTATS_UID_TAG_BUCKET_DURATION, 2 * HOUR_IN_MILLIS), getSecureLong(NETSTATS_UID_TAG_ROTATE_AGE, 5 * DAY_IN_MILLIS), getSecureLong(NETSTATS_UID_TAG_DELETE_AGE, 15 * DAY_IN_MILLIS)); }

How ‘dumpsys netstats’ works?

com.android.server.net.NetworkStatsService.DefaultNetworkStatsSettings

Page 170: Automated Historical Performance Analysis with kmemtracer

170 12th Kandroid Conference - www.kandroid.org

$ adb shell cat /proc/net/xt_qtaguid/iface_stat_all eth0 1 0 0 0 0 220609 3228 2891157 2995

$ adb shell cat /proc/net/xt_qtaguid/iface_stat_fmt ifname total_skb_rx_bytes total_skb_rx_packets total_skb_tx_bytes total_skb_tx_packets eth0 163781 3231 2846738 2994

Dev: IP + Non IP layer traffic

Xt: Only IP layer traffic

$ adb shell cat /proc/net/xt_qtaguid/stats idx iface acct_tag_hex uid_tag_int cnt_set rx_bytes rx_packets tx_bytes tx_packets rx_tcp_bytes rx_tcp_packets rx_udp_bytes rx_udp_packets rx_other_bytes rx_other_packets tx_tcp_bytes tx_tcp_packets tx_udp_bytes tx_udp_packets tx_other_bytes tx_other_packets 2 eth0 0x0 0 0 163335 3226 2846228 2988 161473 3222 1862 4 0 0 2845850 2982 266 4 112 2 3 eth0 0x0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 eth0 0x0 1000 0 819 11 965 12 667 9 152 2 0 0 813 10 152 2 0 0 5 eth0 0x0 1000 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Uid: UID level details

How ‘dumpsys netstats’ works?

Page 171: Automated Historical Performance Analysis with kmemtracer

171 12th Kandroid Conference - www.kandroid.org

Measuring Battery Usage

Page 172: Automated Historical Performance Analysis with kmemtracer

172 12th Kandroid Conference - www.kandroid.org

Measuring Battery Usage

Public

android.os.BatteryManager

Page 173: Automated Historical Performance Analysis with kmemtracer

173 12th Kandroid Conference - www.kandroid.org

Measuring Battery Usage

Public Internal

android.os.BatteryManager

android.os.BatteryStats com.android.internal.os.BatteryStatsImpl com.android.internal.os.PowerProfile com.android.server.os.BatteryStatsService

Page 174: Automated Historical Performance Analysis with kmemtracer

174 12th Kandroid Conference - www.kandroid.org

Measuring Battery Usage

Public Internal Android

android.os.BatteryManager

adb shell dumpsys battery adb shell dumpsys batteryinfo

android.os.BatteryStats com.android.internal.os.BatteryStatsImpl com.android.internal.os.PowerProfile com.android.server.os.BatteryStatsService

Page 175: Automated Historical Performance Analysis with kmemtracer

175 12th Kandroid Conference - www.kandroid.org

Measuring Battery Usage

Public Internal Android Linux

android.os.BatteryManager

adb shell dumpsys battery adb shell dumpsys batteryinfo

None (?)

android.os.BatteryStats com.android.internal.os.BatteryStatsImpl com.android.internal.os.PowerProfile com.android.server.os.BatteryStatsService

Page 176: Automated Historical Performance Analysis with kmemtracer

176 12th Kandroid Conference - www.kandroid.org

$ adb shell dumpsys batteryinfo Battery History: -1d00h43m02s057ms START -1d00h40m00s730ms 050 20080002 status=charging health=good plug=ac temp=0 volt=0 +plugged +sensor brightness=medium -1d00h39m57s332ms 050 60080002 +wake_lock -1d00h39m57s202ms 050 60080302 phone_state=off ... Per-PID Stats: PID 1227 wake time: +3s445ms PID 1227 wake time: +199ms ... Statistics since last charge: System starts: 29, currently on battery: false Time on battery: 0ms (0.0%) realtime, 0ms (0.0%) uptime Total run time: 1d 2h 1m 3s 606ms realtime, 1d 2h 1m 3s 603ms uptime, ... #1000: User activity: 26 other, 27 button, 93 touch Sensor 0: (not used) Apk com.android.keychain: (nothing executed) ... Statistics since last unplugged: Time on battery: 0ms (0.0%) realtime, 0ms (0.0%) uptime ...

Measuring Battery Usage: dumpsys batteryinfo

Page 177: Automated Historical Performance Analysis with kmemtracer

177 12th Kandroid Conference - www.kandroid.org

adb shell dumpsys batteryinfo

How ‘dumpsys batteryinfo’ works?

Page 178: Automated Historical Performance Analysis with kmemtracer

178 12th Kandroid Conference - www.kandroid.org

com.android.server.am. BatteryStatsService

adb shell dumpsys batteryinfo

How ‘dumpsys batteryinfo’ works?

Page 179: Automated Historical Performance Analysis with kmemtracer

179 12th Kandroid Conference - www.kandroid.org

com.android.internal.os BatteryStatsImpl

com.android.server.am. BatteryStatsService

adb shell dumpsys batteryinfo

How ‘dumpsys batteryinfo’ works?

Page 180: Automated Historical Performance Analysis with kmemtracer

180 12th Kandroid Conference - www.kandroid.org

com.android.internal.os BatteryStatsImpl

android.os BatteryStats

com.android.server.am. BatteryStatsService

adb shell dumpsys batteryinfo

How ‘dumpsys batteryinfo’ works?

Page 181: Automated Historical Performance Analysis with kmemtracer

181 12th Kandroid Conference - www.kandroid.org

com.android.internal.os BatteryStatsImpl

android.os BatteryStats

com.android.server.am. BatteryStatsService

adb shell dumpsys batteryinfo

How ‘dumpsys batteryinfo’ works?

noteBluetoothOff() noteBluetoothOn() noteFullWifiLockAcquired(int) noteFullWifiLockAcquiredFromSource(WorkSource) noteFullWifiLockReleased(int) noteFullWifiLockReleasedFromSource(WorkSource) noteInputEvent() noteNetworkInterfaceType(String, int) notePhoneDataConnectionState(int, boolean) notePhoneOff() notePhoneOn() notePhoneSignalStrength(SignalStrength) notePhoneState(int) noteScreenBrightness(int) noteScreenOff() noteScreenOn() ... noteWifiScanStarted(int) noteWifiScanStartedFromSource(WorkSource) noteWifiScanStopped(int) noteWifiScanStoppedFromSource(WorkSource) noteWifiStopped(WorkSource)

Page 182: Automated Historical Performance Analysis with kmemtracer

182 12th Kandroid Conference - www.kandroid.org

If you can’t automate the measurement of it,

it is difficult to improve it.

Page 183: Automated Historical Performance Analysis with kmemtracer

183 12th Kandroid Conference - www.kandroid.org

If you can’t automate the measurement of it,

it is difficult to improve it.

If you can’t understand the measurement results,

it is impossible to improve it.

Page 184: Automated Historical Performance Analysis with kmemtracer