Testing launch performance can be tricky, but it doesn’t have to be

Shell Command for Testing Startup

$ for i in `seq 1 100`
> do 
>   adb shell am force-stop com.android.samples.mytest
>   sleep 1
>   adb shell am start-activity -W -n com.android.samples.mytest/.MainActivity | grep "TotalTime" | cut -d ' ' -f 2
> done

Testing startup performance is… not obvious

ActivityTaskManager startup log

ActivityTaskManager: Displayed com.android.samples.mytest/.MainActivity: +1s380ms
2020-11-18 15:44:02.171 1279-1336/system_process I/ActivityTaskManager: Fully drawn com.android.samples.mytest/.MainActivity: +2s384ms

Automating startup

Insanity is doing the same thing over and over and expecting different results.
– Albert Einstein

Insanity is doing the same thing only once and expecting the results to be definitive.

– Not Albert Einstein

$ adb shell am start-activity -W -n com.android.samples.mytest/.MainActivity
Starting: Intent { cmp=com.android.samples.mytest/.MainActivity }
Status: ok
LaunchState: COLD
Activity: com.android.samples.mytest/.MainActivity
TotalTime: 1380
WaitTime: 1381
Complete
ActivityTaskManager: Displayed com.android.samples.mytest/.MainActivity: +1s380ms
adb shell am start-activity -W -n com.android.samples.mytest/.MainActivity | grep "TotalTime" | cut -d ' ' -f 2
$ [start-activity command as above...]
1380

 

Startup Is a Dish Best Served Cold

adb shell am force-stop com.android.samples.mytest

I Like to Loop It, Loop It

$ for i in `seq 1 100`
> do 
>   adb shell am force-stop com.android.samples.mytest
>   sleep 1
>   adb shell am start-activity -W -n com.android.samples.mytest/.MainActivity | grep "TotalTime" | cut -d ' ' -f 2
> done
$ adb shell am start-activity -S -W -R 100-n com.android.samples.mytest/.MainActivity | grep "TotalTime" | cut -d ' ' -f 2

Lock Your Clocks… When Possible

classpath "androidx.benchmark:benchmark-gradle-plugin:1.0.0"
apply plugin: androidx.benchmark
$ ./gradlew lockClocks
Locked CPUs 4,5,6,7 to 1267200 / 2457600 KHz
Disabled CPUs 0,1,2,3
$ ./gradlew unlockClocks

And… Done!

$ for i in `seq 1 100`
> do 
>   adb shell am force-stop com.android.samples.mytest
>   sleep 1
>   adb shell am start-activity -W -n com.android.samples.mytest/.MainActivity | grep "TotalTime" | cut -d ' ' -f 2
> done

Source: Medium by Chet Haase

Previous Amazon Launches IP Accelerator In Europe To Help Small Businesses Protect Their Brands And Tackle Counterfeit
Next Upgrade A K3s Kubernetes Cluster With System Upgrade Controller