64
12 th Kandroid Conference, 2013 Automated Historical Performance Analysis with kmemtracer 이경민 (Kyungmin Lee) [email protected] LG전자 “4 Featured Sessions” - Kandroid S/W Fundamentals Study Group 1. Terminology and History of Android Performance Features 2. Automated Historical Performance Analysis with kmemtracer 3. High Performance Apps Development with RenderScript 4. Texture Caching Implementation for Hardware Accelerated 2D Rendering

Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

  • Upload
    dotruc

  • View
    225

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

12th Kandroid

Conference, 2013

Automated Historical

Performance Analysis with kmemtracer

이경민 (Kyungmin Lee)

[email protected]

LG전자

“4 Featured Sessions” - Kandroid S/W Fundamentals Study Group

1. Terminology and History of Android Performance Features

2. Automated Historical Performance Analysis with kmemtracer

3. High Performance Apps Development with RenderScript

4. Texture Caching Implementation for Hardware Accelerated 2D Rendering

Page 2: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

2 12th Kandroid Conference, 2013

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 · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

3 12th Kandroid Conference, 2013

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 4: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

4 12th Kandroid Conference, 2013

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

Platform Versions

Screen Sizes and Densities

Diversity or Fragmentation

Page 5: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

5 12th Kandroid Conference, 2013

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

Page 6: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

6 12th Kandroid Conference, 2013

Why Historical Analysis?

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

Activity Navigation & Lifecycle

Historical Performance Analysis

Page 7: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

7 12th Kandroid Conference, 2013

How to Track Activity Navigation & Lifecycle?

Override onCreate, onStart, … of your Activity classes

Modify android.app.Activity in the framework

Is there a way that does not require the modification of

an application or framework?

Instrumentation

Page 8: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

8 12th Kandroid Conference, 2013

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 9: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

12th Kandroid

Conference, 2013

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 10: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

10 12th Kandroid Conference, 2013

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 11: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

11 12th Kandroid Conference, 2013

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 12: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

12 12th Kandroid Conference, 2013

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 13: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

13 12th Kandroid Conference, 2013

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 14: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

14 12th Kandroid Conference, 2013

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 15: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

15 12th Kandroid Conference, 2013

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 16: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

16 12th Kandroid Conference, 2013

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 17: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

17 12th Kandroid Conference, 2013

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 18: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

18 12th Kandroid Conference, 2013

How to Use kmemtracer?

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

Page 19: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

12th Kandroid

Conference, 2013

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 20: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

20 12th Kandroid Conference, 2013

Automate Controlling UI

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 · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

21 12th Kandroid Conference, 2013

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 22: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

22 12th Kandroid Conference, 2013

Click

Case Study: ApiDemos

Scroll down

Click Click

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

Page 23: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

23 12th Kandroid Conference, 2013

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 24: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

24 12th Kandroid Conference, 2013

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 25: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

25 12th Kandroid Conference, 2013

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 26: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

26 12th Kandroid Conference, 2013

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 27: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

27 12th Kandroid Conference, 2013

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 28: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

28 12th Kandroid Conference, 2013

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 29: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

29 12th Kandroid Conference, 2013

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 30: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

30 12th Kandroid Conference, 2013

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 31: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

31 12th Kandroid Conference, 2013

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 32: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

32 12th Kandroid Conference, 2013

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 33: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

33 12th Kandroid Conference, 2013

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 34: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

34 12th Kandroid Conference, 2013

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 35: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

35 12th Kandroid Conference, 2013

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 36: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

36 12th Kandroid Conference, 2013

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 37: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

37 12th Kandroid Conference, 2013

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 38: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

38 12th Kandroid Conference, 2013

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 39: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

39 12th Kandroid Conference, 2013

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 40: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

40 12th Kandroid Conference, 2013

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 41: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

12th Kandroid

Conference, 2013

Why Automated Historical Performance Testing?

kmemtracer: What and How?

Enhancement I : Automating UI Interaction

Enhancement II: Adding More Performance Metrics

Contents

Page 42: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

42 12th Kandroid Conference, 2013

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 43: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

43 12th Kandroid Conference, 2013

How to Measure Resource Consumption?

API cmds

Public Internal Android Linux

Easy Flexible

Page 44: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

44 12th Kandroid Conference, 2013

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 45: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

45 12th Kandroid Conference, 2013

$ 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 46: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

46 12th Kandroid Conference, 2013

$ 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 47: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

47 12th Kandroid Conference, 2013

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

Page 48: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

48 12th Kandroid Conference, 2013

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 49: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

49 12th Kandroid Conference, 2013

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 50: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

50 12th Kandroid Conference, 2013

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 51: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

51 12th Kandroid Conference, 2013

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 52: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

52 12th Kandroid Conference, 2013

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 53: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

53 12th Kandroid Conference, 2013

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 54: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

54 12th Kandroid Conference, 2013

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 55: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

55 12th Kandroid Conference, 2013

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 56: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

56 12th Kandroid Conference, 2013

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 57: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

57 12th Kandroid Conference, 2013

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 58: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

58 12th Kandroid Conference, 2013

@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 59: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

59 12th Kandroid Conference, 2013

$ 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 60: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

60 12th Kandroid Conference, 2013

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 61: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

61 12th Kandroid Conference, 2013

$ 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 62: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

62 12th Kandroid Conference, 2013

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 63: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test

63 12th Kandroid Conference, 2013

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 64: Automated Historical Performance Analysis with kmemtracer · ... android=" ... getInstrumentation().runOnMainSync ... > com.example.android.apis.robotium.test/\ > android.test