diff --git a/sdk/test/metrics/BUILD b/sdk/test/metrics/BUILD index 24b426fdf5..3cf379494d 100644 --- a/sdk/test/metrics/BUILD +++ b/sdk/test/metrics/BUILD @@ -360,6 +360,21 @@ otel_cc_benchmark( ], ) +otel_cc_benchmark( + name = "base2_exponential_histogram_indexer_benchmark", + srcs = [ + "base2_exponential_histogram_indexer_benchmark.cc", + ], + tags = [ + "benchmark", + "metrics", + "test", + ], + deps = [ + "metrics_common_test_utils", + ], +) + otel_cc_benchmark( name = "histogram_aggregation_benchmark", srcs = [ diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 8e60e3b342..b30d71ae22 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -49,6 +49,12 @@ if(WITH_BENCHMARK) target_link_libraries(attributes_hashmap_benchmark benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common) + add_executable(base2_exponential_histogram_indexer_benchmark + base2_exponential_histogram_indexer_benchmark.cc) + target_link_libraries( + base2_exponential_histogram_indexer_benchmark benchmark::benchmark + opentelemetry_metrics ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common) + add_executable(histogram_aggregation_benchmark histogram_aggregation_benchmark.cc) target_link_libraries( diff --git a/sdk/test/metrics/base2_exponential_histogram_indexer_benchmark.cc b/sdk/test/metrics/base2_exponential_histogram_indexer_benchmark.cc new file mode 100644 index 0000000000..9f1d0d864d --- /dev/null +++ b/sdk/test/metrics/base2_exponential_histogram_indexer_benchmark.cc @@ -0,0 +1,65 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h" + +#include +#include +#include + +using namespace opentelemetry::sdk::metrics; +namespace +{ + +void BM_NewIndexer(benchmark::State &state) +{ + std::array batch; + std::default_random_engine generator; + std::uniform_int_distribution distribution(1, 32); + + while (state.KeepRunningBatch(static_cast(batch.size()))) + { + state.PauseTiming(); + for (auto &value : batch) + { + value = distribution(generator); + } + state.ResumeTiming(); + + for (const auto value : batch) + { + benchmark::DoNotOptimize(Base2ExponentialHistogramIndexer(value)); + } + } +} + +BENCHMARK(BM_NewIndexer); + +void BM_ComputeIndex(benchmark::State &state) +{ + std::array batch; + std::default_random_engine generator; + std::uniform_real_distribution distribution(0, 1000); + Base2ExponentialHistogramIndexer indexer(static_cast(state.range(0))); + + while (state.KeepRunningBatch(static_cast(batch.size()))) + { + state.PauseTiming(); + for (auto &value : batch) + { + value = distribution(generator); + } + state.ResumeTiming(); + + for (const auto value : batch) + { + benchmark::DoNotOptimize(indexer.ComputeIndex(value)); + } + } +} + +BENCHMARK(BM_ComputeIndex)->Arg(-1)->Arg(0)->Arg(1)->Arg(20); + +} // namespace + +BENCHMARK_MAIN();