Debian Codegraph @

apct-tests/perftests/core/src/android/app/ResourcesManagerPerfTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 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 package android.app;
17
18 import android.content.Context;
19 import android.content.res.Configuration;
20 import android.perftests.utils.BenchmarkState;
21 import android.perftests.utils.PerfStatusReporter;
22 import android.view.Display;
23
24 import androidx.test.InstrumentationRegistry;
25 import androidx.test.filters.LargeTest;
26
27 import org.junit.AfterClass;
28 import org.junit.Assert;
29 import org.junit.BeforeClass;
30 import org.junit.Rule;
31 import org.junit.Test;
32
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37
38 /**
39 * Benchmarks for {@link android.app.ResourcesManager}.
40 */
41 @LargeTest
42 public class ResourcesManagerPerfTest {
43 private static Context sContext;
44 private static File sResourcesCompressed;
45 private static File sResourcesUncompressed;
46
47 @Rule
48 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
49
50 @BeforeClass
51 public static void setUp() throws Exception {
52 sContext = InstrumentationRegistry.getTargetContext();
53 sResourcesCompressed = copyApkToTemp("LargeResourcesCompressed.apk",
54 "LargeResourcesCompressed.apk");
55 sResourcesUncompressed = copyApkToTemp("LargeResourcesUncompressed.apk",
56 "LargeResourcesUncompressed.apk");
57 }
58
59 @AfterClass
60 public static void tearDown() {
61 Assert.assertTrue(sResourcesCompressed.delete());
62 Assert.assertTrue(sResourcesUncompressed.delete());
63 }
64
65 private static File copyApkToTemp(String inputFileName, String fileName) throws Exception {
66 File file = File.createTempFile(fileName, null, sContext.getCacheDir());
67 try (OutputStream tempOutputStream = new FileOutputStream(file);
68 InputStream is = sContext.getResources().getAssets().openNonAsset(inputFileName)) {
69 byte[] buffer = new byte[4096];
70 int n;
71 while ((n = is.read(buffer)) >= 0) {
72 tempOutputStream.write(buffer, 0, n);
73 }
74 tempOutputStream.flush();
75 }
76 return file;
77 }
78
79 private void getResourcesForPath(String path) {
80 ResourcesManager.getInstance().getResources(null, path, null, null, null, null,
81 Display.DEFAULT_DISPLAY, null, sContext.getResources().getCompatibilityInfo(),
82 null, null);
83 }
84
85 @Test
86 public void getResourcesCached() {
87 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
88 getResourcesForPath(sResourcesCompressed.getPath());
89 while (state.keepRunning()) {
90 getResourcesForPath(sResourcesCompressed.getPath());
91 }
92 }
93
94 @Test
95 public void getResourcesCompressedUncached() {
96 ResourcesManager resourcesManager = ResourcesManager.getInstance();
97 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
98 while (state.keepRunning()) {
99 state.pauseTiming();
100 resourcesManager.invalidatePath(sResourcesCompressed.getPath());
101 state.resumeTiming();
102
103 getResourcesForPath(sResourcesCompressed.getPath());
104 }
105 }
106
107 @Test
108 public void getResourcesUncompressedUncached() {
109 ResourcesManager resourcesManager = ResourcesManager.getInstance();
110 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
111 while (state.keepRunning()) {
112 state.pauseTiming();
113 resourcesManager.invalidatePath(sResourcesUncompressed.getPath());
114 state.resumeTiming();
115
116 getResourcesForPath(sResourcesUncompressed.getPath());
117 }
118 }
119
120 @Test
121 public void applyConfigurationToResourcesLocked() {
122 ResourcesManager resourcesManager = ResourcesManager.getInstance();
123 Configuration c = new Configuration(resourcesManager.getConfiguration());
124 c.uiMode = Configuration.UI_MODE_TYPE_WATCH;
125
126 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
127 while (state.keepRunning()) {
128 resourcesManager.applyConfigurationToResources(c, null);
129
130 // Alternate configurations to ensure the set configuration is different each iteration
131 if (c.uiMode == Configuration.UI_MODE_TYPE_WATCH) {
132 c.uiMode = Configuration.UI_MODE_TYPE_TELEVISION;
133 } else {
134 c.uiMode = Configuration.UI_MODE_TYPE_WATCH;
135 }
136 }
137 }
138
139 @Test
140 public void getDisplayMetrics() {
141 ResourcesManager resourcesManager = ResourcesManager.getInstance();
142
143 final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
144 while (state.keepRunning()) {
145 state.pauseTiming();
146 // Invalidate cache.
147 resourcesManager.applyConfigurationToResources(
148 resourcesManager.getConfiguration(), null);
149 state.resumeTiming();
150
151 // Invoke twice for testing cache.
152 resourcesManager.getDisplayMetrics();
153 resourcesManager.getDisplayMetrics();
154 }
155 }
156 }