| /* | |
| * 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 <random> | |
| #include "benchmark/benchmark.h" | |
| #include "compile/XmlIdCollector.h" | |
| #include "test/Test.h" | |
| #include "xml/XmlDom.h" | |
| namespace aapt { | |
| static void BM_XmlIdCollector_Sequential(benchmark::State& state) { | |
| std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); | |
| for (auto _ : state) { | |
| state.PauseTiming(); | |
| std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>(); | |
| doc->root = util::make_unique<xml::Element>(); | |
| doc->root->name = "LinearLayout"; | |
| for (int i = 0; i < state.range(0); i++) { | |
| xml::Attribute attr; | |
| attr.name = "id"; | |
| attr.value = "@+id/view_" + std::to_string(i); | |
| doc->root->attributes.push_back(std::move(attr)); | |
| } | |
| state.ResumeTiming(); | |
| XmlIdCollector collector; | |
| collector.Consume(context.get(), doc.get()); | |
| } | |
| } | |
| BENCHMARK(BM_XmlIdCollector_Sequential)->Range(10, 100000); | |
| static void BM_XmlIdCollector_Random(benchmark::State& state) { | |
| std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build(); | |
| std::mt19937 mt(42); | |
| for (auto _ : state) { | |
| state.PauseTiming(); | |
| std::unique_ptr<xml::XmlResource> doc = util::make_unique<xml::XmlResource>(); | |
| doc->root = util::make_unique<xml::Element>(); | |
| doc->root->name = "LinearLayout"; | |
| std::vector<std::string> ids; | |
| for (int i = 0; i < state.range(0); i++) { | |
| ids.push_back("@+id/view_" + std::to_string(i)); | |
| } | |
| std::shuffle(ids.begin(), ids.end(), mt); | |
| for (const auto& id : ids) { | |
| xml::Attribute attr; | |
| attr.name = "id"; | |
| attr.value = id; | |
| doc->root->attributes.push_back(std::move(attr)); | |
| } | |
| state.ResumeTiming(); | |
| XmlIdCollector collector; | |
| collector.Consume(context.get(), doc.get()); | |
| } | |
| } | |
| BENCHMARK(BM_XmlIdCollector_Random)->Range(10, 100000); | |
| } // namespace aapt |