README.md
1 # Examples of using simpleperf to profile Android applications
2
3 ## Table of Contents
4
5 - [Introduction](#introduction)
6 - [Profiling Java application](#profiling-java-application)
7 - [Profiling Java/C++ application](#profiling-javac-application)
8 - [Profiling Kotlin application](#profiling-kotlin-application)
9
10 ## Introduction
11
12 Simpleperf is a native profiler used on Android platform. It can be used to profile Android
13 applications. Its documentation is [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md).
14 Instructions of preparing your Android application for profiling are [here](https://android.googlesource.com/platform/system/extras/+/master/simpleperf/doc/README.md#Android-application-profiling).
15 This directory is to show examples of using simpleperf to profile Android applications. The
16 meaning of each directory is as below:
17
18 ../scripts/ -- contain simpleperf binaries and scripts.
19 SimpleperfExamplePureJava/ -- contains an Android Studio project using only Java code.
20 SimpleperfExampleWithNative/ -- contains an Android Studio project using both Java and C++ code.
21 SimpleperfExampleOfKotlin/ -- contains an Android Studio project using Kotlin code.
22
23 It can be downloaded as below:
24
25 ```sh
26 $ git clone https://android.googlesource.com/platform/system/extras
27 $ cd extras/simpleperf/demo
28 ```
29
30 The testing environment:
31
32 ```
33 Android Studio 3.0
34 test device: Android O (Google Pixel 2)
35 test device: Android N (Google Nexus 6P)
36 Please make sure your device having Android version >= N.
37 ```
38
39 ## Profile a Java application
40
41 Android Studio project: SimpleExamplePureJava
42
43 steps:
44 1. Build and install the application:
45
46 ```sh
47 # Open SimpleperfExamplesPureJava project with Android Studio,
48 # and build this project successfully, otherwise the `./gradlew` command below will fail.
49 $ cd SimpleperfExamplePureJava
50
51 # On windows, use "gradlew" instead.
52 $ ./gradlew clean assemble
53 $ adb install -r app/build/outputs/apk/app-profiling.apk
54 ```
55
56 2. Record profiling data:
57
58 ```sh
59 $ cd ../../scripts/
60 # app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/.
61 $ python app_profiler.py -p com.example.simpleperf.simpleperfexamplepurejava
62 ```
63
64 3. Show profiling data:
65
66 ```sh
67 # report_html.py generates profiling result in report.html.
68 $ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly
69 ```
70
71 ## Profile a Java/C++ application
72
73 Android Studio project: SimpleExampleWithNative
74
75 steps:
76 1. Build and install the application:
77
78 ```sh
79 # Open SimpleperfExamplesWithNative project with Android Studio,
80 # and build this project sucessfully, otherwise the `./gradlew` command below will fail.
81 $ cd SimpleperfExampleWithNative
82
83 # On windows, use "gradlew" instead.
84 $ ./gradlew clean assemble
85 $ adb install -r app/build/outputs/apk/profiling/app-profiling.apk
86 ```
87
88 2. Record profiling data:
89
90 ```sh
91 $ cd ../../scripts/
92 # app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/.
93 $ python app_profiler.py -p com.example.simpleperf.simpleperfexamplewithnative
94 ```
95
96 3. Show profiling data:
97
98 ```sh
99 # report_html.py generates profiling result in report.html.
100 $ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly
101 ```
102
103 ## Profile a Kotlin application
104
105 Android Studio project: SimpleExampleOfKotlin
106
107 steps:
108 1. Build and install the application:
109
110 ```sh
111 # Open SimpleperfExamplesOfKotlin project with Android Studio,
112 # and build this project sucessfully, otherwise the `./gradlew` command below will fail.
113 $ cd SimpleperfExampleOfKotlin
114
115 # On windows, use "gradlew" instead.
116 $ ./gradlew clean assemble
117 $ adb install -r app/build/outputs/apk/profiling/app-profiling.apk
118 ```
119
120 2. Record profiling data:
121
122 ```sh
123 $ cd ../../scripts/
124 # app_profiler.py collects profiling data in perf.data, and binaries on device in binary_cache/.
125 $ python app_profiler.py -p com.example.simpleperf.simpleperfexampleofkotlin
126 ```
127
128 3. Show profiling data:
129
130 ```sh
131 # report_html.py generates profiling result in report.html.
132 $ python report_html.py --add_source_code --source_dirs ../demo --add_disassembly
133 ```
134