BenchmarkHelpers.cpp revision 73f6f9daf6bb38e49747bd103c97617b3dccddc4
1/*
2 * Copyright (C) 2017 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 "BenchmarkHelpers.h"
18
19#include "android-base/stringprintf.h"
20#include "androidfw/AssetManager.h"
21#include "androidfw/AssetManager2.h"
22
23namespace android {
24
25void GetResourceBenchmarkOld(const std::vector<std::string>& paths, const ResTable_config* config,
26                             uint32_t resid, benchmark::State& state) {
27  AssetManager assetmanager;
28  for (const std::string& path : paths) {
29    if (!assetmanager.addAssetPath(String8(path.c_str()), nullptr /* cookie */,
30                                   false /* appAsLib */, false /* isSystemAssets */)) {
31      state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
32      return;
33    }
34  }
35
36  if (config != nullptr) {
37    assetmanager.setConfiguration(*config);
38  }
39
40  const ResTable& table = assetmanager.getResources(true);
41
42  Res_value value;
43  ResTable_config selected_config;
44  uint32_t flags;
45
46  while (state.KeepRunning()) {
47    table.getResource(resid, &value, false /*may_be_bag*/, 0u /*density*/, &flags,
48                      &selected_config);
49  }
50}
51
52void GetResourceBenchmark(const std::vector<std::string>& paths, const ResTable_config* config,
53                          uint32_t resid, benchmark::State& state) {
54  std::vector<std::unique_ptr<const ApkAssets>> apk_assets;
55  std::vector<const ApkAssets*> apk_assets_ptrs;
56  for (const std::string& path : paths) {
57    std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path);
58    if (apk == nullptr) {
59      state.SkipWithError(base::StringPrintf("Failed to load assets %s", path.c_str()).c_str());
60      return;
61    }
62    apk_assets_ptrs.push_back(apk.get());
63    apk_assets.push_back(std::move(apk));
64  }
65
66  AssetManager2 assetmanager;
67  assetmanager.SetApkAssets(apk_assets_ptrs);
68  if (config != nullptr) {
69    assetmanager.SetConfiguration(*config);
70  }
71
72  Res_value value;
73  ResTable_config selected_config;
74  uint32_t flags;
75
76  while (state.KeepRunning()) {
77    assetmanager.GetResource(resid, false /* may_be_bag */, 0u /* density_override */, &value,
78                             &selected_config, &flags);
79  }
80}
81
82}  // namespace android
83