From 13926c385c8ca606d9acd8a969a73dc29edd7413 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Mon, 9 Sep 2019 15:03:37 -0700 Subject: [PATCH 1/2] Add additional system_server benchmarks This CL adds benchmarks for the following PackageManager APIs: * getInstalledApplications * getInstalledPackages * getPackageInfo * getApplicationInfo * getResourcesForApplication * getActivityInfo Bug: 140743821 Change-Id: I467e4264a5e8f5c1c34194c7ad4b4c415387da9e --- .../src/SystemServerBenchmarkActivity.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/startop/apps/test/src/SystemServerBenchmarkActivity.java b/startop/apps/test/src/SystemServerBenchmarkActivity.java index 59a30a540d999..c370924c943ca 100644 --- a/startop/apps/test/src/SystemServerBenchmarkActivity.java +++ b/startop/apps/test/src/SystemServerBenchmarkActivity.java @@ -17,8 +17,11 @@ package com.android.startop.test; import android.app.Activity; +import android.content.ComponentName; import android.content.Context; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; import android.os.AsyncTask; import android.os.Bundle; import android.os.Trace; @@ -108,5 +111,48 @@ public class SystemServerBenchmarkActivity extends Activity { new Benchmark(benchmarkList, "getInstalledApplications", () -> { pm.getInstalledApplications(PackageManager.MATCH_SYSTEM_ONLY); }); + + new Benchmark(benchmarkList, "getInstalledPackages", () -> { + pm.getInstalledPackages(PackageManager.GET_ACTIVITIES); + }); + + new Benchmark(benchmarkList, "getPackageInfo", () -> { + try { + pm.getPackageInfo("com.android.startop.test", 0); + } catch (NameNotFoundException e) { + throw new RuntimeException(e); + } + }); + + new Benchmark(benchmarkList, "getApplicationInfo", () -> { + try { + pm.getApplicationInfo("com.android.startop.test", 0); + } catch (NameNotFoundException e) { + throw new RuntimeException(e); + } + }); + + try { + ApplicationInfo app = pm.getApplicationInfo("com.android.startop.test", 0); + new Benchmark(benchmarkList, "getResourcesForApplication", () -> { + try { + pm.getResourcesForApplication(app); + } catch (NameNotFoundException e) { + throw new RuntimeException(e); + } + }); + } catch (NameNotFoundException e) { + throw new RuntimeException(e); + } + + ComponentName component = new ComponentName(this, this.getClass()); + new Benchmark(benchmarkList, "getActivityInfo", () -> { + try { + pm.getActivityInfo(component, PackageManager.GET_META_DATA); + } catch (NameNotFoundException e) { + throw new RuntimeException(e); + } + }); + } } From 4777ee062110421089b8eb8bd64b383937cf1739 Mon Sep 17 00:00:00 2001 From: Eric Holk Date: Tue, 10 Sep 2019 14:22:12 -0700 Subject: [PATCH 2/2] Run system_server benchmarks for 5 seconds Especially for short running methods, this gives us better results and also gives us more data for things like simpleperf to work on. Since we don't know ahead of time how many times each benchmark will run, we had to switch the mean and standard deviation formulas to one that supports running updates. This has the advantage of making it constant space. Bug: 140743821 Change-Id: I3e6b6b44d3bd573bad8afc8831226194147b108a --- .../src/SystemServerBenchmarkActivity.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/startop/apps/test/src/SystemServerBenchmarkActivity.java b/startop/apps/test/src/SystemServerBenchmarkActivity.java index c370924c943ca..61d43226c41ea 100644 --- a/startop/apps/test/src/SystemServerBenchmarkActivity.java +++ b/startop/apps/test/src/SystemServerBenchmarkActivity.java @@ -35,7 +35,8 @@ import android.widget.TextView; import java.util.Arrays; class Benchmark { - public static final int NUM_ITERATIONS = 1000; + // Time limit to run benchmarks in seconds + public static final int TIME_LIMIT = 5; public Benchmark(ViewGroup parent, CharSequence name, Runnable thunk) { Context context = parent.getContext(); @@ -57,28 +58,25 @@ class Benchmark { @Override protected Object doInBackground(Object... _args) { - long[] results = new long[NUM_ITERATIONS]; + long startTime = System.nanoTime(); + int count = 0; // Run benchmark - for (int i = 0; i < results.length; i++) { - results[i] = -System.nanoTime(); + while (true) { + long elapsed = -System.nanoTime(); thunk.run(); - results[i] += System.nanoTime(); + elapsed += System.nanoTime(); + + count++; + double elapsedVariance = (double) elapsed - resultMean; + resultMean += elapsedVariance / count; + resultStdev += elapsedVariance * ((double) elapsed - resultMean); + + if (System.nanoTime() - startTime > TIME_LIMIT * 1e9) { + break; + } } - - // Compute mean - long sum = Arrays.stream(results).sum(); - resultMean = (double) sum / results.length; - - // Compute standard deviation - double variance = 0; - for (long i : results) { - double t = (double) i - resultMean; - variance += t * t; - } - variance /= results.length - 1; - - resultStdev = Math.sqrt(variance); + resultStdev = Math.sqrt(resultStdev / (count - 1)); return null; }