1 /* 2 * Copyright 2024 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 android.os.flagging; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.aconfig.DeviceProtosTestUtil; 22 import android.aconfig.nano.Aconfig.parsed_flag; 23 import android.perftests.utils.BenchmarkState; 24 import android.perftests.utils.PerfStatusReporter; 25 26 import org.junit.BeforeClass; 27 import org.junit.Rule; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.Parameterized; 31 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.Collection; 35 import java.util.HashMap; 36 import java.util.HashSet; 37 import java.util.List; 38 import java.util.Map; 39 import java.util.Set; 40 41 @RunWith(Parameterized.class) 42 public class AconfigPackagePerfTest { 43 44 @Rule public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); 45 46 @Parameterized.Parameters(name = "isPlatform_{0}") data()47 public static Collection<Object[]> data() { 48 return Arrays.asList(new Object[][] {{false}, {true}}); 49 } 50 51 private static final Set<String> PLATFORM_CONTAINERS = Set.of("system", "vendor", "product"); 52 private static List<parsed_flag> sFlags; 53 54 @BeforeClass init()55 public static void init() { 56 try { 57 sFlags = DeviceProtosTestUtil.loadAndParseFlagProtos(); 58 } catch (Exception e) { 59 throw new RuntimeException(e); 60 } 61 } 62 63 // if this variable is true, then the test query flags from system/product/vendor 64 // if this variable is false, then the test query flags from updatable partitions 65 @Parameterized.Parameter(0) 66 public boolean mIsPlatform; 67 68 @Test timeAconfigPackageLoadOnePackage()69 public void timeAconfigPackageLoadOnePackage() { 70 String packageName = ""; 71 for (parsed_flag flag : sFlags) { 72 if (mIsPlatform == PLATFORM_CONTAINERS.contains(flag.container)) { 73 packageName = flag.package_; 74 break; 75 } 76 } 77 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 78 while (state.keepRunning()) { 79 AconfigPackage.load(packageName); 80 } 81 } 82 83 @Test timeAconfigPackageLoadMultiplePackages()84 public void timeAconfigPackageLoadMultiplePackages() { 85 // load num packages 86 int packageNum = 25; 87 Set<String> packageSet = new HashSet<>(); 88 for (parsed_flag flag : sFlags) { 89 if (mIsPlatform == PLATFORM_CONTAINERS.contains(flag.container)) { 90 packageSet.add(flag.package_); 91 } 92 if (packageSet.size() >= packageNum) { 93 break; 94 } 95 } 96 List<String> packageList = new ArrayList(packageSet); 97 assertThat(packageList.size()).isAtLeast(packageNum); 98 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 99 for (int i = 0; state.keepRunning(); i++) { 100 AconfigPackage.load(packageList.get(i % packageNum)); 101 } 102 } 103 104 @Test timeAconfigPackageGetBooleanFlagValue()105 public void timeAconfigPackageGetBooleanFlagValue() { 106 // get one package contains num of flags 107 int flagNum = 20; 108 List<parsed_flag> l = findNumFlagsInSamePackage(flagNum, mIsPlatform); 109 List<String> flagName = new ArrayList<>(); 110 String packageName = l.get(0).package_; 111 for (parsed_flag flag : l) { 112 flagName.add(flag.name); 113 } 114 assertThat(flagName.size()).isAtLeast(flagNum); 115 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 116 AconfigPackage ap = AconfigPackage.load(packageName); 117 for (int i = 0; state.keepRunning(); i++) { 118 ap.getBooleanFlagValue(flagName.get(i % flagNum), false); 119 } 120 } 121 findNumFlagsInSamePackage(int num, boolean isPlatform)122 private static List<parsed_flag> findNumFlagsInSamePackage(int num, boolean isPlatform) { 123 Map<String, List<parsed_flag>> packageToFlag = new HashMap<>(); 124 List<parsed_flag> ret = new ArrayList<parsed_flag>(); 125 for (parsed_flag flag : sFlags) { 126 if (isPlatform == PLATFORM_CONTAINERS.contains(flag.container)) { 127 ret = 128 packageToFlag.computeIfAbsent( 129 flag.package_, k -> new ArrayList<parsed_flag>()); 130 ret.add(flag); 131 if (ret.size() >= num) { 132 break; 133 } 134 } 135 } 136 return ret; 137 } 138 } 139