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 | |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame^] | 34 | typedef std::vector<::testing::Benchmark*> BenchmarkList; |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame^] | 36 | static BenchmarkList& Benchmarks() { |
| 37 | static BenchmarkList benchmarks; |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame] | 38 | return benchmarks; |
| 39 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 40 | |
| 41 | static int Round(int n) { |
| 42 | int base = 1; |
| 43 | while (base*10 < n) { |
| 44 | base *= 10; |
| 45 | } |
| 46 | if (n < 2*base) { |
| 47 | return 2*base; |
| 48 | } |
| 49 | if (n < 5*base) { |
| 50 | return 5*base; |
| 51 | } |
| 52 | return 10*base; |
| 53 | } |
| 54 | |
| 55 | static int64_t NanoTime() { |
| 56 | struct timespec t; |
| 57 | t.tv_sec = t.tv_nsec = 0; |
| 58 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 59 | return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; |
| 60 | } |
| 61 | |
| 62 | namespace testing { |
| 63 | |
| 64 | Benchmark* Benchmark::Arg(int arg) { |
| 65 | args_.push_back(arg); |
| 66 | return this; |
| 67 | } |
| 68 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 69 | const char* Benchmark::Name() { |
| 70 | return name_; |
| 71 | } |
| 72 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 73 | bool Benchmark::ShouldRun(int argc, char* argv[]) { |
| 74 | if (argc == 1) { |
| 75 | return true; // With no arguments, we run all benchmarks. |
| 76 | } |
| 77 | // Otherwise, we interpret each argument as a regular expression and |
| 78 | // see if any of our benchmarks match. |
| 79 | for (int i = 1; i < argc; i++) { |
| 80 | regex_t re; |
| 81 | if (regcomp(&re, argv[i], 0) != 0) { |
| 82 | fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]); |
| 83 | exit(EXIT_FAILURE); |
| 84 | } |
| 85 | int match = regexec(&re, name_, 0, NULL, 0); |
| 86 | regfree(&re); |
| 87 | if (match != REG_NOMATCH) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) { |
| 95 | name_ = name; |
| 96 | fn_ = fn; |
| 97 | fn_range_ = fn_range; |
| 98 | |
| 99 | if (fn_ == NULL && fn_range_ == NULL) { |
| 100 | fprintf(stderr, "%s: missing function\n", name_); |
| 101 | exit(EXIT_FAILURE); |
| 102 | } |
| 103 | |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame^] | 104 | Benchmarks().push_back(this); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void Benchmark::Run() { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 108 | if (fn_ != NULL) { |
| 109 | RunWithArg(0); |
| 110 | } else { |
| 111 | if (args_.empty()) { |
| 112 | fprintf(stderr, "%s: no args!\n", name_); |
| 113 | exit(EXIT_FAILURE); |
| 114 | } |
| 115 | for (size_t i = 0; i < args_.size(); ++i) { |
| 116 | RunWithArg(args_[i]); |
| 117 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 122 | g_bytes_processed = 0; |
| 123 | g_benchmark_total_time_ns = 0; |
| 124 | g_benchmark_start_time_ns = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 125 | if (fn_ != NULL) { |
| 126 | fn_(iterations); |
| 127 | } else { |
| 128 | fn_range_(iterations, arg); |
| 129 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 130 | if (g_benchmark_start_time_ns != 0) { |
| 131 | g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | void Benchmark::RunWithArg(int arg) { |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 136 | // Run once in case it's expensive. |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 137 | int iterations = 1; |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 138 | int64_t realStartTime = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 139 | RunRepeatedlyWithArg(iterations, arg); |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 140 | int64_t realTotalTime = NanoTime() - realStartTime; |
| 141 | while (realTotalTime < 1e9 && iterations < 1e8) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 142 | int last = iterations; |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 143 | if (realTotalTime/iterations == 0) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 144 | iterations = 1e9; |
| 145 | } else { |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 146 | iterations = 1e9 / (realTotalTime/iterations); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 147 | } |
| 148 | iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last)); |
| 149 | iterations = Round(iterations); |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 150 | realStartTime = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 151 | RunRepeatedlyWithArg(iterations, arg); |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 152 | realTotalTime = NanoTime() - realStartTime; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | char throughput[100]; |
| 156 | throughput[0] = '\0'; |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 157 | if (g_benchmark_total_time_ns > 0 && g_bytes_processed > 0) { |
| 158 | double mib_processed = static_cast<double>(g_bytes_processed)/1e6; |
| 159 | double seconds = static_cast<double>(g_benchmark_total_time_ns)/1e9; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 160 | snprintf(throughput, sizeof(throughput), " %8.2f MiB/s", mib_processed/seconds); |
| 161 | } |
| 162 | |
| 163 | char full_name[100]; |
| 164 | if (fn_range_ != NULL) { |
| 165 | if (arg >= (1<<20)) { |
| 166 | snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20)); |
| 167 | } else if (arg >= (1<<10)) { |
| 168 | snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10)); |
| 169 | } else { |
| 170 | snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg); |
| 171 | } |
| 172 | } else { |
| 173 | snprintf(full_name, sizeof(full_name), "%s", name_); |
| 174 | } |
| 175 | |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 176 | 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] | 177 | iterations, g_benchmark_total_time_ns/iterations, throughput); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 178 | fflush(stdout); |
| 179 | } |
| 180 | |
| 181 | } // namespace testing |
| 182 | |
| 183 | void SetBenchmarkBytesProcessed(int64_t x) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 184 | g_bytes_processed = x; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void StopBenchmarkTiming() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 188 | if (g_benchmark_start_time_ns != 0) { |
| 189 | g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 190 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 191 | g_benchmark_start_time_ns = 0; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void StartBenchmarkTiming() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 195 | if (g_benchmark_start_time_ns == 0) { |
| 196 | g_benchmark_start_time_ns = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | int main(int argc, char* argv[]) { |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame] | 201 | if (Benchmarks().empty()) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 202 | fprintf(stderr, "No benchmarks registered!\n"); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 203 | exit(EXIT_FAILURE); |
| 204 | } |
| 205 | |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame^] | 206 | for (auto& b : Benchmarks()) { |
| 207 | int name_width = static_cast<int>(strlen(b->Name())); |
Elliott Hughes | 5ab51d0 | 2014-06-12 12:52:58 -0700 | [diff] [blame] | 208 | g_name_column_width = std::max(g_name_column_width, name_width); |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 211 | bool need_header = true; |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame^] | 212 | for (auto& b : Benchmarks()) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 213 | if (b->ShouldRun(argc, argv)) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 214 | if (need_header) { |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 215 | printf("%-*s %10s %10s\n", g_name_column_width, "", "iterations", "ns/op"); |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 216 | fflush(stdout); |
| 217 | need_header = false; |
| 218 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 219 | b->Run(); |
| 220 | } |
| 221 | } |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 222 | |
| 223 | if (need_header) { |
| 224 | fprintf(stderr, "No matching benchmarks!\n"); |
| 225 | fprintf(stderr, "Available benchmarks:\n"); |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame^] | 226 | for (auto& b : Benchmarks()) { |
| 227 | fprintf(stderr, " %s\n", b->Name()); |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 228 | } |
| 229 | exit(EXIT_FAILURE); |
| 230 | } |
| 231 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |