re2/util/benchmark.cc
Go to the documentation of this file.
1 // Copyright 2009 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <algorithm>
9 #include <chrono>
10 
11 #include "util/benchmark.h"
12 #include "util/flags.h"
13 #include "re2/re2.h"
14 
15 #ifdef _WIN32
16 #define snprintf _snprintf
17 #endif
18 
20 
21 static Benchmark* benchmarks[10000];
22 static int nbenchmarks;
23 
24 void Benchmark::Register() {
25  lo_ = std::max(1, lo_);
26  hi_ = std::max(lo_, hi_);
27  benchmarks[nbenchmarks++] = this;
28 }
29 
30 static int64_t nsec() {
31  return std::chrono::duration_cast<std::chrono::nanoseconds>(
32  std::chrono::steady_clock::now().time_since_epoch())
33  .count();
34 }
35 
36 static int64_t t0;
37 static int64_t ns;
38 static int64_t bytes;
39 static int64_t items;
40 
42  if (t0 == 0) {
43  t0 = nsec();
44  }
45 }
46 
48  if (t0 != 0) {
49  ns += nsec() - t0;
50  t0 = 0;
51  }
52 }
53 
55 
57 
58 static void RunFunc(Benchmark* b, int iters, int arg) {
59  t0 = nsec();
60  ns = 0;
61  bytes = 0;
62  items = 0;
63  b->func()(iters, arg);
65 }
66 
67 static int round(int n) {
68  int base = 1;
69  while (base * 10 < n) base *= 10;
70  if (n < 2 * base) return 2 * base;
71  if (n < 5 * base) return 5 * base;
72  return 10 * base;
73 }
74 
75 static void RunBench(Benchmark* b, int arg) {
76  int iters, last;
77 
78  // Run once just in case it's expensive.
79  iters = 1;
80  RunFunc(b, iters, arg);
81  while (ns < (int)1e9 && iters < (int)1e9) {
82  last = iters;
83  if (ns / iters == 0) {
84  iters = (int)1e9;
85  } else {
86  iters = (int)1e9 / static_cast<int>(ns / iters);
87  }
88  iters = std::max(last + 1, std::min(iters + iters / 2, 100 * last));
89  iters = round(iters);
90  RunFunc(b, iters, arg);
91  }
92 
93  char mb[100];
94  char suf[100];
95  mb[0] = '\0';
96  suf[0] = '\0';
97  if (ns > 0 && bytes > 0)
98  snprintf(mb, sizeof mb, "\t%7.2f MB/s",
99  ((double)bytes / 1e6) / ((double)ns / 1e9));
100  if (b->has_arg()) {
101  if (arg >= (1 << 20)) {
102  snprintf(suf, sizeof suf, "/%dM", arg / (1 << 20));
103  } else if (arg >= (1 << 10)) {
104  snprintf(suf, sizeof suf, "/%dK", arg / (1 << 10));
105  } else {
106  snprintf(suf, sizeof suf, "/%d", arg);
107  }
108  }
109  printf("%s%s\t%8d\t%10lld ns/op%s\n", b->name(), suf, iters,
110  (long long)ns / iters, mb);
111  fflush(stdout);
112 }
113 
114 static bool WantBench(const char* name, int argc, const char** argv) {
115  if (argc == 1) return true;
116  for (int i = 1; i < argc; i++) {
117  if (RE2::PartialMatch(name, argv[i]))
118  return true;
119  }
120  return false;
121 }
122 
123 int main(int argc, const char** argv) {
124  for (int i = 0; i < nbenchmarks; i++) {
125  Benchmark* b = benchmarks[i];
126  if (!WantBench(b->name(), argc, argv))
127  continue;
128  for (int arg = b->lo(); arg <= b->hi(); arg <<= 1)
129  RunBench(b, arg);
130  }
131 }
nsec
static int64_t nsec()
Definition: re2/util/benchmark.cc:30
SetBenchmarkBytesProcessed
void SetBenchmarkBytesProcessed(int64_t b)
Definition: re2/util/benchmark.cc:54
now
static double now(void)
Definition: test/core/fling/client.cc:130
items
static int64_t items
Definition: re2/util/benchmark.cc:39
benchmarks
Definition: third_party/bloaty/third_party/protobuf/benchmarks/__init__.py:1
demumble_test.stdout
stdout
Definition: demumble_test.py:38
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
t0
static int64_t t0
Definition: re2/util/benchmark.cc:36
setup.name
name
Definition: setup.py:542
StopBenchmarkTiming
void StopBenchmarkTiming()
Definition: re2/util/benchmark.cc:47
bytes
static int64_t bytes
Definition: re2/util/benchmark.cc:38
WantBench
static bool WantBench(const char *name, int argc, const char **argv)
Definition: re2/util/benchmark.cc:114
xds_interop_client.int
int
Definition: xds_interop_client.py:113
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
arg
Definition: cmdline.cc:40
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
min
#define min(a, b)
Definition: qsort.h:83
nbenchmarks
static int nbenchmarks
Definition: re2/util/benchmark.cc:22
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
stdint.h
RunFunc
static void RunFunc(Benchmark *b, int iters, int arg)
Definition: re2/util/benchmark.cc:58
round
static int round(int n)
Definition: re2/util/benchmark.cc:67
SetBenchmarkItemsProcessed
void SetBenchmarkItemsProcessed(int64_t i)
Definition: re2/util/benchmark.cc:56
arg
struct arg arg
main
int main(int argc, const char **argv)
Definition: re2/util/benchmark.cc:123
ns
static int64_t ns
Definition: re2/util/benchmark.cc:37
compare.Benchmark
def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False)
Definition: upb/benchmarks/compare.py:56
StartBenchmarkTiming
void StartBenchmarkTiming()
Definition: re2/util/benchmark.cc:41
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
RunBench
static void RunBench(Benchmark *b, int arg)
Definition: re2/util/benchmark.cc:75


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:46