base2_exponential_histogram_indexer_benchmark.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #include <benchmark/benchmark.h>
  4. #include <stdint.h>
  5. #include <array>
  6. #include <random>
  7. #include "opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h"
  8. using namespace opentelemetry::sdk::metrics;
  9. namespace
  10. {
  11. void BM_NewIndexer(benchmark::State &state)
  12. {
  13. std::array<int, 1000> batch;
  14. std::default_random_engine generator;
  15. std::uniform_int_distribution<int> distribution(1, 32);
  16. while (state.KeepRunningBatch(static_cast<benchmark::IterationCount>(batch.size())))
  17. {
  18. state.PauseTiming();
  19. for (auto &value : batch)
  20. {
  21. value = distribution(generator);
  22. }
  23. state.ResumeTiming();
  24. for (const auto value : batch)
  25. {
  26. benchmark::DoNotOptimize(Base2ExponentialHistogramIndexer(value));
  27. }
  28. }
  29. }
  30. BENCHMARK(BM_NewIndexer);
  31. void BM_ComputeIndex(benchmark::State &state)
  32. {
  33. std::array<double, 1000> batch;
  34. std::default_random_engine generator;
  35. std::uniform_real_distribution<double> distribution(0, 1000);
  36. Base2ExponentialHistogramIndexer indexer(static_cast<int32_t>(state.range(0)));
  37. while (state.KeepRunningBatch(static_cast<benchmark::IterationCount>(batch.size())))
  38. {
  39. state.PauseTiming();
  40. for (auto &value : batch)
  41. {
  42. value = distribution(generator);
  43. }
  44. state.ResumeTiming();
  45. for (const auto value : batch)
  46. {
  47. benchmark::DoNotOptimize(indexer.ComputeIndex(value));
  48. }
  49. }
  50. }
  51. BENCHMARK(BM_ComputeIndex)->Arg(-1)->Arg(0)->Arg(1)->Arg(20);
  52. } // namespace
  53. BENCHMARK_MAIN();