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
Register()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
nsec()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
SetBenchmarkBytesProcessed(int64_t x)47 void SetBenchmarkBytesProcessed(int64_t x) {
48 bytes = x;
49 }
50
StopBenchmarkTiming()51 void StopBenchmarkTiming() {
52 if(t0 != 0)
53 ns += nsec() - t0;
54 t0 = 0;
55 }
56
StartBenchmarkTiming()57 void StartBenchmarkTiming() {
58 if(t0 == 0)
59 t0 = nsec();
60 }
61
SetBenchmarkItemsProcessed(int n)62 void SetBenchmarkItemsProcessed(int n) {
63 items = n;
64 }
65
BenchmarkMemoryUsage()66 void BenchmarkMemoryUsage() {
67 // TODO(rsc): Implement.
68 }
69
NumCPUs()70 int NumCPUs() {
71 return static_cast<int>(std::thread::hardware_concurrency());
72 }
73
runN(Benchmark * b,int n,int siz)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
round(int n)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
RunBench(Benchmark * b,int nthread,int siz)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
match(const char * name,int argc,const char ** argv)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
main(int argc,const char ** argv)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
162