File: hdr_histogram_benchmark.cpp

package info (click to toggle)
libhdr-histogram 0.11.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 796 kB
  • sloc: ansic: 5,642; cpp: 133; sh: 13; makefile: 6
file content (152 lines) | stat: -rw-r--r-- 5,338 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <benchmark/benchmark.h>
#include <hdr/hdr_histogram.h>
#include <cmath>
#include <random>

#ifdef _WIN32
#pragma comment(lib, "Shlwapi.lib")
#ifdef _DEBUG
#pragma comment(lib, "benchmarkd.lib")
#else
#pragma comment(lib, "benchmark.lib")
#endif
#endif

int64_t min_value = 1;
int64_t min_precision = 3;
int64_t max_precision = 4;
int64_t min_time_unit = 1000;
int64_t max_time_unit = 1000000;
int64_t step_time_unit = 1000;
int64_t generated_datapoints = 10000000;

static void generate_arguments_pairs(benchmark::internal::Benchmark *b) {
  for (int64_t precision = min_precision; precision <= max_precision;
       precision++) {
    for (int64_t time_unit = min_time_unit; time_unit <= max_time_unit;
         time_unit *= step_time_unit) {
      b = b->ArgPair(precision, INT64_C(24) * 60 * 60 * time_unit);
    }
  }
}

static void BM_hdr_init(benchmark::State &state) {
  const int64_t precision = state.range(0);
  const int64_t max_value = state.range(1);
  for (auto _ : state) {
    struct hdr_histogram *histogram;
    benchmark::DoNotOptimize(
        hdr_init(min_value, max_value, precision, &histogram));
    // read/write barrier
    benchmark::ClobberMemory();
  }
}

static void BM_hdr_record_values(benchmark::State &state) {
  const int64_t precision = state.range(0);
  const int64_t max_value = state.range(1);
  struct hdr_histogram *histogram;
  hdr_init(min_value, max_value, precision, &histogram);
  benchmark::DoNotOptimize(histogram->counts);

  for (auto _ : state) {
    benchmark::DoNotOptimize(hdr_record_values(histogram, 1000000, 1));
    // read/write barrier
    benchmark::ClobberMemory();
  }
}

static void BM_hdr_value_at_percentile(benchmark::State &state) {
  srand(12345);
  const int64_t precision = state.range(0);
  const int64_t max_value = state.range(1);
  std::random_device rd;
  std::mt19937 e2(rd());
  std::uniform_real_distribution<> dist(0, 100);
  std::default_random_engine generator;
  // gama distribution shape 1 scale 100000
  std::gamma_distribution<double> latency_gamma_dist(1.0, 100000);
  struct hdr_histogram *histogram;
  hdr_init(min_value, max_value, precision, &histogram);
  for (int64_t i = 1; i < generated_datapoints; i++) {
    int64_t number = int64_t(latency_gamma_dist(generator)) + 1;
    number = number > max_value ? max_value : number;
    hdr_record_value(histogram, number);
  }
  benchmark::DoNotOptimize(histogram->counts);
  for (auto _ : state) {
    state.PauseTiming();
    const double percentile = dist(e2);
    state.ResumeTiming();
    benchmark::DoNotOptimize(hdr_value_at_percentile(histogram, percentile));
    // read/write barrier
    benchmark::ClobberMemory();
  }
}

static void BM_hdr_value_at_percentile_given_array(benchmark::State &state) {
  srand(12345);
  const int64_t precision = state.range(0);
  const int64_t max_value = state.range(1);
  const double percentile_list[4] = {50.0, 95.0, 99.0, 99.9};
  std::default_random_engine generator;
  // gama distribution shape 1 scale 100000
  std::gamma_distribution<double> latency_gamma_dist(1.0, 100000);
  struct hdr_histogram *histogram;
  hdr_init(min_value, max_value, precision, &histogram);
  for (int64_t i = 1; i < generated_datapoints; i++) {
    int64_t number = int64_t(latency_gamma_dist(generator)) + 1;
    number = number > max_value ? max_value : number;
    hdr_record_value(histogram, number);
  }
  benchmark::DoNotOptimize(histogram->counts);
  int64_t items_processed = 0;
  for (auto _ : state) {
    for (auto percentile : percentile_list) {
      benchmark::DoNotOptimize(hdr_value_at_percentile(histogram, percentile));
      // read/write barrier
      benchmark::ClobberMemory();
    }
    items_processed += 4;
  }
  state.SetItemsProcessed(items_processed);
}

static void BM_hdr_value_at_percentiles_given_array(benchmark::State &state) {
  srand(12345);
  const int64_t precision = state.range(0);
  const int64_t max_value = state.range(1);
  const double percentile_list[4] = {50.0, 95.0, 99.0, 99.9};
  std::default_random_engine generator;
  // gama distribution shape 1 scale 100000
  std::gamma_distribution<double> latency_gamma_dist(1.0, 100000);
  struct hdr_histogram *histogram;
  hdr_init(min_value, max_value, precision, &histogram);
  for (int64_t i = 1; i < generated_datapoints; i++) {
    int64_t number = int64_t(latency_gamma_dist(generator)) + 1;
    number = number > max_value ? max_value : number;
    hdr_record_value(histogram, number);
  }
  benchmark::DoNotOptimize(histogram->counts);
  int64_t *values = (int64_t*) malloc(4 * sizeof(int64_t));
  benchmark::DoNotOptimize(values);
  int64_t items_processed = 0;
  for (auto _ : state) {
    benchmark::DoNotOptimize(
        hdr_value_at_percentiles(histogram, percentile_list, values, 4));
    // read/write barrier
    benchmark::ClobberMemory();
    items_processed += 4;
  }
  state.SetItemsProcessed(items_processed);
}

// Register the functions as a benchmark
BENCHMARK(BM_hdr_init)->Apply(generate_arguments_pairs);
BENCHMARK(BM_hdr_record_values)->Apply(generate_arguments_pairs);
BENCHMARK(BM_hdr_value_at_percentile)->Apply(generate_arguments_pairs);
BENCHMARK(BM_hdr_value_at_percentile_given_array)
    ->Apply(generate_arguments_pairs);
BENCHMARK(BM_hdr_value_at_percentiles_given_array)
    ->Apply(generate_arguments_pairs);
BENCHMARK_MAIN();