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.
6 #include "util/flags.h"
7 #include "util/benchmark.h"
10 DEFINE_string(test_tmpdir
, "/var/tmp", "temp directory");
12 using testing::Benchmark
;
15 static Benchmark
* benchmarks
[10000];
16 static int nbenchmarks
;
18 void Benchmark::Register() {
19 benchmarks
[nbenchmarks
] = this;
29 if(gettimeofday(&tv
, 0) < 0)
31 return (int64
)tv
.tv_sec
*1000*1000*1000 + tv
.tv_usec
*1000;
39 void SetBenchmarkBytesProcessed(long long x
) {
43 void StopBenchmarkTiming() {
49 void StartBenchmarkTiming() {
54 void SetBenchmarkItemsProcessed(int n
) {
58 void BenchmarkMemoryUsage() {
59 // TODO(rsc): Implement.
66 static void runN(Benchmark
*b
, int n
, int siz
) {
76 fprintf(stderr
, "%s: missing function\n", b
->name
);
83 static int round(int n
) {
95 void RunBench(Benchmark
* b
, int nthread
, int siz
) {
98 // TODO(rsc): Threaded benchmarks.
102 // run once in case it's expensive
105 while(ns
< (int)1e9
&& n
< (int)1e9
) {
112 n
= max(last
+1, min(n
+n
/2, 100*last
));
121 if(ns
> 0 && bytes
> 0)
122 snprintf(mb
, sizeof mb
, "\t%7.2f MB/s", ((double)bytes
/1e6
)/((double)ns
/1e9
));
123 if(b
->fnr
|| b
->lo
!= b
->hi
) {
125 snprintf(suf
, sizeof suf
, "/%dM", siz
/(1<<20));
126 else if(siz
>= (1<<10))
127 snprintf(suf
, sizeof suf
, "/%dK", siz
/(1<<10));
129 snprintf(suf
, sizeof suf
, "/%d", siz
);
131 printf("%s%s\t%8lld\t%10lld ns/op%s\n", b
->name
, suf
, (long long)n
, (long long)ns
/n
, mb
);
135 static int match(const char* name
, int argc
, const char** argv
) {
138 for(int i
= 1; i
< argc
; i
++)
139 if(RE2::PartialMatch(name
, argv
[i
]))
144 int main(int argc
, const char** argv
) {
145 for(int i
= 0; i
< nbenchmarks
; i
++) {
146 Benchmark
* b
= benchmarks
[i
];
147 if(match(b
->name
, argc
, argv
))
148 for(int j
= b
->threadlo
; j
<= b
->threadhi
; j
++)
149 for(int k
= max(b
->lo
, 1); k
<= max(b
->hi
, 1); k
<<=1)