blob: 7140ab648cf635be9c850f446222ff91fafc9a1a [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) {
137 // run once in case it's expensive
138 int iterations = 1;
139 RunRepeatedlyWithArg(iterations, arg);
Elliott Hughes1728b232014-05-14 10:02:03 -0700140 while (g_benchmark_total_time_ns < 1e9 && iterations < 1e9) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800141 int last = iterations;
Elliott Hughes1728b232014-05-14 10:02:03 -0700142 if (g_benchmark_total_time_ns/iterations == 0) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800143 iterations = 1e9;
144 } else {
Elliott Hughes1728b232014-05-14 10:02:03 -0700145 iterations = 1e9 / (g_benchmark_total_time_ns/iterations);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800146 }
147 iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last));
148 iterations = Round(iterations);
149 RunRepeatedlyWithArg(iterations, arg);
150 }
151
152 char throughput[100];
153 throughput[0] = '\0';
Elliott Hughes1728b232014-05-14 10:02:03 -0700154 if (g_benchmark_total_time_ns > 0 && g_bytes_processed > 0) {
155 double mib_processed = static_cast<double>(g_bytes_processed)/1e6;
156 double seconds = static_cast<double>(g_benchmark_total_time_ns)/1e9;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800157 snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds);
158 }
159
160 char full_name[100];
161 if (fn_range_ != NULL) {
162 if (arg >= (1<<20)) {
163 snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20));
164 } else if (arg >= (1<<10)) {
165 snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10));
166 } else {
167 snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg);
168 }
169 } else {
170 snprintf(full_name, sizeof(full_name), "%s", name_);
171 }
172
Elliott Hughesc0eed722014-06-11 16:48:29 -0700173 printf("%-*s %10d %10" PRId64 "%s\n", g_name_column_width, full_name,
Elliott Hughes1728b232014-05-14 10:02:03 -0700174 iterations, g_benchmark_total_time_ns/iterations, throughput);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800175 fflush(stdout);
176}
177
178} // namespace testing
179
180void SetBenchmarkBytesProcessed(int64_t x) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700181 g_bytes_processed = x;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800182}
183
184void StopBenchmarkTiming() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700185 if (g_benchmark_start_time_ns != 0) {
186 g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800187 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700188 g_benchmark_start_time_ns = 0;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800189}
190
191void StartBenchmarkTiming() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700192 if (g_benchmark_start_time_ns == 0) {
193 g_benchmark_start_time_ns = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800194 }
195}
196
197int main(int argc, char* argv[]) {
Colin Cross7b9df192013-11-15 14:34:22 -0800198 if (Benchmarks().empty()) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800199 fprintf(stderr, "No benchmarks registered!\n");
Elliott Hughes7be369d2012-11-08 15:37:43 -0800200 exit(EXIT_FAILURE);
201 }
202
Colin Cross7b9df192013-11-15 14:34:22 -0800203 for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) {
Elliott Hughes5ab51d02014-06-12 12:52:58 -0700204 int name_width = static_cast<int>(strlen(it->second->Name()));
205 g_name_column_width = std::max(g_name_column_width, name_width);
Elliott Hughesc0eed722014-06-11 16:48:29 -0700206 }
207
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800208 bool need_header = true;
Colin Cross7b9df192013-11-15 14:34:22 -0800209 for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800210 ::testing::Benchmark* b = it->second;
211 if (b->ShouldRun(argc, argv)) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800212 if (need_header) {
Elliott Hughesc0eed722014-06-11 16:48:29 -0700213 printf("%-*s %10s %10s\n", g_name_column_width, "", "iterations", "ns/op");
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800214 fflush(stdout);
215 need_header = false;
216 }
Elliott Hughes7be369d2012-11-08 15:37:43 -0800217 b->Run();
218 }
219 }
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800220
221 if (need_header) {
222 fprintf(stderr, "No matching benchmarks!\n");
223 fprintf(stderr, "Available benchmarks:\n");
Colin Cross7b9df192013-11-15 14:34:22 -0800224 for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800225 fprintf(stderr, " %s\n", it->second->Name());
226 }
227 exit(EXIT_FAILURE);
228 }
229
Elliott Hughes7be369d2012-11-08 15:37:43 -0800230 return 0;
231}