• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <benchmark/benchmark.h>
16 
17 #include <random>
18 
19 #include "src/core/util/tdigest.h"
20 
21 namespace grpc_core {
22 
BM_AddWithCompression(benchmark::State & state)23 static void BM_AddWithCompression(benchmark::State& state) {
24   // kNumValues is 512 with a 4k page.
25   const size_t kNumValues = sysconf(_SC_PAGE_SIZE) / sizeof(double);
26   std::vector<double> vals;
27   vals.reserve(kNumValues);
28   std::mt19937 gen(1234);
29   std::exponential_distribution<double> exp_dist;
30 
31   for (int idx = 0; idx < kNumValues; idx++) {
32     vals.push_back(exp_dist(gen));
33   }
34 
35   TDigest tdigest(/*compression=*/state.range(0));
36 
37   while (state.KeepRunningBatch(kNumValues)) {
38     for (double val : vals) {
39       tdigest.Add(val);
40     }
41   }
42 
43   state.SetItemsProcessed(state.iterations());
44 }
45 BENCHMARK(BM_AddWithCompression)->Arg(1)->Arg(10)->Arg(100)->Arg(1000);
46 
47 }  // namespace grpc_core
48 
49 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
50 // and others do not. This allows us to support both modes.
51 namespace benchmark {
RunTheBenchmarksNamespaced()52 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
53 }  // namespace benchmark
54 
main(int argc,char ** argv)55 int main(int argc, char** argv) {
56   ::benchmark::Initialize(&argc, argv);
57   benchmark::RunTheBenchmarksNamespaced();
58   return 0;
59 }
60