| /* | |
| * Copyright (C) 2026 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 <algorithm> | |
| #include <numeric> | |
| #include <random> | |
| #include <string> | |
| #include "ResourceTable.h" | |
| #include "androidfw/IDiagnostics.h" | |
| #include "benchmark/benchmark.h" | |
| namespace aapt { | |
| class DummyDiagnostics : public android::IDiagnostics { | |
| public: | |
| void Log(Level level, android::DiagMessageActual& actual_msg) override { | |
| } | |
| }; | |
| static void BM_ResourceTablePartitionedView(benchmark::State& state) { | |
| DummyDiagnostics diag; | |
| ResourceTable table; | |
| int num_items = state.range(0); | |
| std::vector<std::string> types = {"string", "id", "layout", "color", "drawable", "attr", | |
| "style", "array", "plurals", "bool", "integer", "dimen"}; | |
| // use random names for the items | |
| std::vector<int> indices(num_items); | |
| std::iota(indices.begin(), indices.end(), 0); | |
| std::mt19937 rng(42); | |
| std::shuffle(indices.begin(), indices.end(), rng); | |
| for (int i = 0; i < num_items; i++) { | |
| std::string type = types[i % types.size()]; | |
| std::string name = "android:" + type + "/res_" + std::to_string(indices[i]); | |
| table.AddResource(NewResourceBuilder(name).Build(), &diag); | |
| } | |
| for (auto _ : state) { | |
| auto view = table.GetPartitionedView(); | |
| benchmark::DoNotOptimize(view); | |
| } | |
| } | |
| BENCHMARK(BM_ResourceTablePartitionedView)->Range(1000, 10000); | |
| } // namespace aapt | |
| BENCHMARK_MAIN(); |