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 /**
18  *  A threaded CPU intensive class for use in benchmarks.
19  */
20 
21 package com.android.startop.test;
22 
23 import android.app.Activity;
24 
25 public class CPUIntensiveBenchmarks {
26     public static final int ARRAY_SIZE = 30000;
27     public static int[][] mArray;
28 
29     static class WorkerThread extends Thread {
30         int mThreadNumber;
WorkerThread(int number)31         WorkerThread(int number) {
32             mThreadNumber = number;
33         }
run()34         public void run() {
35             final int arrayLength = mArray[mThreadNumber].length;
36             for (int i = 0; i < arrayLength; ++i) {
37                 mArray[mThreadNumber][i] = i * i;
38             }
39             for (int i = 0; i < arrayLength; ++i) {
40                 for (int j = 0; j < arrayLength; ++j) {
41                     int swap = mArray[mThreadNumber][j];
42                     mArray[mThreadNumber][j] = mArray[mThreadNumber][(j + i) % arrayLength];
43                     mArray[mThreadNumber][(j + i) % arrayLength] = swap;
44                 }
45             }
46         }
47     };
48 
doSomeWork(int threadCount)49     static void doSomeWork(int threadCount) {
50         mArray = new int[threadCount][ARRAY_SIZE];
51         WorkerThread[] threads = new WorkerThread[threadCount];
52         // Create the threads.
53         for (int i = 0; i < threadCount; ++i) {
54             threads[i] = new WorkerThread(i);
55         }
56         // Start the threads.
57         for (int i = 0; i < threadCount; ++i) {
58             threads[i].start();
59         }
60         // Join the threads.
61         for (int i = 0; i < threadCount; ++i) {
62             try {
63                 threads[i].join();
64             } catch (Exception ex) {
65             }
66         }
67     }
68 
69     // Time limit to run benchmarks in seconds
70     public static final int TIME_LIMIT = 5;
71 
initializeBenchmarks(Activity parent, BenchmarkRunner benchmarks)72     static void initializeBenchmarks(Activity parent, BenchmarkRunner benchmarks) {
73         benchmarks.addBenchmark("Use 1 thread", () -> {
74             doSomeWork(1);
75         });
76         benchmarks.addBenchmark("Use 2 threads", () -> {
77             doSomeWork(2);
78         });
79         benchmarks.addBenchmark("Use 4 threads", () -> {
80             doSomeWork(4);
81         });
82         benchmarks.addBenchmark("Use 8 threads", () -> {
83             doSomeWork(8);
84         });
85         benchmarks.addBenchmark("Use 16 threads", () -> {
86             doSomeWork(16);
87         });
88     }
89 }
90