bloaty/third_party/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 #include <thread>
11 
12 #include "util/util.h"
13 #include "util/flags.h"
14 #include "util/benchmark.h"
15 #include "re2/re2.h"
16 
17 DEFINE_string(test_tmpdir, "/var/tmp", "temp directory");
18 
19 #ifdef _WIN32
20 #define snprintf _snprintf
21 #endif
22 
23 using testing::Benchmark;
24 
25 static Benchmark* benchmarks[10000];
26 static int nbenchmarks;
27 
28 void Benchmark::Register() {
29  benchmarks[nbenchmarks] = this;
30  if(lo < 1)
31  lo = 1;
32  if(hi < lo)
33  hi = lo;
34  nbenchmarks++;
35 }
36 
37 static int64_t nsec() {
38  return std::chrono::duration_cast<std::chrono::nanoseconds>(
39  std::chrono::steady_clock::now().time_since_epoch()).count();
40 }
41 
42 static int64_t bytes;
43 static int64_t ns;
44 static int64_t t0;
45 static int64_t items;
46 
48  bytes = x;
49 }
50 
52  if(t0 != 0)
53  ns += nsec() - t0;
54  t0 = 0;
55 }
56 
58  if(t0 == 0)
59  t0 = nsec();
60 }
61 
63  items = n;
64 }
65 
67  // TODO(rsc): Implement.
68 }
69 
70 int NumCPUs() {
71  return static_cast<int>(std::thread::hardware_concurrency());
72 }
73 
74 static void runN(Benchmark *b, int n, int siz) {
75  bytes = 0;
76  items = 0;
77  ns = 0;
78  t0 = nsec();
79  if(b->fn)
80  b->fn(n);
81  else if(b->fnr)
82  b->fnr(n, siz);
83  else {
84  fprintf(stderr, "%s: missing function\n", b->name);
85  abort();
86  }
87  if(t0 != 0)
88  ns += nsec() - t0;
89 }
90 
91 static int round(int n) {
92  int base = 1;
93 
94  while(base*10 < n)
95  base *= 10;
96  if(n < 2*base)
97  return 2*base;
98  if(n < 5*base)
99  return 5*base;
100  return 10*base;
101 }
102 
103 void RunBench(Benchmark* b, int nthread, int siz) {
104  int n, last;
105 
106  // TODO(rsc): Threaded benchmarks.
107  if(nthread != 1)
108  return;
109 
110  // run once in case it's expensive
111  n = 1;
112  runN(b, n, siz);
113  while(ns < (int)1e9 && n < (int)1e9) {
114  last = n;
115  if(ns/n == 0)
116  n = (int)1e9;
117  else
118  n = (int)1e9 / static_cast<int>(ns/n);
119 
120  n = std::max(last+1, std::min(n+n/2, 100*last));
121  n = round(n);
122  runN(b, n, siz);
123  }
124 
125  char mb[100];
126  char suf[100];
127  mb[0] = '\0';
128  suf[0] = '\0';
129  if(ns > 0 && bytes > 0)
130  snprintf(mb, sizeof mb, "\t%7.2f MB/s", ((double)bytes/1e6)/((double)ns/1e9));
131  if(b->fnr || b->lo != b->hi) {
132  if(siz >= (1<<20))
133  snprintf(suf, sizeof suf, "/%dM", siz/(1<<20));
134  else if(siz >= (1<<10))
135  snprintf(suf, sizeof suf, "/%dK", siz/(1<<10));
136  else
137  snprintf(suf, sizeof suf, "/%d", siz);
138  }
139  printf("%s%s\t%8lld\t%10lld ns/op%s\n", b->name, suf, (long long)n, (long long)ns/n, mb);
140  fflush(stdout);
141 }
142 
143 static int match(const char* name, int argc, const char** argv) {
144  if(argc == 1)
145  return 1;
146  for(int i = 1; i < argc; i++)
147  if(RE2::PartialMatch(name, argv[i]))
148  return 1;
149  return 0;
150 }
151 
152 int main(int argc, const char** argv) {
153  for(int i = 0; i < nbenchmarks; i++) {
154  Benchmark* b = benchmarks[i];
155  if(match(b->name, argc, argv))
156  for(int j = b->threadlo; j <= b->threadhi; j++)
157  for(int k = std::max(b->lo, 1); k <= std::max(b->hi, 1); k<<=1)
158  RunBench(b, j, k);
159  }
160 }
161 
StopBenchmarkTiming
void StopBenchmarkTiming()
Definition: bloaty/third_party/re2/util/benchmark.cc:51
SetBenchmarkItemsProcessed
void SetBenchmarkItemsProcessed(int n)
Definition: bloaty/third_party/re2/util/benchmark.cc:62
now
static double now(void)
Definition: test/core/fling/client.cc:130
benchmarks
Definition: third_party/bloaty/third_party/protobuf/benchmarks/__init__.py:1
demumble_test.stdout
stdout
Definition: demumble_test.py:38
NumCPUs
int NumCPUs()
Definition: bloaty/third_party/re2/util/benchmark.cc:70
nsec
static int64_t nsec()
Definition: bloaty/third_party/re2/util/benchmark.cc:37
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
setup.name
name
Definition: setup.py:542
testing::Benchmark::hi
int hi
Definition: bloaty/third_party/re2/util/benchmark.h:16
BenchmarkMemoryUsage
void BenchmarkMemoryUsage()
Definition: bloaty/third_party/re2/util/benchmark.cc:66
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
RunBench
void RunBench(Benchmark *b, int nthread, int siz)
Definition: bloaty/third_party/re2/util/benchmark.cc:103
runN
static void runN(Benchmark *b, int n, int siz)
Definition: bloaty/third_party/re2/util/benchmark.cc:74
bytes
static int64_t bytes
Definition: bloaty/third_party/re2/util/benchmark.cc:42
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
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
t0
static int64_t t0
Definition: bloaty/third_party/re2/util/benchmark.cc:44
round
static int round(int n)
Definition: bloaty/third_party/re2/util/benchmark.cc:91
items
static int64_t items
Definition: bloaty/third_party/re2/util/benchmark.cc:45
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
gen_synthetic_protos.base
base
Definition: gen_synthetic_protos.py:31
min
#define min(a, b)
Definition: qsort.h:83
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
match
static int match(const char *name, int argc, const char **argv)
Definition: bloaty/third_party/re2/util/benchmark.cc:143
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
stdint.h
nbenchmarks
static int nbenchmarks
Definition: bloaty/third_party/re2/util/benchmark.cc:26
DEFINE_string
DEFINE_string(test_tmpdir, "/var/tmp", "temp directory")
SetBenchmarkBytesProcessed
void SetBenchmarkBytesProcessed(int64_t x)
Definition: bloaty/third_party/re2/util/benchmark.cc:47
main
int main(int argc, const char **argv)
Definition: bloaty/third_party/re2/util/benchmark.cc:152
testing::Benchmark
Definition: bloaty/third_party/re2/util/benchmark.h:11
testing::Benchmark::lo
int lo
Definition: bloaty/third_party/re2/util/benchmark.h:15
StartBenchmarkTiming
void StartBenchmarkTiming()
Definition: bloaty/third_party/re2/util/benchmark.cc:57
ns
static int64_t ns
Definition: bloaty/third_party/re2/util/benchmark.cc:43
compare.Benchmark
def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False)
Definition: upb/benchmarks/compare.py:56
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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