blob: e922e1e7213c4aaa406500c01b33387405b7b2ba [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
Colin Cross7b9df192013-11-15 14:34:22 -080017#include <benchmark.h>
Elliott Hughes7be369d2012-11-08 15:37:43 -080018
19#include <regex.h>
20#include <stdio.h>
21#include <stdlib.h>
Elliott Hughes212e0e32014-12-01 16:43:51 -080022#include <time.h>
Elliott Hughes7be369d2012-11-08 15:37:43 -080023
24#include <string>
25#include <map>
26
Serban Constantinescu282e2322013-10-22 11:30:12 +010027#include <inttypes.h>
28
Elliott Hughes1728b232014-05-14 10:02:03 -070029static int64_t g_bytes_processed;
30static int64_t g_benchmark_total_time_ns;
31static int64_t g_benchmark_start_time_ns;
Colin Cross7b9df192013-11-15 14:34:22 -080032static int g_name_column_width = 20;
Elliott Hughes7be369d2012-11-08 15:37:43 -080033
34typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap;
35typedef BenchmarkMap::iterator BenchmarkMapIt;
Colin Cross7b9df192013-11-15 14:34:22 -080036
37static BenchmarkMap& Benchmarks() {
38 static BenchmarkMap benchmarks;
39 return benchmarks;
40}
Elliott Hughes7be369d2012-11-08 15:37:43 -080041
42static int Round(int n) {
43 int base = 1;
44 while (base*10 < n) {
45 base *= 10;
46 }
47 if (n < 2*base) {
48 return 2*base;
49 }
50 if (n < 5*base) {
51 return 5*base;
52 }
53 return 10*base;
54}
55
56static int64_t NanoTime() {
57 struct timespec t;
58 t.tv_sec = t.tv_nsec = 0;
59 clock_gettime(CLOCK_MONOTONIC, &t);
60 return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
61}
62
63namespace testing {
64
65Benchmark* Benchmark::Arg(int arg) {
66 args_.push_back(arg);
67 return this;
68}
69
Elliott Hughes9edb3e02013-02-06 15:47:09 -080070const char* Benchmark::Name() {
71 return name_;
72}
73
Elliott Hughes7be369d2012-11-08 15:37:43 -080074bool Benchmark::ShouldRun(int argc, char* argv[]) {
75 if (argc == 1) {
76 return true; // With no arguments, we run all benchmarks.
77 }
78 // Otherwise, we interpret each argument as a regular expression and
79 // see if any of our benchmarks match.
80 for (int i = 1; i < argc; i++) {
81 regex_t re;
82 if (regcomp(&re, argv[i], 0) != 0) {
83 fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]);
84 exit(EXIT_FAILURE);
85 }
86 int match = regexec(&re, name_, 0, NULL, 0);
87 regfree(&re);
88 if (match != REG_NOMATCH) {
89 return true;
90 }
91 }
92 return false;
93}
94
95void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) {
96 name_ = name;
97 fn_ = fn;
98 fn_range_ = fn_range;
99
100 if (fn_ == NULL && fn_range_ == NULL) {
101 fprintf(stderr, "%s: missing function\n", name_);
102 exit(EXIT_FAILURE);
103 }
104
Colin Cross7b9df192013-11-15 14:34:22 -0800105 Benchmarks().insert(std::make_pair(name, this));
Elliott Hughes7be369d2012-11-08 15:37:43 -0800106}
107
108void Benchmark::Run() {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800109 if (fn_ != NULL) {
110 RunWithArg(0);
111 } else {
112 if (args_.empty()) {
113 fprintf(stderr, "%s: no args!\n", name_);
114 exit(EXIT_FAILURE);
115 }
116 for (size_t i = 0; i < args_.size(); ++i) {
117 RunWithArg(args_[i]);
118 }
Elliott Hughes7be369d2012-11-08 15:37:43 -0800119 }
120}
121
122void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700123 g_bytes_processed = 0;
124 g_benchmark_total_time_ns = 0;
125 g_benchmark_start_time_ns = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800126 if (fn_ != NULL) {
127 fn_(iterations);
128 } else {
129 fn_range_(iterations, arg);
130 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700131 if (g_benchmark_start_time_ns != 0) {
132 g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800133 }
134}
135
136void Benchmark::RunWithArg(int arg) {
Colin Crossa7635042013-11-27 17:37:54 -0800137 // Run once in case it's expensive.
Elliott Hughes7be369d2012-11-08 15:37:43 -0800138 int iterations = 1;
Colin Crossa7635042013-11-27 17:37:54 -0800139 int64_t realStartTime = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800140 RunRepeatedlyWithArg(iterations, arg);
Colin Crossa7635042013-11-27 17:37:54 -0800141 int64_t realTotalTime = NanoTime() - realStartTime;
142 while (realTotalTime < 1e9 && iterations < 1e8) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800143 int last = iterations;
Colin Crossa7635042013-11-27 17:37:54 -0800144 if (realTotalTime/iterations == 0) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800145 iterations = 1e9;
146 } else {
Colin Crossa7635042013-11-27 17:37:54 -0800147 iterations = 1e9 / (realTotalTime/iterations);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800148 }
149 iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last));
150 iterations = Round(iterations);
Colin Crossa7635042013-11-27 17:37:54 -0800151 realStartTime = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800152 RunRepeatedlyWithArg(iterations, arg);
Colin Crossa7635042013-11-27 17:37:54 -0800153 realTotalTime = NanoTime() - realStartTime;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800154 }
155
156 char throughput[100];
157 throughput[0] = '\0';
Elliott Hughes1728b232014-05-14 10:02:03 -0700158 if (g_benchmark_total_time_ns > 0 && g_bytes_processed > 0) {
159 double mib_processed = static_cast<double>(g_bytes_processed)/1e6;
160 double seconds = static_cast<double>(g_benchmark_total_time_ns)/1e9;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800161 snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds);
162 }
163
164 char full_name[100];
165 if (fn_range_ != NULL) {
166 if (arg >= (1<<20)) {
167 snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20));
168 } else if (arg >= (1<<10)) {
169 snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10));
170 } else {
171 snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg);
172 }
173 } else {
174 snprintf(full_name, sizeof(full_name), "%s", name_);
175 }
176
Elliott Hughesc0eed722014-06-11 16:48:29 -0700177 printf("%-*s %10d %10" PRId64 "%s\n", g_name_column_width, full_name,
Elliott Hughes1728b232014-05-14 10:02:03 -0700178 iterations, g_benchmark_total_time_ns/iterations, throughput);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800179 fflush(stdout);
180}
181
182} // namespace testing
183
184void SetBenchmarkBytesProcessed(int64_t x) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700185 g_bytes_processed = x;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800186}
187
188void StopBenchmarkTiming() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700189 if (g_benchmark_start_time_ns != 0) {
190 g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800191 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700192 g_benchmark_start_time_ns = 0;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800193}
194
195void StartBenchmarkTiming() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700196 if (g_benchmark_start_time_ns == 0) {
197 g_benchmark_start_time_ns = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800198 }
199}
200
201int main(int argc, char* argv[]) {
Colin Cross7b9df192013-11-15 14:34:22 -0800202 if (Benchmarks().empty()) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800203 fprintf(stderr, "No benchmarks registered!\n");
Elliott Hughes7be369d2012-11-08 15:37:43 -0800204 exit(EXIT_FAILURE);
205 }
206
Colin Cross7b9df192013-11-15 14:34:22 -0800207 for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) {
Elliott Hughes5ab51d02014-06-12 12:52:58 -0700208 int name_width = static_cast<int>(strlen(it->second->Name()));
209 g_name_column_width = std::max(g_name_column_width, name_width);
Elliott Hughesc0eed722014-06-11 16:48:29 -0700210 }
211
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800212 bool need_header = true;
Colin Cross7b9df192013-11-15 14:34:22 -0800213 for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800214 ::testing::Benchmark* b = it->second;
215 if (b->ShouldRun(argc, argv)) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800216 if (need_header) {
Elliott Hughesc0eed722014-06-11 16:48:29 -0700217 printf("%-*s %10s %10s\n", g_name_column_width, "", "iterations", "ns/op");
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800218 fflush(stdout);
219 need_header = false;
220 }
Elliott Hughes7be369d2012-11-08 15:37:43 -0800221 b->Run();
222 }
223 }
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800224
225 if (need_header) {
226 fprintf(stderr, "No matching benchmarks!\n");
227 fprintf(stderr, "Available benchmarks:\n");
Colin Cross7b9df192013-11-15 14:34:22 -0800228 for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800229 fprintf(stderr, " %s\n", it->second->Name());
230 }
231 exit(EXIT_FAILURE);
232 }
233
Elliott Hughes7be369d2012-11-08 15:37:43 -0800234 return 0;
235}