Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 20 | #include "androidfw/ApkAssets.h" |
| 21 | #include "androidfw/AssetManager.h" |
| 22 | #include "androidfw/AssetManager2.h" |
| 23 | #include "androidfw/ResourceTypes.h" |
| 24 | |
Adam Lesinski | c8f71aa | 2017-02-08 07:03:50 -0800 | [diff] [blame] | 25 | #include "BenchmarkHelpers.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 26 | #include "data/basic/R.h" |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 27 | #include "data/libclient/R.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 28 | #include "data/styles/R.h" |
| 29 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 30 | namespace app = com::android::app; |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 31 | namespace basic = com::android::basic; |
| 32 | namespace libclient = com::android::libclient; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | constexpr const static char* kFrameworkPath = "/system/framework/framework-res.apk"; |
| 37 | |
| 38 | static void BM_AssetManagerLoadAssets(benchmark::State& state) { |
| 39 | std::string path = GetTestDataPath() + "/basic/basic.apk"; |
| 40 | while (state.KeepRunning()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 41 | std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 42 | AssetManager2 assets; |
| 43 | assets.SetApkAssets({apk.get()}); |
| 44 | } |
| 45 | } |
| 46 | BENCHMARK(BM_AssetManagerLoadAssets); |
| 47 | |
| 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 | |
| 61 | static void BM_AssetManagerLoadFrameworkAssets(benchmark::State& state) { |
| 62 | std::string path = kFrameworkPath; |
| 63 | while (state.KeepRunning()) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 64 | std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(path); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 65 | AssetManager2 assets; |
| 66 | assets.SetApkAssets({apk.get()}); |
| 67 | } |
| 68 | } |
| 69 | BENCHMARK(BM_AssetManagerLoadFrameworkAssets); |
| 70 | |
| 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 | |
Adam Lesinski | fae57eb | 2018-01-23 03:16:33 -0800 | [diff] [blame] | 84 | static void BM_AssetManagerGetResource(benchmark::State& state) { |
| 85 | GetResourceBenchmark({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, |
| 86 | basic::R::integer::number1, state); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 87 | } |
Adam Lesinski | fae57eb | 2018-01-23 03:16:33 -0800 | [diff] [blame] | 88 | BENCHMARK(BM_AssetManagerGetResource); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | fae57eb | 2018-01-23 03:16:33 -0800 | [diff] [blame] | 90 | static void BM_AssetManagerGetResourceOld(benchmark::State& state) { |
| 91 | GetResourceBenchmarkOld({GetTestDataPath() + "/basic/basic.apk"}, nullptr /*config*/, |
| 92 | basic::R::integer::number1, state); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 93 | } |
Adam Lesinski | fae57eb | 2018-01-23 03:16:33 -0800 | [diff] [blame] | 94 | BENCHMARK(BM_AssetManagerGetResourceOld); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 96 | static void BM_AssetManagerGetLibraryResource(benchmark::State& state) { |
| 97 | GetResourceBenchmark( |
| 98 | {GetTestDataPath() + "/lib_two/lib_two.apk", GetTestDataPath() + "/lib_one/lib_one.apk", |
| 99 | GetTestDataPath() + "/libclient/libclient.apk"}, |
| 100 | nullptr /*config*/, libclient::R::string::foo_one, state); |
| 101 | } |
| 102 | BENCHMARK(BM_AssetManagerGetLibraryResource); |
| 103 | |
| 104 | static void BM_AssetManagerGetLibraryResourceOld(benchmark::State& state) { |
| 105 | GetResourceBenchmarkOld( |
| 106 | {GetTestDataPath() + "/lib_two/lib_two.apk", GetTestDataPath() + "/lib_one/lib_one.apk", |
| 107 | GetTestDataPath() + "/libclient/libclient.apk"}, |
| 108 | nullptr /*config*/, libclient::R::string::foo_one, state); |
| 109 | } |
| 110 | BENCHMARK(BM_AssetManagerGetLibraryResourceOld); |
| 111 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 112 | constexpr static const uint32_t kStringOkId = 0x0104000au; |
| 113 | |
| 114 | static void BM_AssetManagerGetResourceFrameworkLocale(benchmark::State& state) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 115 | ResTable_config config; |
| 116 | memset(&config, 0, sizeof(config)); |
| 117 | memcpy(config.language, "fr", 2); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 118 | GetResourceBenchmark({kFrameworkPath}, &config, kStringOkId, state); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 119 | } |
| 120 | BENCHMARK(BM_AssetManagerGetResourceFrameworkLocale); |
| 121 | |
| 122 | static void BM_AssetManagerGetResourceFrameworkLocaleOld(benchmark::State& state) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 123 | ResTable_config config; |
| 124 | memset(&config, 0, sizeof(config)); |
| 125 | memcpy(config.language, "fr", 2); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 126 | GetResourceBenchmarkOld({kFrameworkPath}, &config, kStringOkId, state); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 127 | } |
| 128 | BENCHMARK(BM_AssetManagerGetResourceFrameworkLocaleOld); |
| 129 | |
| 130 | static void BM_AssetManagerGetBag(benchmark::State& state) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 131 | std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(GetTestDataPath() + "/styles/styles.apk"); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 132 | if (apk == nullptr) { |
| 133 | state.SkipWithError("Failed to load assets"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | AssetManager2 assets; |
| 138 | assets.SetApkAssets({apk.get()}); |
| 139 | |
| 140 | while (state.KeepRunning()) { |
| 141 | const ResolvedBag* bag = assets.GetBag(app::R::style::StyleTwo); |
| 142 | const auto bag_end = end(bag); |
| 143 | for (auto iter = begin(bag); iter != bag_end; ++iter) { |
| 144 | uint32_t key = iter->key; |
| 145 | Res_value value = iter->value; |
| 146 | benchmark::DoNotOptimize(key); |
| 147 | benchmark::DoNotOptimize(value); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | BENCHMARK(BM_AssetManagerGetBag); |
| 152 | |
| 153 | static void BM_AssetManagerGetBagOld(benchmark::State& state) { |
| 154 | AssetManager assets; |
| 155 | if (!assets.addAssetPath(String8((GetTestDataPath() + "/styles/styles.apk").data()), |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 156 | nullptr /*cookie*/, false /*appAsLib*/, false /*isSystemAssets*/)) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 157 | state.SkipWithError("Failed to load assets"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | const ResTable& table = assets.getResources(true); |
| 162 | |
| 163 | while (state.KeepRunning()) { |
| 164 | const ResTable::bag_entry* bag_begin; |
| 165 | const ssize_t N = table.lockBag(app::R::style::StyleTwo, &bag_begin); |
| 166 | const ResTable::bag_entry* const bag_end = bag_begin + N; |
| 167 | for (auto iter = bag_begin; iter != bag_end; ++iter) { |
| 168 | uint32_t key = iter->map.name.ident; |
| 169 | Res_value value = iter->map.value; |
| 170 | benchmark::DoNotOptimize(key); |
| 171 | benchmark::DoNotOptimize(value); |
| 172 | } |
| 173 | table.unlockBag(bag_begin); |
| 174 | } |
| 175 | } |
| 176 | BENCHMARK(BM_AssetManagerGetBagOld); |
| 177 | |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 178 | static void BM_AssetManagerGetResourceLocales(benchmark::State& state) { |
| 179 | std::unique_ptr<const ApkAssets> apk = ApkAssets::Load(kFrameworkPath); |
| 180 | if (apk == nullptr) { |
| 181 | state.SkipWithError("Failed to load assets"); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | AssetManager2 assets; |
| 186 | assets.SetApkAssets({apk.get()}); |
| 187 | |
| 188 | while (state.KeepRunning()) { |
| 189 | std::set<std::string> locales = |
| 190 | assets.GetResourceLocales(false /*exclude_system*/, true /*merge_equivalent_languages*/); |
| 191 | benchmark::DoNotOptimize(locales); |
| 192 | } |
| 193 | } |
| 194 | BENCHMARK(BM_AssetManagerGetResourceLocales); |
| 195 | |
| 196 | static void BM_AssetManagerGetResourceLocalesOld(benchmark::State& state) { |
| 197 | AssetManager assets; |
| 198 | if (!assets.addAssetPath(String8(kFrameworkPath), nullptr /*cookie*/, false /*appAsLib*/, |
Adam Lesinski | fae57eb | 2018-01-23 03:16:33 -0800 | [diff] [blame] | 199 | false /*isSystemAssets*/)) { |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 200 | state.SkipWithError("Failed to load assets"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | const ResTable& table = assets.getResources(true); |
| 205 | |
| 206 | while (state.KeepRunning()) { |
| 207 | Vector<String8> locales; |
| 208 | table.getLocales(&locales, true /*includeSystemLocales*/, true /*mergeEquivalentLangs*/); |
| 209 | benchmark::DoNotOptimize(locales); |
| 210 | } |
| 211 | } |
| 212 | BENCHMARK(BM_AssetManagerGetResourceLocalesOld); |
| 213 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 214 | } // namespace android |