File: AssetManager2_bench.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A14~beta1-4~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 326,096 kB
  • sloc: java: 2,032,373; xml: 343,016; cpp: 304,181; python: 3,683; ansic: 2,090; sh: 1,871; makefile: 120; sed: 19
file content (259 lines) | stat: -rw-r--r-- 8,881 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "benchmark/benchmark.h"

#include "android-base/stringprintf.h"
#include "androidfw/ApkAssets.h"
#include "androidfw/AssetManager.h"
#include "androidfw/AssetManager2.h"
#include "androidfw/ResourceTypes.h"

#include "BenchmarkHelpers.h"
#include "data/basic/R.h"
#include "data/libclient/R.h"
#include "data/styles/R.h"

namespace app = com::android::app;
namespace basic = com::android::basic;
namespace libclient = com::android::libclient;

namespace android {

constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk";

static void BM_AssetManagerLoadAssets(benchmark::State& state) {
  std::string path = GetTestDataPath() + "/basic/basic.apk";
  while (state.KeepRunning()) {
    std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
    AssetManager2 assets;
    assets.SetApkAssets({apk.get()});
  }
}
BENCHMARK(BM_AssetManagerLoadAssets);

static void BM_AssetManagerLoadAssetsOld(benchmark::State& state) {
  String8 path((GetTestDataPath() + "/basic/basic.apk").data());
  while (state.KeepRunning()) {
    AssetManager assets;
    assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
                        false /* isSystemAsset */);

    // Force creation.
    assets.getResources(true);
  }
}
BENCHMARK(BM_AssetManagerLoadAssetsOld);

static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) {
  std::string path = kFrameworkPath;
  while (state.KeepRunning()) {
    std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
    AssetManager2 assets;
    assets.SetApkAssets({apk.get()});
  }
}
BENCHMARK(BM_AssetManagerLoadFrameworkAssets);

static void BM_AssetManagerLoadFrameworkAssetsOld(benchmark::State& state) {
  String8 path(kFrameworkPath);
  while (state.KeepRunning()) {
    AssetManager assets;
    assets.addAssetPath(path, nullptr /* cookie */, false /* appAsLib */,
                        false /* isSystemAsset */);

    // Force creation.
    assets.getResources(true);
  }
}
BENCHMARK(BM_AssetManagerLoadFrameworkAssetsOld);

static void BM_AssetManagerGetResource(benchmark::State& state, uint32_t resid) {
  GetResourceBenchmark({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, resid, state);
}
BENCHMARK_CAPTURE(BM_AssetManagerGetResource, number1, basic::R::integer::number1);
BENCHMARK_CAPTURE(BM_AssetManagerGetResource, deep_ref, basic::R::integer::deep_ref);

static void BM_AssetManagerGetResourceOld(benchmark::State& state, uint32_t resid) {
  GetResourceBenchmarkOld({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, resid,
                          state);
}
BENCHMARK_CAPTURE(BM_AssetManagerGetResourceOld, number1, basic::R::integer::number1);
BENCHMARK_CAPTURE(BM_AssetManagerGetResourceOld, deep_ref, basic::R::integer::deep_ref);

static void BM_AssetManagerGetLibraryResource(benchmark::State& state) {
  GetResourceBenchmark(
      {GetTestDataPath() + "/lib_two/lib_two.apk", GetTestDataPath() + "/lib_one/lib_one.apk",
       GetTestDataPath() + "/libclient/libclient.apk"},
      nullptr /*config*/, libclient::R::string::foo_one, state);
}
BENCHMARK(BM_AssetManagerGetLibraryResource);

static void BM_AssetManagerGetLibraryResourceOld(benchmark::State& state) {
  GetResourceBenchmarkOld(
      {GetTestDataPath() + "/lib_two/lib_two.apk", GetTestDataPath() + "/lib_one/lib_one.apk",
       GetTestDataPath() + "/libclient/libclient.apk"},
      nullptr /*config*/, libclient::R::string::foo_one, state);
}
BENCHMARK(BM_AssetManagerGetLibraryResourceOld);

constexpr static const uint32_t kStringOkId = 0x0104000au;

static void BM_AssetManagerGetResourceFrameworkLocale(benchmark::State& state) {
  ResTable_config config;
  memset(&config, 0, sizeof(config));
  memcpy(config.language, "fr", 2);
  GetResourceBenchmark({kFrameworkPath}, &config, kStringOkId, state);
}
BENCHMARK(BM_AssetManagerGetResourceFrameworkLocale);

static void BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State& state) {
  ResTable_config config;
  memset(&config, 0, sizeof(config));
  memcpy(config.language, "fr", 2);
  GetResourceBenchmarkOld({kFrameworkPath}, &config, kStringOkId, state);
}
BENCHMARK(BM_AssetManagerGetResourceFrameworkLocaleOld);

static void BM_AssetManagerGetBag(benchmark::State& state) {
  std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk");
  if (apk == nullptr) {
    state.SkipWithError("Failed to load assets");
    return;
  }

  AssetManager2 assets;
  assets.SetApkAssets({apk.get()});

  while (state.KeepRunning()) {
    auto bag = assets.GetBag(app::R::style::StyleTwo);
    if (!bag.has_value()) {
      state.SkipWithError("Failed to load get bag");
      return;
    }
    const auto bag_end = end(*bag);
    for (auto iter = begin(*bag); iter != bag_end; ++iter) {
      uint32_t key = iter->key;
      Res_value value = iter->value;
      benchmark::DoNotOptimize(key);
      benchmark::DoNotOptimize(value);
    }
  }
}
BENCHMARK(BM_AssetManagerGetBag);

static void BM_AssetManagerGetBagOld(benchmark::State& state) {
  AssetManager assets;
  if (!assets.addAssetPath(String8((GetTestDataPath() + "/styles/styles.apk").data()),
                           nullptr /*cookie*/, false /*appAsLib*/, false /*isSystemAssets*/)) {
    state.SkipWithError("Failed to load assets");
    return;
  }

  const ResTable& table = assets.getResources(true);

  while (state.KeepRunning()) {
    const ResTable::bag_entry* bag_begin;
    const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin);
    const ResTable::bag_entry* const bag_end = bag_begin + N;
    for (auto iter = bag_begin; iter != bag_end; ++iter) {
      uint32_t key = iter->map.name.ident;
      Res_value value = iter->map.value;
      benchmark::DoNotOptimize(key);
      benchmark::DoNotOptimize(value);
    }
    table.unlockBag(bag_begin);
  }
}
BENCHMARK(BM_AssetManagerGetBagOld);

static void BM_AssetManagerGetResourceLocales(benchmark::State& state) {
  std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
  if (apk == nullptr) {
    state.SkipWithError("Failed to load assets");
    return;
  }

  AssetManager2 assets;
  assets.SetApkAssets({apk.get()});

  while (state.KeepRunning()) {
    std::set<std::string> locales =
        assets.GetResourceLocales(false /*exclude_system*/, true /*merge_equivalent_languages*/);
    benchmark::DoNotOptimize(locales);
  }
}
BENCHMARK(BM_AssetManagerGetResourceLocales);

static void BM_AssetManagerGetResourceLocalesOld(benchmark::State& state) {
  AssetManager assets;
  if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/,
                           true /*isSystemAssets*/)) {
    state.SkipWithError("Failed to load assets");
    return;
  }

  const ResTable& table = assets.getResources(true);

  while (state.KeepRunning()) {
    Vector<String8> locales;
    table.getLocales(&locales, true /*includeSystemLocales*/, true /*mergeEquivalentLangs*/);
    benchmark::DoNotOptimize(locales);
  }
}
BENCHMARK(BM_AssetManagerGetResourceLocalesOld);

static void BM_AssetManagerSetConfigurationFramework(benchmark::State& state) {
  std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(kFrameworkPath);
  if (apk == nullptr) {
    state.SkipWithError("Failed to load assets");
    return;
  }

  AssetManager2 assets;
  assets.SetApkAssets({apk.get()});

  ResTable_config config;
  memset(&config, 0, sizeof(config));

  while (state.KeepRunning()) {
    config.sdkVersion = ~config.sdkVersion;
    assets.SetConfiguration(config);
  }
}
BENCHMARK(BM_AssetManagerSetConfigurationFramework);

static void BM_AssetManagerSetConfigurationFrameworkOld(benchmark::State& state) {
  AssetManager assets;
  if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/,
                           true /*isSystemAssets*/)) {
    state.SkipWithError("Failed to load assets");
    return;
  }

  const ResTable& table = assets.getResources(true);

  ResTable_config config;
  memset(&config, 0, sizeof(config));

  while (state.KeepRunning()) {
    config.sdkVersion = ~config.sdkVersion;
    assets.setConfiguration(config);
  }
}
BENCHMARK(BM_AssetManagerSetConfigurationFrameworkOld);

}  // namespace android