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