1 /*
2  * Copyright (C) 2016 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 #include "benchmark/benchmark.h"
18 
19 #include "android-base/stringprintf.h"
20 #include "androidfw/ApkAssets.h"
21 #include "androidfw/AssetManager.h"
22 #include "androidfw/AssetManager2.h"
23 #include "androidfw/ResourceTypes.h"
24 
25 #include "BenchmarkHelpers.h"
26 #include "data/basic/R.h"
27 #include "data/libclient/R.h"
28 #include "data/styles/R.h"
29 
30 namespace app = com::android::app;
31 namespace basic = com::android::basic;
32 namespace libclient = com::android::libclient;
33 
34 namespace android {
35 
36 constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk";
37 
BM_AssetManagerLoadAssets(benchmark::State & state)38 static void BM_AssetManagerLoadAssets(benchmark::State& state) {
39   std::string path = GetTestDataPath() + "/basic/basic.apk";
40   while (state.KeepRunning()) {
41     std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
42     AssetManager2 assets;
43     assets.SetApkAssets({apk.get()});
44   }
45 }
46 BENCHMARK(BM_AssetManagerLoadAssets);
47 
BM_AssetManagerLoadAssetsOld(benchmark::State & state)48 static void BM_AssetManagerLoadAssetsOld(benchmark::State& state) {
49   String8 path((GetTestDataPath() + "/basic/basic.apk").data());
50   while (state.KeepRunning()) {
51     AssetManager assets;
52     assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
53                         false /* isSystemAsset */);
54 
55     // Force creation.
56     assets.getResources(true);
57   }
58 }
59 BENCHMARK(BM_AssetManagerLoadAssetsOld);
60 
BM_AssetManagerLoadFrameworkAssets(benchmark::State & state)61 static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) {
62   std::string path = kFrameworkPath;
63   while (state.KeepRunning()) {
64     std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
65     AssetManager2 assets;
66     assets.SetApkAssets({apk.get()});
67   }
68 }
69 BENCHMARK(BM_AssetManagerLoadFrameworkAssets);
70 
BM_AssetManagerLoadFrameworkAssetsOld(benchmark::State & state)71 static void BM_AssetManagerLoadFrameworkAssetsOld(benchmark::State& state) {
72   String8 path(kFrameworkPath);
73   while (state.KeepRunning()) {
74     AssetManager assets;
75     assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
76                         false /* isSystemAsset */);
77 
78     // Force creation.
79     assets.getResources(true);
80   }
81 }
82 BENCHMARK(BM_AssetManagerLoadFrameworkAssetsOld);
83 
BM_AssetManagerGetResource(benchmark::State & state,uint32_t resid)84 static void BM_AssetManagerGetResource(benchmark::State& state, uint32_t resid) {
85   GetResourceBenchmark({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, resid, state);
86 }
87 BENCHMARK_CAPTURE(BM_AssetManagerGetResource, number1, basic::R::integer::number1);
88 BENCHMARK_CAPTURE(BM_AssetManagerGetResource, deep_ref, basic::R::integer::deep_ref);
89 
BM_AssetManagerGetResourceOld(benchmark::State & state,uint32_t resid)90 static void BM_AssetManagerGetResourceOld(benchmark::State& state, uint32_t resid) {
91   GetResourceBenchmarkOld({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, resid,
92                           state);
93 }
94 BENCHMARK_CAPTURE(BM_AssetManagerGetResourceOld, number1, basic::R::integer::number1);
95 BENCHMARK_CAPTURE(BM_AssetManagerGetResourceOld, deep_ref, basic::R::integer::deep_ref);
96 
BM_AssetManagerGetLibraryResource(benchmark::State & state)97 static void BM_AssetManagerGetLibraryResource(benchmark::State& state) {
98   GetResourceBenchmark(
99       {GetTestDataPath() + "/lib_two/lib_two.apk", GetTestDataPath() + "/lib_one/lib_one.apk",
100        GetTestDataPath() + "/libclient/libclient.apk"},
101       nullptr /*config*/, libclient::R::string::foo_one, state);
102 }
103 BENCHMARK(BM_AssetManagerGetLibraryResource);
104 
BM_AssetManagerGetLibraryResourceOld(benchmark::State & state)105 static void BM_AssetManagerGetLibraryResourceOld(benchmark::State& state) {
106   GetResourceBenchmarkOld(
107       {GetTestDataPath() + "/lib_two/lib_two.apk", GetTestDataPath() + "/lib_one/lib_one.apk",
108        GetTestDataPath() + "/libclient/libclient.apk"},
109       nullptr /*config*/, libclient::R::string::foo_one, state);
110 }
111 BENCHMARK(BM_AssetManagerGetLibraryResourceOld);
112 
113 constexpr static const uint32_t kStringOkId = 0x0104000au;
114 
BM_AssetManagerGetResourceFrameworkLocale(benchmark::State & state)115 static void BM_AssetManagerGetResourceFrameworkLocale(benchmark::State& state) {
116   ResTable_config config;
117   memset(&config, 0, sizeof(config));
118   memcpy(config.language, "fr", 2);
119   GetResourceBenchmark({kFrameworkPath}, &config, kStringOkId, state);
120 }
121 BENCHMARK(BM_AssetManagerGetResourceFrameworkLocale);
122 
BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State & state)123 static void BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State& state) {
124   ResTable_config config;
125   memset(&config, 0, sizeof(config));
126   memcpy(config.language, "fr", 2);
127   GetResourceBenchmarkOld({kFrameworkPath}, &config, kStringOkId, state);
128 }
129 BENCHMARK(BM_AssetManagerGetResourceFrameworkLocaleOld);
130 
BM_AssetManagerGetBag(benchmark::State & state)131 static void BM_AssetManagerGetBag(benchmark::State& state) {
132   std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
133   if (apk == nullptr) {
134     state.SkipWithError("Failed to load assets");
135     return;
136   }
137 
138   AssetManager2 assets;
139   assets.SetApkAssets({apk.get()});
140 
141   while (state.KeepRunning()) {
142     const ResolvedBag* bag = assets.GetBag(app::R::style::StyleTwo);
143     const auto bag_end = end(bag);
144     for (auto iter = begin(bag); iter != bag_end; ++iter) {
145       uint32_t key = iter->key;
146       Res_value value = iter->value;
147       benchmark::DoNotOptimize(key);
148       benchmark::DoNotOptimize(value);
149     }
150   }
151 }
152 BENCHMARK(BM_AssetManagerGetBag);
153 
BM_AssetManagerGetBagOld(benchmark::State & state)154 static void BM_AssetManagerGetBagOld(benchmark::State& state) {
155   AssetManager assets;
156   if (!assets.addAssetPath(String8((GetTestDataPath() + "/styles/styles.apk").data()),
157                            nullptr /*cookie*/, false /*appAsLib*/, false /*isSystemAssets*/)) {
158     state.SkipWithError("Failed to load assets");
159     return;
160   }
161 
162   const ResTable& table = assets.getResources(true);
163 
164   while (state.KeepRunning()) {
165     const ResTable::bag_entry* bag_begin;
166     const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin);
167     const ResTable::bag_entry* const bag_end = bag_begin + N;
168     for (auto iter = bag_begin; iter != bag_end; ++iter) {
169       uint32_t key = iter->map.name.ident;
170       Res_value value = iter->map.value;
171       benchmark::DoNotOptimize(key);
172       benchmark::DoNotOptimize(value);
173     }
174     table.unlockBag(bag_begin);
175   }
176 }
177 BENCHMARK(BM_AssetManagerGetBagOld);
178 
BM_AssetManagerGetResourceLocales(benchmark::State & state)179 static void BM_AssetManagerGetResourceLocales(benchmark::State& state) {
180   std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
181   if (apk == nullptr) {
182     state.SkipWithError("Failed to load assets");
183     return;
184   }
185 
186   AssetManager2 assets;
187   assets.SetApkAssets({apk.get()});
188 
189   while (state.KeepRunning()) {
190     std::set<std::string> locales =
191         assets.GetResourceLocales(false /*exclude_system*/, true /*merge_equivalent_languages*/);
192     benchmark::DoNotOptimize(locales);
193   }
194 }
195 BENCHMARK(BM_AssetManagerGetResourceLocales);
196 
BM_AssetManagerGetResourceLocalesOld(benchmark::State & state)197 static void BM_AssetManagerGetResourceLocalesOld(benchmark::State& state) {
198   AssetManager assets;
199   if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/,
200                            true /*isSystemAssets*/)) {
201     state.SkipWithError("Failed to load assets");
202     return;
203   }
204 
205   const ResTable& table = assets.getResources(true);
206 
207   while (state.KeepRunning()) {
208     Vector<String8> locales;
209     table.getLocales(&locales, true /*includeSystemLocales*/, true /*mergeEquivalentLangs*/);
210     benchmark::DoNotOptimize(locales);
211   }
212 }
213 BENCHMARK(BM_AssetManagerGetResourceLocalesOld);
214 
BM_AssetManagerSetConfigurationFramework(benchmark::State & state)215 static void BM_AssetManagerSetConfigurationFramework(benchmark::State& state) {
216   std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
217   if (apk == nullptr) {
218     state.SkipWithError("Failed to load assets");
219     return;
220   }
221 
222   AssetManager2 assets;
223   assets.SetApkAssets({apk.get()});
224 
225   ResTable_config config;
226   memset(&config, 0, sizeof(config));
227 
228   while (state.KeepRunning()) {
229     config.sdkVersion = ~config.sdkVersion;
230     assets.SetConfiguration(config);
231   }
232 }
233 BENCHMARK(BM_AssetManagerSetConfigurationFramework);
234 
BM_AssetManagerSetConfigurationFrameworkOld(benchmark::State & state)235 static void BM_AssetManagerSetConfigurationFrameworkOld(benchmark::State& state) {
236   AssetManager assets;
237   if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/,
238                            true /*isSystemAssets*/)) {
239     state.SkipWithError("Failed to load assets");
240     return;
241   }
242 
243   const ResTable& table = assets.getResources(true);
244 
245   ResTable_config config;
246   memset(&config, 0, sizeof(config));
247 
248   while (state.KeepRunning()) {
249     config.sdkVersion = ~config.sdkVersion;
250     assets.setConfiguration(config);
251   }
252 }
253 BENCHMARK(BM_AssetManagerSetConfigurationFrameworkOld);
254 
255 }  // namespace android
256