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 android.app.Activity;
20 import android.content.Context;
21 import android.graphics.Typeface;
22 import android.os.Bundle;
23 import android.widget.Button;
24 import android.widget.GridLayout;
25 import android.widget.TextView;
26 
27 public class InteractiveMicrobenchmarkActivity extends Activity implements BenchmarkRunner {
28     protected GridLayout mBenchmarkList;
29 
onCreate(Bundle savedInstanceState)30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.system_server_benchmark_page);
33 
34         mBenchmarkList = findViewById(R.id.benchmark_list);
35 
36         addBenchmark("Empty", () -> {
37         });
38         addHeader("Application Benchmarks");
39         ApplicationBenchmarks.initializeBenchmarks(this, this);
40         addHeader("CPU Intensive Benchmarks");
41         CPUIntensiveBenchmarks.initializeBenchmarks(this, this);
42         addHeader("Init Check Overhead Benchmarks");
43         InitCheckOverheadBenchmarks.initializeBenchmarks(this, this);
44         addHeader("System Server Benchmarks");
45         SystemServerBenchmarks.initializeBenchmarks(this, this);
46     }
47 
48     /**
49      * Add a heading for a group of related benchmarks
50      *
51      * @param name The name of this group of benchmarks
52      */
addHeader(CharSequence name)53     public void addHeader(CharSequence name) {
54         Context context = mBenchmarkList.getContext();
55         TextView header = new TextView(context);
56         header.setText(name);
57         header.setTypeface(header.getTypeface(), Typeface.BOLD);
58         GridLayout.LayoutParams params = new GridLayout.LayoutParams();
59         params.columnSpec = GridLayout.spec(0, 3);
60         mBenchmarkList.addView(header, params);
61     }
62 
63     /**
64      * Adds a benchmark to the set to run.
65      *
66      * @param name A short name that shows up in the UI or benchmark results
67      */
addBenchmark(CharSequence name, Runnable thunk)68     public void addBenchmark(CharSequence name, Runnable thunk) {
69         Context context = mBenchmarkList.getContext();
70         Button button = new Button(context);
71         TextView mean = new TextView(context);
72         TextView stdev = new TextView(context);
73 
74         button.setText(name);
75         mean.setText("");
76         stdev.setText("");
77 
78         button.setOnClickListener((_button) -> {
79             mean.setText("Running...");
80             stdev.setText("");
81 
82             SystemServerBenchmarks.runBenchmarkInBackground(thunk, (resultMean, resultStdev) -> {
83                 mean.setText(String.format("%.3f", resultMean / 1e6));
84                 stdev.setText(String.format("%.3f", resultStdev / 1e6));
85             });
86         });
87 
88         mBenchmarkList.addView(button);
89         mBenchmarkList.addView(mean);
90         mBenchmarkList.addView(stdev);
91     }
92 }
93