|
| 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 <algorithm> |
| 24 | +#include <cstddef> |
| 25 | +#include <cstdint> |
| 26 | +#include <cstring> |
| 27 | +#include <ostream> |
| 28 | +#include <streambuf> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +namespace |
| 32 | +{ |
| 33 | + |
| 34 | +using Sketch = datasketches::count_min_sketch<uint64_t>; |
| 35 | + |
| 36 | +// Roughly 99.9% confidence. Serialization cost then scales with the bucket count. |
| 37 | +constexpr uint8_t NUM_HASHES = 7; |
| 38 | + |
| 39 | +Sketch makeSketch(uint32_t num_buckets) |
| 40 | +{ |
| 41 | + Sketch sketch(NUM_HASHES, num_buckets); |
| 42 | + for (uint64_t i = 0; i < NUM_HASHES; ++i) |
| 43 | + sketch.update(i, i + 1); |
| 44 | + return sketch; |
| 45 | +} |
| 46 | + |
| 47 | +class fixed_buffer_streambuf final: public std::streambuf |
| 48 | +{ |
| 49 | +public: |
| 50 | + explicit fixed_buffer_streambuf(std::vector<uint8_t>& buffer): buffer_(buffer), position_(0) {} |
| 51 | + |
| 52 | + void reset() |
| 53 | + { |
| 54 | + position_ = 0; |
| 55 | + } |
| 56 | + |
| 57 | + size_t bytes_written() const |
| 58 | + { |
| 59 | + return position_; |
| 60 | + } |
| 61 | + |
| 62 | +protected: |
| 63 | + std::streamsize xsputn(const char* data, std::streamsize size) override |
| 64 | + { |
| 65 | + const size_t requested = static_cast<size_t>(size); |
| 66 | + const size_t available = buffer_.size() - position_; |
| 67 | + const size_t bytes_to_write = std::min(requested, available); |
| 68 | + if (bytes_to_write > 0) { |
| 69 | + std::memcpy(buffer_.data() + position_, data, bytes_to_write); |
| 70 | + position_ += bytes_to_write; |
| 71 | + } |
| 72 | + return static_cast<std::streamsize>(bytes_to_write); |
| 73 | + } |
| 74 | + |
| 75 | + int_type overflow(int_type ch) override |
| 76 | + { |
| 77 | + if (traits_type::eq_int_type(ch, traits_type::eof())) |
| 78 | + return traits_type::not_eof(ch); |
| 79 | + if (position_ == buffer_.size()) |
| 80 | + return traits_type::eof(); |
| 81 | + buffer_[position_++] = static_cast<uint8_t>(traits_type::to_char_type(ch)); |
| 82 | + return ch; |
| 83 | + } |
| 84 | + |
| 85 | +private: |
| 86 | + std::vector<uint8_t>& buffer_; |
| 87 | + size_t position_; |
| 88 | +}; |
| 89 | + |
| 90 | +void BM_CountMinGetSerializedSizeBytes(benchmark::State& state) |
| 91 | +{ |
| 92 | + const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); |
| 93 | + |
| 94 | + for (auto _ : state) { |
| 95 | + const auto size = sketch.get_serialized_size_bytes(); |
| 96 | + benchmark::DoNotOptimize(size); |
| 97 | + } |
| 98 | + |
| 99 | + state.SetItemsProcessed(state.iterations()); |
| 100 | +} |
| 101 | + |
| 102 | +void BM_CountMinSerializeVector(benchmark::State& state) |
| 103 | +{ |
| 104 | + const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); |
| 105 | + const auto serialized_size = sketch.get_serialized_size_bytes(); |
| 106 | + |
| 107 | + for (auto _ : state) { |
| 108 | + auto bytes = sketch.serialize(); |
| 109 | + benchmark::DoNotOptimize(bytes.data()); |
| 110 | + benchmark::DoNotOptimize(bytes.size()); |
| 111 | + benchmark::ClobberMemory(); |
| 112 | + } |
| 113 | + |
| 114 | + state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(serialized_size)); |
| 115 | +} |
| 116 | + |
| 117 | +void BM_CountMinSerializeOStream(benchmark::State& state) |
| 118 | +{ |
| 119 | + const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); |
| 120 | + const auto serialized_size = sketch.get_serialized_size_bytes(); |
| 121 | + std::vector<uint8_t> bytes(serialized_size); |
| 122 | + fixed_buffer_streambuf stream_buffer(bytes); |
| 123 | + std::ostream os(&stream_buffer); |
| 124 | + |
| 125 | + for (auto _ : state) { |
| 126 | + stream_buffer.reset(); |
| 127 | + os.clear(); |
| 128 | + sketch.serialize(os); |
| 129 | + benchmark::DoNotOptimize(stream_buffer.bytes_written()); |
| 130 | + benchmark::ClobberMemory(); |
| 131 | + } |
| 132 | + |
| 133 | + state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(serialized_size)); |
| 134 | +} |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +BENCHMARK(BM_CountMinGetSerializedSizeBytes)->RangeMultiplier(8)->Range(1024, 65536); |
| 139 | +BENCHMARK(BM_CountMinSerializeVector)->RangeMultiplier(8)->Range(1024, 65536); |
| 140 | +BENCHMARK(BM_CountMinSerializeOStream)->RangeMultiplier(8)->Range(1024, 65536); |
0 commit comments