blob: 942e12c999e54e286f490d8cb51116ad703c8ce9 [file] [log] [blame]
Mark Salyzyn46abc522013-11-22 12:39:43 -08001/*
2 * Copyright (C) 2012-2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <benchmark.h>
18
19#include <regex.h>
20#include <stdio.h>
21#include <stdlib.h>
22
23#include <string>
24#include <map>
25#include <vector>
26
27static uint64_t gBytesProcessed;
28static uint64_t gBenchmarkTotalTimeNs;
29static uint64_t gBenchmarkTotalTimeNsSquared;
30static uint64_t gBenchmarkNum;
31static uint64_t gBenchmarkStartTimeNs;
32
33typedef std::vector< ::testing::Benchmark* > BenchmarkList;
34static BenchmarkList* gBenchmarks;
35
36static int Round(int n) {
37 int base = 1;
38 while (base*10 < n) {
39 base *= 10;
40 }
41 if (n < 2*base) {
42 return 2*base;
43 }
44 if (n < 5*base) {
45 return 5*base;
46 }
47 return 10*base;
48}
49
50static uint64_t NanoTime() {
51 struct timespec t;
52 t.tv_sec = t.tv_nsec = 0;
53 clock_gettime(CLOCK_MONOTONIC, &t);
54 return static_cast<uint64_t>(t.tv_sec) * 1000000000ULL + t.tv_nsec;
55}
56
57namespace testing {
58
59int PrettyPrintInt(char* str, int len, unsigned int arg)
60{
61 if (arg >= (1<<30) && arg % (1<<30) == 0) {
62 return snprintf(str, len, "%uGi", arg/(1<<30));
63 } else if (arg >= (1<<20) && arg % (1<<20) == 0) {
64 return snprintf(str, len, "%uMi", arg/(1<<20));
65 } else if (arg >= (1<<10) && arg % (1<<10) == 0) {
66 return snprintf(str, len, "%uKi", arg/(1<<10));
67 } else if (arg >= 1000000000 && arg % 1000000000 == 0) {
68 return snprintf(str, len, "%uG", arg/1000000000);
69 } else if (arg >= 1000000 && arg % 1000000 == 0) {
70 return snprintf(str, len, "%uM", arg/1000000);
71 } else if (arg >= 1000 && arg % 1000 == 0) {
72 return snprintf(str, len, "%uK", arg/1000);
73 } else {
74 return snprintf(str, len, "%u", arg);
75 }
76}
77
78bool ShouldRun(Benchmark* b, int argc, char* argv[]) {
79 if (argc == 1) {
80 return true; // With no arguments, we run all benchmarks.
81 }
82 // Otherwise, we interpret each argument as a regular expression and
83 // see if any of our benchmarks match.
84 for (int i = 1; i < argc; i++) {
85 regex_t re;
86 if (regcomp(&re, argv[i], 0) != 0) {
87 fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]);
88 exit(EXIT_FAILURE);
89 }
90 int match = regexec(&re, b->Name(), 0, NULL, 0);
91 regfree(&re);
92 if (match != REG_NOMATCH) {
93 return true;
94 }
95 }
96 return false;
97}
98
99void BenchmarkRegister(Benchmark* b) {
100 if (gBenchmarks == NULL) {
101 gBenchmarks = new BenchmarkList;
102 }
103 gBenchmarks->push_back(b);
104}
105
106void RunRepeatedly(Benchmark* b, int iterations) {
107 gBytesProcessed = 0;
108 ResetBenchmarkTiming();
109 uint64_t StartTimeNs = NanoTime();
110 b->RunFn(iterations);
111 // Catch us if we fail to log anything.
112 if ((gBenchmarkTotalTimeNs == 0)
113 && (StartTimeNs != 0)
114 && (gBenchmarkStartTimeNs == 0)) {
115 gBenchmarkTotalTimeNs = NanoTime() - StartTimeNs;
116 }
117}
118
119void Run(Benchmark* b) {
120 // run once in case it's expensive
121 unsigned iterations = 1;
122 uint64_t s = NanoTime();
123 RunRepeatedly(b, iterations);
124 s = NanoTime() - s;
125 while (s < 2e9 && gBenchmarkTotalTimeNs < 1e9 && iterations < 1e9) {
126 unsigned last = iterations;
127 if (gBenchmarkTotalTimeNs/iterations == 0) {
128 iterations = 1e9;
129 } else {
130 iterations = 1e9 / (gBenchmarkTotalTimeNs/iterations);
131 }
132 iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last));
133 iterations = Round(iterations);
134 s = NanoTime();
135 RunRepeatedly(b, iterations);
136 s = NanoTime() - s;
137 }
138
139 char throughput[100];
140 throughput[0] = '\0';
141 if (gBenchmarkTotalTimeNs > 0 && gBytesProcessed > 0) {
142 double mib_processed = static_cast<double>(gBytesProcessed)/1e6;
143 double seconds = static_cast<double>(gBenchmarkTotalTimeNs)/1e9;
144 snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds);
145 }
146
147 char full_name[100];
148 snprintf(full_name, sizeof(full_name), "%s%s%s", b->Name(),
149 b->ArgName() ? "/" : "",
150 b->ArgName() ? b->ArgName() : "");
151
152 uint64_t mean = gBenchmarkTotalTimeNs / iterations;
153 uint64_t sdev = 0;
154 if (gBenchmarkNum == iterations) {
155 mean = gBenchmarkTotalTimeNs / gBenchmarkNum;
156 uint64_t nXvariance = gBenchmarkTotalTimeNsSquared * gBenchmarkNum
157 - (gBenchmarkTotalTimeNs * gBenchmarkTotalTimeNs);
158 sdev = (sqrt((double)nXvariance) / gBenchmarkNum / gBenchmarkNum) + 0.5;
159 }
160 if (mean > (10000 * sdev)) {
161 printf("%-25s %10llu %10llu%s\n", full_name,
162 static_cast<uint64_t>(iterations), mean, throughput);
163 } else {
164 printf("%-25s %10llu %10llu(\317\203%llu)%s\n", full_name,
165 static_cast<uint64_t>(iterations), mean, sdev, throughput);
166 }
167 fflush(stdout);
168}
169
170} // namespace testing
171
172void SetBenchmarkBytesProcessed(uint64_t x) {
173 gBytesProcessed = x;
174}
175
176void ResetBenchmarkTiming() {
177 gBenchmarkStartTimeNs = 0;
178 gBenchmarkTotalTimeNs = 0;
179 gBenchmarkTotalTimeNsSquared = 0;
180 gBenchmarkNum = 0;
181}
182
183void StopBenchmarkTiming(void) {
184 if (gBenchmarkStartTimeNs != 0) {
185 int64_t diff = NanoTime() - gBenchmarkStartTimeNs;
186 gBenchmarkTotalTimeNs += diff;
187 gBenchmarkTotalTimeNsSquared += diff * diff;
188 ++gBenchmarkNum;
189 }
190 gBenchmarkStartTimeNs = 0;
191}
192
193void StartBenchmarkTiming(void) {
194 if (gBenchmarkStartTimeNs == 0) {
195 gBenchmarkStartTimeNs = NanoTime();
196 }
197}
198
199void StopBenchmarkTiming(uint64_t NanoTime) {
200 if (gBenchmarkStartTimeNs != 0) {
201 int64_t diff = NanoTime - gBenchmarkStartTimeNs;
202 gBenchmarkTotalTimeNs += diff;
203 gBenchmarkTotalTimeNsSquared += diff * diff;
204 if (NanoTime != 0) {
205 ++gBenchmarkNum;
206 }
207 }
208 gBenchmarkStartTimeNs = 0;
209}
210
211void StartBenchmarkTiming(uint64_t NanoTime) {
212 if (gBenchmarkStartTimeNs == 0) {
213 gBenchmarkStartTimeNs = NanoTime;
214 }
215}
216
217int main(int argc, char* argv[]) {
218 if (gBenchmarks->empty()) {
219 fprintf(stderr, "No benchmarks registered!\n");
220 exit(EXIT_FAILURE);
221 }
222
223 bool need_header = true;
224 for (auto b : *gBenchmarks) {
225 if (ShouldRun(b, argc, argv)) {
226 if (need_header) {
227 printf("%-25s %10s %10s\n", "", "iterations", "ns/op");
228 fflush(stdout);
229 need_header = false;
230 }
231 Run(b);
232 }
233 }
234
235 if (need_header) {
236 fprintf(stderr, "No matching benchmarks!\n");
237 fprintf(stderr, "Available benchmarks:\n");
238 for (auto b : *gBenchmarks) {
239 fprintf(stderr, " %s\n", b->Name());
240 }
241 exit(EXIT_FAILURE);
242 }
243
244 return 0;
245}