blob: 2caa98c35971e6d08c6ee74275ef8c51c47eb729 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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
Adam Lesinskida431a22016-12-29 16:08:16 -050019#include "android-base/stringprintf.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070020#include "androidfw/ApkAssets.h"
21#include "androidfw/AssetManager.h"
22#include "androidfw/AssetManager2.h"
23#include "androidfw/ResourceTypes.h"
24
Adam Lesinskic8f71aa2017-02-08 07:03:50 -080025#include "BenchmarkHelpers.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070026#include "data/basic/R.h"
Adam Lesinskida431a22016-12-29 16:08:16 -050027#include "data/libclient/R.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070028#include "data/styles/R.h"
29
Adam Lesinski7ad11102016-10-28 16:39:15 -070030namespace app = com::android::app;
Adam Lesinskida431a22016-12-29 16:08:16 -050031namespace basic = com::android::basic;
32namespace libclient = com::android::libclient;
Adam Lesinski7ad11102016-10-28 16:39:15 -070033
34namespace android {
35
36constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk";
37
38static void BM_AssetManagerLoadAssets(benchmark::State& state) {
39 std::string path = GetTestDataPath() + "/basic/basic.apk";
40 while (state.KeepRunning()) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070041 auto apk = ApkAssets::Load(path);
Adam Lesinski7ad11102016-10-28 16:39:15 -070042 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070043 assets.SetApkAssets({apk});
Adam Lesinski7ad11102016-10-28 16:39:15 -070044 }
45}
46BENCHMARK(BM_AssetManagerLoadAssets);
47
48static 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}
59BENCHMARK(BM_AssetManagerLoadAssetsOld);
60
61static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) {
62 std::string path = kFrameworkPath;
63 while (state.KeepRunning()) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070064 auto apk = ApkAssets::Load(path);
Adam Lesinski7ad11102016-10-28 16:39:15 -070065 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -070066 assets.SetApkAssets({apk});
Adam Lesinski7ad11102016-10-28 16:39:15 -070067 }
68}
69BENCHMARK(BM_AssetManagerLoadFrameworkAssets);
70
71static 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}
82BENCHMARK(BM_AssetManagerLoadFrameworkAssetsOld);
83
Adam Lesinskibebfcc42018-02-12 14:27:46 -080084static void BM_AssetManagerGetResource(benchmark::State& state, uint32_t resid) {
85 GetResourceBenchmark({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, resid, state);
Adam Lesinskida431a22016-12-29 16:08:16 -050086}
Adam Lesinskibebfcc42018-02-12 14:27:46 -080087BENCHMARK_CAPTURE(BM_AssetManagerGetResource, number1, basic::R::integer::number1);
88BENCHMARK_CAPTURE(BM_AssetManagerGetResource, deep_ref, basic::R::integer::deep_ref);
Adam Lesinski7ad11102016-10-28 16:39:15 -070089
Adam Lesinskibebfcc42018-02-12 14:27:46 -080090static void BM_AssetManagerGetResourceOld(benchmark::State& state, uint32_t resid) {
91 GetResourceBenchmarkOld({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, resid,
92 state);
Adam Lesinski7ad11102016-10-28 16:39:15 -070093}
Adam Lesinskibebfcc42018-02-12 14:27:46 -080094BENCHMARK_CAPTURE(BM_AssetManagerGetResourceOld, number1, basic::R::integer::number1);
95BENCHMARK_CAPTURE(BM_AssetManagerGetResourceOld, deep_ref, basic::R::integer::deep_ref);
Adam Lesinski7ad11102016-10-28 16:39:15 -070096
Adam Lesinskida431a22016-12-29 16:08:16 -050097static 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}
103BENCHMARK(BM_AssetManagerGetLibraryResource);
104
105static 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}
111BENCHMARK(BM_AssetManagerGetLibraryResourceOld);
112
Adam Lesinski7ad11102016-10-28 16:39:15 -0700113constexpr static const uint32_t kStringOkId = 0x0104000au;
114
115static void BM_AssetManagerGetResourceFrameworkLocale(benchmark::State& state) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700116 ResTable_config config;
117 memset(&config, 0, sizeof(config));
118 memcpy(config.language, "fr", 2);
Adam Lesinskida431a22016-12-29 16:08:16 -0500119 GetResourceBenchmark({kFrameworkPath}, &config, kStringOkId, state);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700120}
121BENCHMARK(BM_AssetManagerGetResourceFrameworkLocale);
122
123static void BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State& state) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700124 ResTable_config config;
125 memset(&config, 0, sizeof(config));
126 memcpy(config.language, "fr", 2);
Adam Lesinskida431a22016-12-29 16:08:16 -0500127 GetResourceBenchmarkOld({kFrameworkPath}, &config, kStringOkId, state);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700128}
129BENCHMARK(BM_AssetManagerGetResourceFrameworkLocaleOld);
130
131static void BM_AssetManagerGetBag(benchmark::State& state) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700132 auto apk = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
Adam Lesinski7ad11102016-10-28 16:39:15 -0700133 if (apk == nullptr) {
134 state.SkipWithError("Failed to load assets");
135 return;
136 }
137
138 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700139 assets.SetApkAssets({apk});
Adam Lesinski7ad11102016-10-28 16:39:15 -0700140
141 while (state.KeepRunning()) {
Ryan Mitchelldb21f09a2020-11-16 23:08:18 +0000142 auto bag = assets.GetBag(app::R::style::StyleTwo);
143 if (!bag.has_value()) {
144 state.SkipWithError("Failed to load get bag");
145 return;
146 }
147 const auto bag_end = end(*bag);
148 for (auto iter = begin(*bag); iter != bag_end; ++iter) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700149 uint32_t key = iter->key;
150 Res_value value = iter->value;
151 benchmark::DoNotOptimize(key);
152 benchmark::DoNotOptimize(value);
153 }
154 }
155}
156BENCHMARK(BM_AssetManagerGetBag);
157
158static void BM_AssetManagerGetBagOld(benchmark::State& state) {
159 AssetManager assets;
160 if (!assets.addAssetPath(String8((GetTestDataPath() + "/styles/styles.apk").data()),
Adam Lesinskida431a22016-12-29 16:08:16 -0500161 nullptr /*cookie*/, false /*appAsLib*/, false /*isSystemAssets*/)) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700162 state.SkipWithError("Failed to load assets");
163 return;
164 }
165
166 const ResTable& table = assets.getResources(true);
167
168 while (state.KeepRunning()) {
169 const ResTable::bag_entry* bag_begin;
170 const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin);
171 const ResTable::bag_entry* const bag_end = bag_begin + N;
172 for (auto iter = bag_begin; iter != bag_end; ++iter) {
173 uint32_t key = iter->map.name.ident;
174 Res_value value = iter->map.value;
175 benchmark::DoNotOptimize(key);
176 benchmark::DoNotOptimize(value);
177 }
178 table.unlockBag(bag_begin);
179 }
180}
181BENCHMARK(BM_AssetManagerGetBagOld);
182
Adam Lesinski0c405242017-01-13 20:47:26 -0800183static void BM_AssetManagerGetResourceLocales(benchmark::State& state) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700184 auto apk = ApkAssets::Load(kFrameworkPath);
Adam Lesinski0c405242017-01-13 20:47:26 -0800185 if (apk == nullptr) {
186 state.SkipWithError("Failed to load assets");
187 return;
188 }
189
190 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700191 assets.SetApkAssets({apk});
Adam Lesinski0c405242017-01-13 20:47:26 -0800192
193 while (state.KeepRunning()) {
194 std::set<std::string> locales =
195 assets.GetResourceLocales(false /*exclude_system*/, true /*merge_equivalent_languages*/);
196 benchmark::DoNotOptimize(locales);
197 }
198}
199BENCHMARK(BM_AssetManagerGetResourceLocales);
200
201static void BM_AssetManagerGetResourceLocalesOld(benchmark::State& state) {
202 AssetManager assets;
203 if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/,
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800204 true /*isSystemAssets*/)) {
Adam Lesinski0c405242017-01-13 20:47:26 -0800205 state.SkipWithError("Failed to load assets");
206 return;
207 }
208
209 const ResTable& table = assets.getResources(true);
210
211 while (state.KeepRunning()) {
212 Vector<String8> locales;
213 table.getLocales(&locales, true /*includeSystemLocales*/, true /*mergeEquivalentLangs*/);
214 benchmark::DoNotOptimize(locales);
215 }
216}
217BENCHMARK(BM_AssetManagerGetResourceLocalesOld);
218
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800219static void BM_AssetManagerSetConfigurationFramework(benchmark::State& state) {
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700220 auto apk = ApkAssets::Load(kFrameworkPath);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800221 if (apk == nullptr) {
222 state.SkipWithError("Failed to load assets");
223 return;
224 }
225
226 AssetManager2 assets;
Yurii Zubrytskyib3455192023-05-01 14:35:48 -0700227 assets.SetApkAssets({apk});
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800228
229 ResTable_config config;
230 memset(&config, 0, sizeof(config));
Jeremy Meyer04cf00d2023-07-20 22:17:27 +0000231 std::vector<ResTable_config> configs;
232 configs.push_back(config);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800233
234 while (state.KeepRunning()) {
Jeremy Meyer04cf00d2023-07-20 22:17:27 +0000235 configs[0].sdkVersion = ~configs[0].sdkVersion;
236 assets.SetConfigurations(configs);
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800237 }
238}
239BENCHMARK(BM_AssetManagerSetConfigurationFramework);
240
241static void BM_AssetManagerSetConfigurationFrameworkOld(benchmark::State& state) {
242 AssetManager assets;
243 if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/,
244 true /*isSystemAssets*/)) {
245 state.SkipWithError("Failed to load assets");
246 return;
247 }
248
249 const ResTable& table = assets.getResources(true);
250
251 ResTable_config config;
252 memset(&config, 0, sizeof(config));
253
254 while (state.KeepRunning()) {
255 config.sdkVersion = ~config.sdkVersion;
256 assets.setConfiguration(config);
257 }
258}
259BENCHMARK(BM_AssetManagerSetConfigurationFrameworkOld);
260
Adam Lesinski7ad11102016-10-28 16:39:15 -0700261} // namespace android