|
| 1 | +/* |
| 2 | + * Copyright 2020 Improbable Worlds Limited |
| 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 | +#include "logging.h" |
| 17 | +#include "phtree/benchmark/benchmark_util.h" |
| 18 | +#include "phtree/phtree.h" |
| 19 | +#include <benchmark/benchmark.h> |
| 20 | +#include <random> |
| 21 | + |
| 22 | +using namespace improbable; |
| 23 | +using namespace improbable::phtree; |
| 24 | +using namespace improbable::phtree::phbenchmark; |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +const int GLOBAL_MAX = 10000; |
| 29 | + |
| 30 | +/* |
| 31 | + * Benchmark for removing entries. |
| 32 | + */ |
| 33 | +template <dimension_t DIM> |
| 34 | +class IndexBenchmark { |
| 35 | + public: |
| 36 | + IndexBenchmark(benchmark::State& state); |
| 37 | + void Benchmark(benchmark::State& state); |
| 38 | + |
| 39 | + private: |
| 40 | + void SetupWorld(benchmark::State& state); |
| 41 | + void Insert(benchmark::State& state, PhTreeD<DIM, int>& tree); |
| 42 | + void Remove(benchmark::State& state, PhTreeD<DIM, int>& tree); |
| 43 | + |
| 44 | + const TestGenerator data_type_; |
| 45 | + const int num_entities_; |
| 46 | + |
| 47 | + std::default_random_engine random_engine_; |
| 48 | + std::uniform_real_distribution<> cube_distribution_; |
| 49 | + std::vector<PhPointD<DIM>> points_; |
| 50 | +}; |
| 51 | + |
| 52 | +template <dimension_t DIM> |
| 53 | +IndexBenchmark<DIM>::IndexBenchmark(benchmark::State& state) |
| 54 | +: data_type_{static_cast<TestGenerator>(state.range(1))} |
| 55 | +, num_entities_(state.range(0)) |
| 56 | +, random_engine_{1} |
| 57 | +, cube_distribution_{0, GLOBAL_MAX} |
| 58 | +, points_(state.range(0)) { |
| 59 | + logging::SetupDefaultLogging(); |
| 60 | + SetupWorld(state); |
| 61 | +} |
| 62 | + |
| 63 | +template <dimension_t DIM> |
| 64 | +void IndexBenchmark<DIM>::Benchmark(benchmark::State& state) { |
| 65 | + for (auto _ : state) { |
| 66 | + state.PauseTiming(); |
| 67 | + auto* tree = new PhTreeD<DIM, int>(); |
| 68 | + Insert(state, *tree); |
| 69 | + state.ResumeTiming(); |
| 70 | + |
| 71 | + Remove(state, *tree); |
| 72 | + |
| 73 | + state.PauseTiming(); |
| 74 | + // avoid measuring deallocation |
| 75 | + delete tree; |
| 76 | + state.ResumeTiming(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +template <dimension_t DIM> |
| 81 | +void IndexBenchmark<DIM>::SetupWorld(benchmark::State& state) { |
| 82 | + logging::info("Setting up world with {} entities and {} dimensions.", num_entities_, DIM); |
| 83 | + CreatePointData<DIM>(points_, data_type_, num_entities_, 0, GLOBAL_MAX); |
| 84 | + |
| 85 | + state.counters["total_remove_count"] = benchmark::Counter(0); |
| 86 | + state.counters["remove_rate"] = benchmark::Counter(0, benchmark::Counter::kIsRate); |
| 87 | + |
| 88 | + logging::info("World setup complete."); |
| 89 | +} |
| 90 | + |
| 91 | +template <dimension_t DIM> |
| 92 | +void IndexBenchmark<DIM>::Insert(benchmark::State&, PhTreeD<DIM, int>& tree) { |
| 93 | + for (int i = 0; i < num_entities_; ++i) { |
| 94 | + tree.emplace(points_[i], i); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +template <dimension_t DIM> |
| 99 | +void IndexBenchmark<DIM>::Remove(benchmark::State& state, PhTreeD<DIM, int>& tree) { |
| 100 | + int n = 0; |
| 101 | + for (int i = 0; i < num_entities_; ++i) { |
| 102 | + n += tree.erase(points_[i]); |
| 103 | + } |
| 104 | + |
| 105 | + state.counters["total_remove_count"] += n; |
| 106 | + state.counters["remove_rate"] += n; |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace |
| 110 | + |
| 111 | +template <typename... Arguments> |
| 112 | +void PhTree6D(benchmark::State& state, Arguments&&...) { |
| 113 | + IndexBenchmark<6> benchmark{state}; |
| 114 | + benchmark.Benchmark(state); |
| 115 | +} |
| 116 | + |
| 117 | +template <typename... Arguments> |
| 118 | +void PhTree10D(benchmark::State& state, Arguments&&...) { |
| 119 | + IndexBenchmark<10> benchmark{state}; |
| 120 | + benchmark.Benchmark(state); |
| 121 | +} |
| 122 | + |
| 123 | +template <typename... Arguments> |
| 124 | +void PhTree20D(benchmark::State& state, Arguments&&...) { |
| 125 | + IndexBenchmark<20> benchmark{state}; |
| 126 | + benchmark.Benchmark(state); |
| 127 | +} |
| 128 | + |
| 129 | +// index type, scenario name, data_generator, num_entities |
| 130 | +BENCHMARK_CAPTURE(PhTree6D, ERASE, 0) |
| 131 | + ->RangeMultiplier(10) |
| 132 | + ->Ranges({{1000, 1000 * 1000}, {TestGenerator::CLUSTER, TestGenerator::CUBE}}) |
| 133 | + ->Unit(benchmark::kMillisecond); |
| 134 | + |
| 135 | +BENCHMARK_CAPTURE(PhTree10D, ERASE, 0) |
| 136 | + ->RangeMultiplier(10) |
| 137 | + ->Ranges({{1000, 1000 * 1000}, {TestGenerator::CLUSTER, TestGenerator::CUBE}}) |
| 138 | + ->Unit(benchmark::kMillisecond); |
| 139 | + |
| 140 | +BENCHMARK_CAPTURE(PhTree20D, ERASE, 0) |
| 141 | + ->RangeMultiplier(10) |
| 142 | + ->Ranges({{1000, 1000 * 1000}, {TestGenerator::CLUSTER, TestGenerator::CUBE}}) |
| 143 | + ->Unit(benchmark::kMillisecond); |
| 144 | + |
| 145 | +BENCHMARK_MAIN(); |
0 commit comments