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 test of initialization check costs for AOT. 19 */ 20 21 package com.android.startop.test; 22 23 import android.app.Activity; 24 25 import java.util.Random; 26 27 public class InitCheckOverheadBenchmarks { 28 public static int mSum; 29 public static int mSum2; 30 public static int mStep; 31 public static int mStep2; 32 public static int mStartingValue; 33 34 static { 35 Random random = new Random(); 36 mStep = random.nextInt(); 37 mStep2 = random.nextInt(); 38 mStartingValue = random.nextInt(); 39 }; 40 41 static class OtherClass { 42 public static int mStep; 43 public static int mStep2; 44 public static int mStartingValue; 45 static { 46 Random random = new Random(); 47 mStep = random.nextInt(); 48 mStep2 = random.nextInt(); 49 mStartingValue = random.nextInt(); 50 }; 51 }; 52 localStaticFor(int iterationCount)53 public static void localStaticFor(int iterationCount) { 54 for (int i = 0; i < iterationCount; ++i) { 55 mSum += mStep; 56 } 57 } 58 nonLocalStaticFor(int iterationCount)59 public static void nonLocalStaticFor(int iterationCount) { 60 mSum = OtherClass.mStartingValue; 61 for (int i = 0; i < iterationCount; ++i) { 62 mSum += OtherClass.mStep; 63 } 64 } 65 localStaticForTwo(int iterationCount)66 public static void localStaticForTwo(int iterationCount) { 67 for (int i = 0; i < iterationCount; ++i) { 68 mSum += mStep; 69 mSum2 += mStep2; 70 } 71 } 72 nonLocalStaticForTwo(int iterationCount)73 public static void nonLocalStaticForTwo(int iterationCount) { 74 mSum = OtherClass.mStartingValue; 75 for (int i = 0; i < iterationCount; ++i) { 76 mSum += OtherClass.mStep; 77 mSum2 += OtherClass.mStep2; 78 } 79 } 80 localStaticDoWhile(int iterationCount)81 public static void localStaticDoWhile(int iterationCount) { 82 int i = 0; 83 do { 84 mSum += mStep; 85 ++i; 86 } while (i < iterationCount); 87 } 88 nonLocalStaticDoWhile(int iterationCount)89 public static void nonLocalStaticDoWhile(int iterationCount) { 90 mSum = OtherClass.mStartingValue; 91 int i = 0; 92 do { 93 mSum += OtherClass.mStep; 94 ++i; 95 } while (i < iterationCount); 96 } 97 doGC()98 public static void doGC() { 99 Runtime.getRuntime().gc(); 100 } 101 102 // Time limit to run benchmarks in seconds 103 public static final int TIME_LIMIT = 5; 104 initializeBenchmarks(Activity parent, BenchmarkRunner benchmarks)105 static void initializeBenchmarks(Activity parent, BenchmarkRunner benchmarks) { 106 benchmarks.addBenchmark("GC", () -> { 107 doGC(); 108 }); 109 110 benchmarks.addBenchmark("InitCheckFor (local)", () -> { 111 localStaticFor(10000000); 112 }); 113 114 benchmarks.addBenchmark("InitCheckFor (non-local)", () -> { 115 nonLocalStaticFor(10000000); 116 }); 117 118 benchmarks.addBenchmark("InitCheckForTwo (local)", () -> { 119 localStaticForTwo(10000000); 120 }); 121 122 benchmarks.addBenchmark("InitCheckForTwo (non-local)", () -> { 123 nonLocalStaticForTwo(10000000); 124 }); 125 126 benchmarks.addBenchmark("InitCheckDoWhile (local)", () -> { 127 localStaticDoWhile(10000000); 128 }); 129 130 benchmarks.addBenchmark("InitCheckDoWhile (non-local)", () -> { 131 nonLocalStaticDoWhile(10000000); 132 }); 133 } 134 } 135