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> |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 25 | #include <vector> |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 41 | // Similar to the code in art, but supporting both binary and decimal prefixes. |
| 42 | static std::string PrettyInt(uint64_t count, size_t base) { |
| 43 | if (base != 2 && base != 10) abort(); |
| 44 | |
| 45 | // The byte thresholds at which we display amounts. A count is displayed |
| 46 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
| 47 | static const uint64_t kUnitThresholds2[] = { |
| 48 | 1024*1024*1024 /* Gi */, 2*1024*1024 /* Mi */, 3*1024 /* Ki */, 0, |
| 49 | }; |
| 50 | static const uint64_t kUnitThresholds10[] = { |
| 51 | 1000*1000*1000 /* G */, 2*1000*1000 /* M */, 3*1000 /* k */, 0, |
| 52 | }; |
| 53 | static const uint64_t kAmountPerUnit2[] = { 1024*1024*1024, 1024*1024, 1024, 1 }; |
| 54 | static const uint64_t kAmountPerUnit10[] = { 1000*1000*1000, 1000*1000, 1000, 1 }; |
| 55 | static const char* const kUnitStrings2[] = { "Gi", "Mi", "Ki", "" }; |
| 56 | static const char* const kUnitStrings10[] = { "G", "M", "k", "" }; |
| 57 | |
| 58 | // Which set are we using? |
| 59 | const uint64_t* kUnitThresholds = ((base == 2) ? kUnitThresholds2 : kUnitThresholds10); |
| 60 | const uint64_t* kAmountPerUnit = ((base == 2) ? kAmountPerUnit2 : kAmountPerUnit10); |
| 61 | const char* const* kUnitStrings = ((base == 2) ? kUnitStrings2 : kUnitStrings10); |
| 62 | |
| 63 | size_t i = 0; |
| 64 | for (; kUnitThresholds[i] != 0; ++i) { |
| 65 | if (count >= kUnitThresholds[i]) { |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | char* s = NULL; |
| 70 | asprintf(&s, "%" PRId64 "%s", count / kAmountPerUnit[i], kUnitStrings[i]); |
| 71 | std::string result(s); |
| 72 | free(s); |
| 73 | return result; |
| 74 | } |
| 75 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 76 | static int Round(int n) { |
| 77 | int base = 1; |
| 78 | while (base*10 < n) { |
| 79 | base *= 10; |
| 80 | } |
| 81 | if (n < 2*base) { |
| 82 | return 2*base; |
| 83 | } |
| 84 | if (n < 5*base) { |
| 85 | return 5*base; |
| 86 | } |
| 87 | return 10*base; |
| 88 | } |
| 89 | |
| 90 | static int64_t NanoTime() { |
| 91 | struct timespec t; |
| 92 | t.tv_sec = t.tv_nsec = 0; |
| 93 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 94 | return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; |
| 95 | } |
| 96 | |
| 97 | namespace testing { |
| 98 | |
| 99 | Benchmark* Benchmark::Arg(int arg) { |
| 100 | args_.push_back(arg); |
| 101 | return this; |
| 102 | } |
| 103 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 104 | const char* Benchmark::Name() { |
| 105 | return name_; |
| 106 | } |
| 107 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 108 | bool Benchmark::ShouldRun(int argc, char* argv[]) { |
| 109 | if (argc == 1) { |
| 110 | return true; // With no arguments, we run all benchmarks. |
| 111 | } |
| 112 | // Otherwise, we interpret each argument as a regular expression and |
| 113 | // see if any of our benchmarks match. |
| 114 | for (int i = 1; i < argc; i++) { |
| 115 | regex_t re; |
| 116 | if (regcomp(&re, argv[i], 0) != 0) { |
| 117 | fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]); |
| 118 | exit(EXIT_FAILURE); |
| 119 | } |
| 120 | int match = regexec(&re, name_, 0, NULL, 0); |
| 121 | regfree(&re); |
| 122 | if (match != REG_NOMATCH) { |
| 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) { |
| 130 | name_ = name; |
| 131 | fn_ = fn; |
| 132 | fn_range_ = fn_range; |
| 133 | |
| 134 | if (fn_ == NULL && fn_range_ == NULL) { |
| 135 | fprintf(stderr, "%s: missing function\n", name_); |
| 136 | exit(EXIT_FAILURE); |
| 137 | } |
| 138 | |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame] | 139 | Benchmarks().push_back(this); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void Benchmark::Run() { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 143 | if (fn_ != NULL) { |
| 144 | RunWithArg(0); |
| 145 | } else { |
| 146 | if (args_.empty()) { |
| 147 | fprintf(stderr, "%s: no args!\n", name_); |
| 148 | exit(EXIT_FAILURE); |
| 149 | } |
| 150 | for (size_t i = 0; i < args_.size(); ++i) { |
| 151 | RunWithArg(args_[i]); |
| 152 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 157 | g_bytes_processed = 0; |
| 158 | g_benchmark_total_time_ns = 0; |
| 159 | g_benchmark_start_time_ns = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 160 | if (fn_ != NULL) { |
| 161 | fn_(iterations); |
| 162 | } else { |
| 163 | fn_range_(iterations, arg); |
| 164 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 165 | if (g_benchmark_start_time_ns != 0) { |
| 166 | g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | void Benchmark::RunWithArg(int arg) { |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 171 | // Run once in case it's expensive. |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 172 | int iterations = 1; |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 173 | int64_t realStartTime = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 174 | RunRepeatedlyWithArg(iterations, arg); |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 175 | int64_t realTotalTime = NanoTime() - realStartTime; |
| 176 | while (realTotalTime < 1e9 && iterations < 1e8) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 177 | int last = iterations; |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 178 | if (realTotalTime/iterations == 0) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 179 | iterations = 1e9; |
| 180 | } else { |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 181 | iterations = 1e9 / (realTotalTime/iterations); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 182 | } |
| 183 | iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last)); |
| 184 | iterations = Round(iterations); |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 185 | realStartTime = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 186 | RunRepeatedlyWithArg(iterations, arg); |
Colin Cross | a763504 | 2013-11-27 17:37:54 -0800 | [diff] [blame] | 187 | realTotalTime = NanoTime() - realStartTime; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | char throughput[100]; |
| 191 | throughput[0] = '\0'; |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 192 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 193 | if (g_benchmark_total_time_ns > 0 && g_bytes_processed > 0) { |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 194 | double gib_processed = static_cast<double>(g_bytes_processed)/1e9; |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 195 | double seconds = static_cast<double>(g_benchmark_total_time_ns)/1e9; |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 196 | snprintf(throughput, sizeof(throughput), " %8.3f GiB/s", gib_processed/seconds); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | char full_name[100]; |
| 200 | if (fn_range_ != NULL) { |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 201 | snprintf(full_name, sizeof(full_name), "%s/%s", name_, PrettyInt(arg, 2).c_str()); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 202 | } else { |
| 203 | snprintf(full_name, sizeof(full_name), "%s", name_); |
| 204 | } |
| 205 | |
Elliott Hughes | 8bb020e | 2015-01-16 13:11:25 -0800 | [diff] [blame^] | 206 | printf("%-*s %10s %10" PRId64 "%s\n", |
| 207 | g_name_column_width, full_name, |
| 208 | PrettyInt(iterations, 10).c_str(), |
| 209 | g_benchmark_total_time_ns/iterations, |
| 210 | throughput); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 211 | fflush(stdout); |
| 212 | } |
| 213 | |
| 214 | } // namespace testing |
| 215 | |
| 216 | void SetBenchmarkBytesProcessed(int64_t x) { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 217 | g_bytes_processed = x; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void StopBenchmarkTiming() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 221 | if (g_benchmark_start_time_ns != 0) { |
| 222 | g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 223 | } |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 224 | g_benchmark_start_time_ns = 0; |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void StartBenchmarkTiming() { |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 228 | if (g_benchmark_start_time_ns == 0) { |
| 229 | g_benchmark_start_time_ns = NanoTime(); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | int main(int argc, char* argv[]) { |
Colin Cross | 7b9df19 | 2013-11-15 14:34:22 -0800 | [diff] [blame] | 234 | if (Benchmarks().empty()) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 235 | fprintf(stderr, "No benchmarks registered!\n"); |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 236 | exit(EXIT_FAILURE); |
| 237 | } |
| 238 | |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame] | 239 | for (auto& b : Benchmarks()) { |
| 240 | int name_width = static_cast<int>(strlen(b->Name())); |
Elliott Hughes | 5ab51d0 | 2014-06-12 12:52:58 -0700 | [diff] [blame] | 241 | g_name_column_width = std::max(g_name_column_width, name_width); |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 244 | bool need_header = true; |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame] | 245 | for (auto& b : Benchmarks()) { |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 246 | if (b->ShouldRun(argc, argv)) { |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 247 | if (need_header) { |
Elliott Hughes | c0eed72 | 2014-06-11 16:48:29 -0700 | [diff] [blame] | 248 | printf("%-*s %10s %10s\n", g_name_column_width, "", "iterations", "ns/op"); |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 249 | fflush(stdout); |
| 250 | need_header = false; |
| 251 | } |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 252 | b->Run(); |
| 253 | } |
| 254 | } |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 255 | |
| 256 | if (need_header) { |
| 257 | fprintf(stderr, "No matching benchmarks!\n"); |
| 258 | fprintf(stderr, "Available benchmarks:\n"); |
Elliott Hughes | e48f533 | 2015-01-15 17:10:42 -0800 | [diff] [blame] | 259 | for (auto& b : Benchmarks()) { |
| 260 | fprintf(stderr, " %s\n", b->Name()); |
Elliott Hughes | 9edb3e0 | 2013-02-06 15:47:09 -0800 | [diff] [blame] | 261 | } |
| 262 | exit(EXIT_FAILURE); |
| 263 | } |
| 264 | |
Elliott Hughes | 7be369d | 2012-11-08 15:37:43 -0800 | [diff] [blame] | 265 | return 0; |
| 266 | } |