Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 17 | #include <benchmark.h> |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 18 | |
| 19 | #include <regex.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 22 | #include <time.h> |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 23 | |
| 24 | #include <string> |
| 25 | #include <map> |
| 26 | |
Serban Constantinescu | 282e232 | 2013-10-22 11:30:12 +0100 | [diff] [blame] | 27 | #include <inttypes.h> |
| 28 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 29 | static int64_t g_bytes_processed; |
| 30 | static int64_t g_benchmark_total_time_ns; |
| 31 | static int64_t g_benchmark_start_time_ns; |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 32 | static int g_name_column_width = 20; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 33 | |
| 34 | typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap; |
| 35 | typedef BenchmarkMap::iterator BenchmarkMapIt; |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 36 | |
| 37 | static BenchmarkMap& Benchmarks() { |
| 38 | static BenchmarkMap benchmarks; |
| 39 | return benchmarks; |
| 40 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 41 | |
| 42 | static 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 | |
| 56 | static 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 | |
| 63 | namespace testing { |
| 64 | |
| 65 | Benchmark* Benchmark::Arg(int arg) { |
| 66 | args_.push_back(arg); |
| 67 | return this; |
| 68 | } |
| 69 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 70 | const char* Benchmark::Name() { |
| 71 | return name_; |
| 72 | } |
| 73 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 74 | bool 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 | |
| 95 | void 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 Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 105 | Benchmarks().insert(std::make_pair(name, this)); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void Benchmark::Run() { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 109 | 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 Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 123 | g_bytes_processed = 0; |
| 124 | g_benchmark_total_time_ns = 0; |
| 125 | g_benchmark_start_time_ns = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 126 | if (fn_ != NULL) { |
| 127 | fn_(iterations); |
| 128 | } else { |
| 129 | fn_range_(iterations, arg); |
| 130 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 131 | if (g_benchmark_start_time_ns != 0) { |
| 132 | g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | void Benchmark::RunWithArg(int arg) { |
| 137 | // run once in case it's expensive |
| 138 | int iterations = 1; |
| 139 | RunRepeatedlyWithArg(iterations, arg); |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 140 | while (g_benchmark_total_time_ns < 1e9 && iterations < 1e9) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 141 | int last = iterations; |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 142 | if (g_benchmark_total_time_ns/iterations == 0) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 143 | iterations = 1e9; |
| 144 | } else { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 145 | iterations = 1e9 / (g_benchmark_total_time_ns/iterations); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 146 | } |
| 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 Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 154 | 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 Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 157 | 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 Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 173 | printf("%-*s %10d %10" PRId64 "%s\n", g_name_column_width, full_name, |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 174 | iterations, g_benchmark_total_time_ns/iterations, throughput); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 175 | fflush(stdout); |
| 176 | } |
| 177 | |
| 178 | } // namespace testing |
| 179 | |
| 180 | void SetBenchmarkBytesProcessed(int64_t x) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 181 | g_bytes_processed = x; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void StopBenchmarkTiming() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 185 | if (g_benchmark_start_time_ns != 0) { |
| 186 | g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 187 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 188 | g_benchmark_start_time_ns = 0; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void StartBenchmarkTiming() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 192 | if (g_benchmark_start_time_ns == 0) { |
| 193 | g_benchmark_start_time_ns = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | int main(int argc, char* argv[]) { |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 198 | if (Benchmarks().empty()) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 199 | fprintf(stderr, "No benchmarks registered!\n"); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 200 | exit(EXIT_FAILURE); |
| 201 | } |
| 202 | |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 203 | for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) { |
Elliott Hughes | 5ab51d0 | 2014-06-12 12:52:58 -0700 | [diff] [blame] | 204 | int name_width = static_cast<int>(strlen(it->second->Name())); |
| 205 | g_name_column_width = std::max(g_name_column_width, name_width); |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 208 | bool need_header = true; |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 209 | for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 210 | ::testing::Benchmark* b = it->second; |
| 211 | if (b->ShouldRun(argc, argv)) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 212 | if (need_header) { |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 213 | printf("%-*s %10s %10s\n", g_name_column_width, "", "iterations", "ns/op"); |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 214 | fflush(stdout); |
| 215 | need_header = false; |
| 216 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 217 | b->Run(); |
| 218 | } |
| 219 | } |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 220 | |
| 221 | if (need_header) { |
| 222 | fprintf(stderr, "No matching benchmarks!\n"); |
| 223 | fprintf(stderr, "Available benchmarks:\n"); |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame^] | 224 | for (BenchmarkMapIt it = Benchmarks().begin(); it != Benchmarks().end(); ++it) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 225 | fprintf(stderr, " %s\n", it->second->Name()); |
| 226 | } |
| 227 | exit(EXIT_FAILURE); |
| 228 | } |
| 229 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 230 | return 0; |
| 231 | } |