blob: 9ff947807a1e26a0c4a4c202c9115e65d6b96452 [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
19#include "androidfw/ApkAssets.h"
20#include "androidfw/AssetManager.h"
21#include "androidfw/AssetManager2.h"
22#include "androidfw/ResourceTypes.h"
23
24#include "TestHelpers.h"
25#include "data/basic/R.h"
26#include "data/styles/R.h"
27
28namespace basic = com::android::basic;
29namespace app = com::android::app;
30
31namespace android {
32
33constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk";
34
35static void BM_AssetManagerLoadAssets(benchmark::State& state) {
36 std::string path = GetTestDataPath() + "/basic/basic.apk";
37 while (state.KeepRunning()) {
38 std::unique_ptr<ApkAssets> apk = ApkAssets::Load(path);
39 AssetManager2 assets;
40 assets.SetApkAssets({apk.get()});
41 }
42}
43BENCHMARK(BM_AssetManagerLoadAssets);
44
45static void BM_AssetManagerLoadAssetsOld(benchmark::State& state) {
46 String8 path((GetTestDataPath() + "/basic/basic.apk").data());
47 while (state.KeepRunning()) {
48 AssetManager assets;
49 assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
50 false /* isSystemAsset */);
51
52 // Force creation.
53 assets.getResources(true);
54 }
55}
56BENCHMARK(BM_AssetManagerLoadAssetsOld);
57
58static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) {
59 std::string path = kFrameworkPath;
60 while (state.KeepRunning()) {
61 std::unique_ptr<ApkAssets> apk = ApkAssets::Load(path);
62 AssetManager2 assets;
63 assets.SetApkAssets({apk.get()});
64 }
65}
66BENCHMARK(BM_AssetManagerLoadFrameworkAssets);
67
68static void BM_AssetManagerLoadFrameworkAssetsOld(benchmark::State& state) {
69 String8 path(kFrameworkPath);
70 while (state.KeepRunning()) {
71 AssetManager assets;
72 assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
73 false /* isSystemAsset */);
74
75 // Force creation.
76 assets.getResources(true);
77 }
78}
79BENCHMARK(BM_AssetManagerLoadFrameworkAssetsOld);
80
81static void BM_AssetManagerGetResource(benchmark::State& state) {
82 std::unique_ptr<ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/basic/basic.apk");
83 if (apk == nullptr) {
84 state.SkipWithError("Failed to load assets");
85 return;
86 }
87
88 AssetManager2 assets;
89 assets.SetApkAssets({apk.get()});
90
91 Res_value value;
92 ResTable_config selected_config;
93 uint32_t flags;
94
95 while (state.KeepRunning()) {
96 assets.GetResource(basic::R::integer::number1, false /* may_be_bag */,
97 0u /* density_override */, &value, &selected_config, &flags);
98 }
99}
100BENCHMARK(BM_AssetManagerGetResource);
101
102static void BM_AssetManagerGetResourceOld(benchmark::State& state) {
103 AssetManager assets;
104 if (!assets.addAssetPath(String8((GetTestDataPath() + "/basic/basic.apk").data()),
105 nullptr /* cookie */, false /* appAsLib */,
106 false /* isSystemAssets */)) {
107 state.SkipWithError("Failed to load assets");
108 return;
109 }
110
111 const ResTable& table = assets.getResources(true);
112
113 Res_value value;
114 ResTable_config selected_config;
115 uint32_t flags;
116
117 while (state.KeepRunning()) {
118 table.getResource(basic::R::integer::number1, &value, false /* may_be_bag */,
119 0u /* density_override */, &flags, &selected_config);
120 }
121}
122BENCHMARK(BM_AssetManagerGetResourceOld);
123
124constexpr static const uint32_t kStringOkId = 0x0104000au;
125
126static void BM_AssetManagerGetResourceFrameworkLocale(benchmark::State& state) {
127 std::unique_ptr<ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
128 if (apk == nullptr) {
129 state.SkipWithError("Failed to load assets");
130 return;
131 }
132
133 AssetManager2 assets;
134 assets.SetApkAssets({apk.get()});
135
136 ResTable_config config;
137 memset(&config, 0, sizeof(config));
138 memcpy(config.language, "fr", 2);
139 assets.SetConfiguration(config);
140
141 Res_value value;
142 ResTable_config selected_config;
143 uint32_t flags;
144
145 while (state.KeepRunning()) {
146 assets.GetResource(kStringOkId, false /* may_be_bag */, 0u /* density_override */, &value,
147 &selected_config, &flags);
148 }
149}
150BENCHMARK(BM_AssetManagerGetResourceFrameworkLocale);
151
152static void BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State& state) {
153 AssetManager assets;
154 if (!assets.addAssetPath(String8((GetTestDataPath() + "/basic/basic.apk").data()),
155 nullptr /* cookie */, false /* appAsLib */,
156 false /* isSystemAssets */)) {
157 state.SkipWithError("Failed to load assets");
158 return;
159 }
160
161 ResTable_config config;
162 memset(&config, 0, sizeof(config));
163 memcpy(config.language, "fr", 2);
164 assets.setConfiguration(config, nullptr);
165
166 const ResTable& table = assets.getResources(true);
167
168 Res_value value;
169 ResTable_config selected_config;
170 uint32_t flags;
171
172 while (state.KeepRunning()) {
173 table.getResource(kStringOkId, &value, false /* may_be_bag */, 0u /* density_override */,
174 &flags, &selected_config);
175 }
176}
177BENCHMARK(BM_AssetManagerGetResourceFrameworkLocaleOld);
178
179static void BM_AssetManagerGetBag(benchmark::State& state) {
180 std::unique_ptr<ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
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 const ResolvedBag* bag = assets.GetBag(app::R::style::StyleTwo);
191 const auto bag_end = end(bag);
192 for (auto iter = begin(bag); iter != bag_end; ++iter) {
193 uint32_t key = iter->key;
194 Res_value value = iter->value;
195 benchmark::DoNotOptimize(key);
196 benchmark::DoNotOptimize(value);
197 }
198 }
199}
200BENCHMARK(BM_AssetManagerGetBag);
201
202static void BM_AssetManagerGetBagOld(benchmark::State& state) {
203 AssetManager assets;
204 if (!assets.addAssetPath(String8((GetTestDataPath() + "/styles/styles.apk").data()),
205 nullptr /* cookie */, false /* appAsLib */,
206 false /* isSystemAssets */)) {
207 state.SkipWithError("Failed to load assets");
208 return;
209 }
210
211 const ResTable& table = assets.getResources(true);
212
213 while (state.KeepRunning()) {
214 const ResTable::bag_entry* bag_begin;
215 const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin);
216 const ResTable::bag_entry* const bag_end = bag_begin + N;
217 for (auto iter = bag_begin; iter != bag_end; ++iter) {
218 uint32_t key = iter->map.name.ident;
219 Res_value value = iter->map.value;
220 benchmark::DoNotOptimize(key);
221 benchmark::DoNotOptimize(value);
222 }
223 table.unlockBag(bag_begin);
224 }
225}
226BENCHMARK(BM_AssetManagerGetBagOld);
227
228} // namespace android