blob: d8b8e58eb1be15acc1da721604cc53b11578b050 [file] [log] [blame]
Elliott Hughes7be369d2012-11-08 15:37:43 -08001/*
2 * Copyright (C) 2012 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
Serban Constantinescu282e2322013-10-22 11:30:12 +010026#include <inttypes.h>
27
Elliott Hughes7be369d2012-11-08 15:37:43 -080028static int64_t gBytesProcessed;
29static int64_t gBenchmarkTotalTimeNs;
30static int64_t gBenchmarkStartTimeNs;
31
32typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap;
33typedef BenchmarkMap::iterator BenchmarkMapIt;
34static BenchmarkMap 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 int64_t NanoTime() {
51 struct timespec t;
52 t.tv_sec = t.tv_nsec = 0;
53 clock_gettime(CLOCK_MONOTONIC, &t);
54 return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
55}
56
57namespace testing {
58
59Benchmark* Benchmark::Arg(int arg) {
60 args_.push_back(arg);
61 return this;
62}
63
Elliott Hughes9edb3e02013-02-06 15:47:09 -080064const char* Benchmark::Name() {
65 return name_;
66}
67
Elliott Hughes7be369d2012-11-08 15:37:43 -080068bool Benchmark::ShouldRun(int argc, char* argv[]) {
69 if (argc == 1) {
70 return true; // With no arguments, we run all benchmarks.
71 }
72 // Otherwise, we interpret each argument as a regular expression and
73 // see if any of our benchmarks match.
74 for (int i = 1; i < argc; i++) {
75 regex_t re;
76 if (regcomp(&re, argv[i], 0) != 0) {
77 fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]);
78 exit(EXIT_FAILURE);
79 }
80 int match = regexec(&re, name_, 0, NULL, 0);
81 regfree(&re);
82 if (match != REG_NOMATCH) {
83 return true;
84 }
85 }
86 return false;
87}
88
89void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) {
90 name_ = name;
91 fn_ = fn;
92 fn_range_ = fn_range;
93
94 if (fn_ == NULL && fn_range_ == NULL) {
95 fprintf(stderr, "%s: missing function\n", name_);
96 exit(EXIT_FAILURE);
97 }
98
99 gBenchmarks.insert(std::make_pair(name, this));
100}
101
102void Benchmark::Run() {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800103 if (fn_ != NULL) {
104 RunWithArg(0);
105 } else {
106 if (args_.empty()) {
107 fprintf(stderr, "%s: no args!\n", name_);
108 exit(EXIT_FAILURE);
109 }
110 for (size_t i = 0; i < args_.size(); ++i) {
111 RunWithArg(args_[i]);
112 }
Elliott Hughes7be369d2012-11-08 15:37:43 -0800113 }
114}
115
116void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) {
117 gBytesProcessed = 0;
118 gBenchmarkTotalTimeNs = 0;
119 gBenchmarkStartTimeNs = NanoTime();
120 if (fn_ != NULL) {
121 fn_(iterations);
122 } else {
123 fn_range_(iterations, arg);
124 }
125 if (gBenchmarkStartTimeNs != 0) {
126 gBenchmarkTotalTimeNs += NanoTime() - gBenchmarkStartTimeNs;
127 }
128}
129
130void Benchmark::RunWithArg(int arg) {
131 // run once in case it's expensive
132 int iterations = 1;
133 RunRepeatedlyWithArg(iterations, arg);
134 while (gBenchmarkTotalTimeNs < 1e9 && iterations < 1e9) {
135 int last = iterations;
136 if (gBenchmarkTotalTimeNs/iterations == 0) {
137 iterations = 1e9;
138 } else {
139 iterations = 1e9 / (gBenchmarkTotalTimeNs/iterations);
140 }
141 iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last));
142 iterations = Round(iterations);
143 RunRepeatedlyWithArg(iterations, arg);
144 }
145
146 char throughput[100];
147 throughput[0] = '\0';
148 if (gBenchmarkTotalTimeNs > 0 && gBytesProcessed > 0) {
149 double mib_processed = static_cast<double>(gBytesProcessed)/1e6;
150 double seconds = static_cast<double>(gBenchmarkTotalTimeNs)/1e9;
151 snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds);
152 }
153
154 char full_name[100];
155 if (fn_range_ != NULL) {
156 if (arg >= (1<<20)) {
157 snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20));
158 } else if (arg >= (1<<10)) {
159 snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10));
160 } else {
161 snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg);
162 }
163 } else {
164 snprintf(full_name, sizeof(full_name), "%s", name_);
165 }
166
Serban Constantinescu282e2322013-10-22 11:30:12 +0100167 printf("%-20s %10d %10" PRId64 "%s\n", full_name,
168 iterations, gBenchmarkTotalTimeNs/iterations, throughput);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800169 fflush(stdout);
170}
171
172} // namespace testing
173
174void SetBenchmarkBytesProcessed(int64_t x) {
175 gBytesProcessed = x;
176}
177
178void StopBenchmarkTiming() {
179 if (gBenchmarkStartTimeNs != 0) {
180 gBenchmarkTotalTimeNs += NanoTime() - gBenchmarkStartTimeNs;
181 }
182 gBenchmarkStartTimeNs = 0;
183}
184
185void StartBenchmarkTiming() {
186 if (gBenchmarkStartTimeNs == 0) {
187 gBenchmarkStartTimeNs = NanoTime();
188 }
189}
190
191int main(int argc, char* argv[]) {
192 if (gBenchmarks.empty()) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800193 fprintf(stderr, "No benchmarks registered!\n");
Elliott Hughes7be369d2012-11-08 15:37:43 -0800194 exit(EXIT_FAILURE);
195 }
196
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800197 bool need_header = true;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800198 for (BenchmarkMapIt it = gBenchmarks.begin(); it != gBenchmarks.end(); ++it) {
199 ::testing::Benchmark* b = it->second;
200 if (b->ShouldRun(argc, argv)) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800201 if (need_header) {
202 printf("%-20s %10s %10s\n", "", "iterations", "ns/op");
203 fflush(stdout);
204 need_header = false;
205 }
Elliott Hughes7be369d2012-11-08 15:37:43 -0800206 b->Run();
207 }
208 }
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800209
210 if (need_header) {
211 fprintf(stderr, "No matching benchmarks!\n");
212 fprintf(stderr, "Available benchmarks:\n");
213 for (BenchmarkMapIt it = gBenchmarks.begin(); it != gBenchmarks.end(); ++it) {
214 fprintf(stderr, " %s\n", it->second->Name());
215 }
216 exit(EXIT_FAILURE);
217 }
218
Elliott Hughes7be369d2012-11-08 15:37:43 -0800219 return 0;
220}