Merge changes I3e6b6b44,I467e4264

* changes:
  Run system_server benchmarks for 5 seconds
  Add additional system_server benchmarks
This commit is contained in:
TreeHugger Robot
2019-09-11 01:20:40 +00:00
committed by Android (Google) Code Review

View File

@@ -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;
@@ -32,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();
@@ -54,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;
}
@@ -108,5 +109,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);
}
});
}
}