Skip to content

Commit

Permalink
[SDK] Add benchmark for base2 exponential histogram indexer (#2181)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Nigmatullin authored Jun 7, 2023
1 parent d980e8b commit d233b9b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
15 changes: 15 additions & 0 deletions sdk/test/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 6 additions & 0 deletions sdk/test/metrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
65 changes: 65 additions & 0 deletions sdk/test/metrics/base2_exponential_histogram_indexer_benchmark.cc
Original file line number Diff line number Diff line change
@@ -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 <benchmark/benchmark.h>
#include <array>
#include <random>

using namespace opentelemetry::sdk::metrics;
namespace
{

void BM_NewIndexer(benchmark::State &state)
{
std::array<int, 1000> batch;
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1, 32);

while (state.KeepRunningBatch(static_cast<benchmark::IterationCount>(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<double, 1000> batch;
std::default_random_engine generator;
std::uniform_real_distribution<double> distribution(0, 1000);
Base2ExponentialHistogramIndexer indexer(static_cast<int32_t>(state.range(0)));

while (state.KeepRunningBatch(static_cast<benchmark::IterationCount>(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();

0 comments on commit d233b9b

Please sign in to comment.