blob: f8e85bb77210d73094a189c4984afd24b1530633 [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 Hughes1728b232014-05-14 10:02:03 -070028static int64_t g_bytes_processed;
29static int64_t g_benchmark_total_time_ns;
30static int64_t g_benchmark_start_time_ns;
Elliott Hughes7be369d2012-11-08 15:37:43 -080031
32typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap;
33typedef BenchmarkMap::iterator BenchmarkMapIt;
Elliott Hughes1728b232014-05-14 10:02:03 -070034static BenchmarkMap g_benchmarks;
Elliott Hughes7be369d2012-11-08 15:37:43 -080035
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
Elliott Hughes1728b232014-05-14 10:02:03 -070099 g_benchmarks.insert(std::make_pair(name, this));
Elliott Hughes7be369d2012-11-08 15:37:43 -0800100}
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) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700117 g_bytes_processed = 0;
118 g_benchmark_total_time_ns = 0;
119 g_benchmark_start_time_ns = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800120 if (fn_ != NULL) {
121 fn_(iterations);
122 } else {
123 fn_range_(iterations, arg);
124 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700125 if (g_benchmark_start_time_ns != 0) {
126 g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800127 }
128}
129
130void Benchmark::RunWithArg(int arg) {
131 // run once in case it's expensive
132 int iterations = 1;
133 RunRepeatedlyWithArg(iterations, arg);
Elliott Hughes1728b232014-05-14 10:02:03 -0700134 while (g_benchmark_total_time_ns < 1e9 && iterations < 1e9) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800135 int last = iterations;
Elliott Hughes1728b232014-05-14 10:02:03 -0700136 if (g_benchmark_total_time_ns/iterations == 0) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800137 iterations = 1e9;
138 } else {
Elliott Hughes1728b232014-05-14 10:02:03 -0700139 iterations = 1e9 / (g_benchmark_total_time_ns/iterations);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800140 }
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';
Elliott Hughes1728b232014-05-14 10:02:03 -0700148 if (g_benchmark_total_time_ns > 0 && g_bytes_processed > 0) {
149 double mib_processed = static_cast<double>(g_bytes_processed)/1e6;
150 double seconds = static_cast<double>(g_benchmark_total_time_ns)/1e9;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800151 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,
Elliott Hughes1728b232014-05-14 10:02:03 -0700168 iterations, g_benchmark_total_time_ns/iterations, throughput);
Elliott Hughes7be369d2012-11-08 15:37:43 -0800169 fflush(stdout);
170}
171
172} // namespace testing
173
174void SetBenchmarkBytesProcessed(int64_t x) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700175 g_bytes_processed = x;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800176}
177
178void StopBenchmarkTiming() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700179 if (g_benchmark_start_time_ns != 0) {
180 g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800181 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700182 g_benchmark_start_time_ns = 0;
Elliott Hughes7be369d2012-11-08 15:37:43 -0800183}
184
185void StartBenchmarkTiming() {
Elliott Hughes1728b232014-05-14 10:02:03 -0700186 if (g_benchmark_start_time_ns == 0) {
187 g_benchmark_start_time_ns = NanoTime();
Elliott Hughes7be369d2012-11-08 15:37:43 -0800188 }
189}
190
191int main(int argc, char* argv[]) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700192 if (g_benchmarks.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 Hughes1728b232014-05-14 10:02:03 -0700198 for (BenchmarkMapIt it = g_benchmarks.begin(); it != g_benchmarks.end(); ++it) {
Elliott Hughes7be369d2012-11-08 15:37:43 -0800199 ::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");
Elliott Hughes1728b232014-05-14 10:02:03 -0700213 for (BenchmarkMapIt it = g_benchmarks.begin(); it != g_benchmarks.end(); ++it) {
Elliott Hughes9edb3e02013-02-06 15:47:09 -0800214 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}