1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.startop.test;
18 
19 import java.io.File;
20 import java.io.FileNotFoundException;
21 import java.io.PrintStream;
22 import java.util.ArrayList;
23 
24 import android.app.Activity;
25 import android.app.ActivityManager;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.pm.ApplicationInfo;
30 import android.content.pm.PackageManager;
31 import android.content.pm.PackageManager.NameNotFoundException;
32 import android.os.AsyncTask;
33 import android.os.Bundle;
34 import android.view.ViewGroup;
35 import android.widget.Button;
36 import android.widget.GridLayout;
37 import android.widget.TextView;
38 
39 public class NonInteractiveMicrobenchmarkActivity extends Activity {
40     ArrayList<CharSequence> benchmarkNames = new ArrayList();
41     ArrayList<Runnable> benchmarkThunks = new ArrayList();
42 
43     PrintStream out;
44 
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47 
48         SystemServerBenchmarks.initializeBenchmarks(this, (name, thunk) -> {
49             benchmarkNames.add(name);
50             benchmarkThunks.add(thunk);
51         });
52 
53         try {
54             out = new PrintStream(new File(getExternalFilesDir(null), "benchmark.csv"));
55         } catch (FileNotFoundException e) {
56             throw new RuntimeException(e);
57         }
58         out.println("Name,Mean,Stdev");
59         runBenchmarks(0);
60     }
61 
runBenchmarks(int i)62     void runBenchmarks(int i) {
63         if (i < benchmarkNames.size()) {
64             SystemServerBenchmarks.runBenchmarkInBackground(benchmarkThunks.get(i),
65                     (mean, stdev) -> {
66                         out.printf("%s,%.0f,%.0f\n", benchmarkNames.get(i), mean, stdev);
67                         runBenchmarks(i + 1);
68                     });
69         }
70     }
71 }
72