|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include <benchmark/benchmark.h> |
| 21 | +#include <count_min.hpp> |
| 22 | + |
| 23 | +#include <cstddef> |
| 24 | +#include <cstdint> |
| 25 | +#include <string> |
| 26 | +#include <vector> |
| 27 | + |
| 28 | +namespace |
| 29 | +{ |
| 30 | + |
| 31 | +using Sketch = datasketches::count_min_sketch<uint64_t>; |
| 32 | + |
| 33 | +// Roughly 99.9% confidence and 0.1% relative error: |
| 34 | +// suggest_num_hashes(0.999) == 7 and suggest_num_buckets(0.001) == 2719. |
| 35 | +constexpr uint8_t NUM_HASHES = 7; |
| 36 | +constexpr uint32_t NUM_BUCKETS = 2719; |
| 37 | + |
| 38 | +std::vector<uint64_t> makeUInt64Keys(size_t size) |
| 39 | +{ |
| 40 | + std::vector<uint64_t> keys; |
| 41 | + keys.reserve(size); |
| 42 | + for (size_t i = 0; i < size; ++i) |
| 43 | + keys.push_back(static_cast<uint64_t>(i * 0x9e3779b97f4a7c15ULL)); |
| 44 | + return keys; |
| 45 | +} |
| 46 | + |
| 47 | +std::vector<std::string> makeStringKeys(size_t size) |
| 48 | +{ |
| 49 | + std::vector<std::string> keys; |
| 50 | + keys.reserve(size); |
| 51 | + for (size_t i = 0; i < size; ++i) |
| 52 | + keys.push_back("countmin-key-" + std::to_string(i * 2654435761ULL)); |
| 53 | + return keys; |
| 54 | +} |
| 55 | + |
| 56 | +int64_t totalStringBytes(const std::vector<std::string> & keys) |
| 57 | +{ |
| 58 | + int64_t bytes = 0; |
| 59 | + for (const auto & key : keys) |
| 60 | + bytes += static_cast<int64_t>(key.size()); |
| 61 | + return bytes; |
| 62 | +} |
| 63 | + |
| 64 | +void BM_CountMinUpdateUInt64(benchmark::State & state) |
| 65 | +{ |
| 66 | + const auto keys = makeUInt64Keys(static_cast<size_t>(state.range(0))); |
| 67 | + for (auto _ : state) |
| 68 | + { |
| 69 | + state.PauseTiming(); |
| 70 | + Sketch sketch(NUM_HASHES, NUM_BUCKETS); |
| 71 | + state.ResumeTiming(); |
| 72 | + |
| 73 | + for (const auto key : keys) |
| 74 | + sketch.update(&key, sizeof(key), 1); |
| 75 | + |
| 76 | + benchmark::DoNotOptimize(sketch.get_total_weight()); |
| 77 | + } |
| 78 | + |
| 79 | + state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(keys.size())); |
| 80 | + state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(keys.size() * sizeof(uint64_t))); |
| 81 | +} |
| 82 | + |
| 83 | +void BM_CountMinUpdateStringBytes(benchmark::State & state) |
| 84 | +{ |
| 85 | + const auto keys = makeStringKeys(static_cast<size_t>(state.range(0))); |
| 86 | + const auto bytes_per_iteration = totalStringBytes(keys); |
| 87 | + |
| 88 | + for (auto _ : state) |
| 89 | + { |
| 90 | + state.PauseTiming(); |
| 91 | + Sketch sketch(NUM_HASHES, NUM_BUCKETS); |
| 92 | + state.ResumeTiming(); |
| 93 | + |
| 94 | + for (const auto & key : keys) |
| 95 | + sketch.update(key.data(), key.size(), 1); |
| 96 | + |
| 97 | + benchmark::DoNotOptimize(sketch.get_total_weight()); |
| 98 | + } |
| 99 | + |
| 100 | + state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(keys.size())); |
| 101 | + state.SetBytesProcessed(state.iterations() * bytes_per_iteration); |
| 102 | +} |
| 103 | + |
| 104 | +void BM_CountMinEstimateUInt64(benchmark::State & state) |
| 105 | +{ |
| 106 | + const auto keys = makeUInt64Keys(static_cast<size_t>(state.range(0))); |
| 107 | + Sketch sketch(NUM_HASHES, NUM_BUCKETS); |
| 108 | + for (const auto key : keys) |
| 109 | + sketch.update(&key, sizeof(key), 1); |
| 110 | + |
| 111 | + uint64_t sum = 0; |
| 112 | + for (auto _ : state) |
| 113 | + { |
| 114 | + for (const auto key : keys) |
| 115 | + sum += sketch.get_estimate(&key, sizeof(key)); |
| 116 | + |
| 117 | + benchmark::DoNotOptimize(sum); |
| 118 | + } |
| 119 | + |
| 120 | + state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(keys.size())); |
| 121 | + state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(keys.size() * sizeof(uint64_t))); |
| 122 | +} |
| 123 | + |
| 124 | +void BM_CountMinEstimateStringBytes(benchmark::State & state) |
| 125 | +{ |
| 126 | + const auto keys = makeStringKeys(static_cast<size_t>(state.range(0))); |
| 127 | + const auto bytes_per_iteration = totalStringBytes(keys); |
| 128 | + |
| 129 | + Sketch sketch(NUM_HASHES, NUM_BUCKETS); |
| 130 | + for (const auto & key : keys) |
| 131 | + sketch.update(key.data(), key.size(), 1); |
| 132 | + |
| 133 | + uint64_t sum = 0; |
| 134 | + for (auto _ : state) |
| 135 | + { |
| 136 | + for (const auto & key : keys) |
| 137 | + sum += sketch.get_estimate(key.data(), key.size()); |
| 138 | + |
| 139 | + benchmark::DoNotOptimize(sum); |
| 140 | + } |
| 141 | + |
| 142 | + state.SetItemsProcessed(state.iterations() * static_cast<int64_t>(keys.size())); |
| 143 | + state.SetBytesProcessed(state.iterations() * bytes_per_iteration); |
| 144 | +} |
| 145 | + |
| 146 | +} |
| 147 | + |
| 148 | +BENCHMARK(BM_CountMinUpdateUInt64)->RangeMultiplier(8)->Range(1024, 65536); |
| 149 | +BENCHMARK(BM_CountMinUpdateStringBytes)->RangeMultiplier(8)->Range(1024, 65536); |
| 150 | +BENCHMARK(BM_CountMinEstimateUInt64)->RangeMultiplier(8)->Range(1024, 65536); |
| 151 | +BENCHMARK(BM_CountMinEstimateStringBytes)->RangeMultiplier(8)->Range(1024, 65536); |
0 commit comments