benchmark_main.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #include "benchmark.h"
  17. #include <regex.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <string>
  22. #include <inttypes.h>
  23. #include <time.h>
  24. #include <map>
  25. static int64_t g_flops_processed;
  26. static int64_t g_benchmark_total_time_ns;
  27. static int64_t g_benchmark_start_time_ns;
  28. typedef std::map<std::string, ::testing::Benchmark*> BenchmarkMap;
  29. typedef BenchmarkMap::iterator BenchmarkMapIt;
  30. BenchmarkMap& gBenchmarks() {
  31. static BenchmarkMap g_benchmarks;
  32. return g_benchmarks;
  33. }
  34. static int g_name_column_width = 20;
  35. static int Round(int n) {
  36. int base = 1;
  37. while (base*10 < n) {
  38. base *= 10;
  39. }
  40. if (n < 2*base) {
  41. return 2*base;
  42. }
  43. if (n < 5*base) {
  44. return 5*base;
  45. }
  46. return 10*base;
  47. }
  48. #ifdef __APPLE__
  49. #include <mach/mach_time.h>
  50. static mach_timebase_info_data_t g_time_info;
  51. static void __attribute__((constructor)) init_info() {
  52. mach_timebase_info(&g_time_info);
  53. }
  54. #endif
  55. static int64_t NanoTime() {
  56. #if defined(__APPLE__)
  57. uint64_t t = mach_absolute_time();
  58. return t * g_time_info.numer / g_time_info.denom;
  59. #else
  60. struct timespec t;
  61. t.tv_sec = t.tv_nsec = 0;
  62. clock_gettime(CLOCK_MONOTONIC, &t);
  63. return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec;
  64. #endif
  65. }
  66. namespace testing {
  67. Benchmark* Benchmark::Arg(int arg) {
  68. args_.push_back(arg);
  69. return this;
  70. }
  71. Benchmark* Benchmark::Range(int lo, int hi) {
  72. const int kRangeMultiplier = 8;
  73. if (hi < lo) {
  74. int temp = hi;
  75. hi = lo;
  76. lo = temp;
  77. }
  78. while (lo < hi) {
  79. args_.push_back(lo);
  80. lo *= kRangeMultiplier;
  81. }
  82. // We always run the hi number.
  83. args_.push_back(hi);
  84. return this;
  85. }
  86. const char* Benchmark::Name() {
  87. return name_;
  88. }
  89. bool Benchmark::ShouldRun(int argc, char* argv[]) {
  90. if (argc == 1) {
  91. return true; // With no arguments, we run all benchmarks.
  92. }
  93. // Otherwise, we interpret each argument as a regular expression and
  94. // see if any of our benchmarks match.
  95. for (int i = 1; i < argc; i++) {
  96. regex_t re;
  97. if (regcomp(&re, argv[i], 0) != 0) {
  98. fprintf(stderr, "couldn't compile \"%s\" as a regular expression!\n", argv[i]);
  99. exit(EXIT_FAILURE);
  100. }
  101. int match = regexec(&re, name_, 0, NULL, 0);
  102. regfree(&re);
  103. if (match != REG_NOMATCH) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. void Benchmark::Register(const char* name, void (*fn)(int), void (*fn_range)(int, int)) {
  110. name_ = name;
  111. fn_ = fn;
  112. fn_range_ = fn_range;
  113. if (fn_ == NULL && fn_range_ == NULL) {
  114. fprintf(stderr, "%s: missing function\n", name_);
  115. exit(EXIT_FAILURE);
  116. }
  117. gBenchmarks().insert(std::make_pair(name, this));
  118. }
  119. void Benchmark::Run() {
  120. if (fn_ != NULL) {
  121. RunWithArg(0);
  122. } else {
  123. if (args_.empty()) {
  124. fprintf(stderr, "%s: no args!\n", name_);
  125. exit(EXIT_FAILURE);
  126. }
  127. for (size_t i = 0; i < args_.size(); ++i) {
  128. RunWithArg(args_[i]);
  129. }
  130. }
  131. }
  132. void Benchmark::RunRepeatedlyWithArg(int iterations, int arg) {
  133. g_flops_processed = 0;
  134. g_benchmark_total_time_ns = 0;
  135. g_benchmark_start_time_ns = NanoTime();
  136. if (fn_ != NULL) {
  137. fn_(iterations);
  138. } else {
  139. fn_range_(iterations, arg);
  140. }
  141. if (g_benchmark_start_time_ns != 0) {
  142. g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
  143. }
  144. }
  145. void Benchmark::RunWithArg(int arg) {
  146. // run once in case it's expensive
  147. int iterations = 1;
  148. RunRepeatedlyWithArg(iterations, arg);
  149. while (g_benchmark_total_time_ns < 1e9 && iterations < 1e9) {
  150. int last = iterations;
  151. if (g_benchmark_total_time_ns/iterations == 0) {
  152. iterations = 1e9;
  153. } else {
  154. iterations = 1e9 / (g_benchmark_total_time_ns/iterations);
  155. }
  156. iterations = std::max(last + 1, std::min(iterations + iterations/2, 100*last));
  157. iterations = Round(iterations);
  158. RunRepeatedlyWithArg(iterations, arg);
  159. }
  160. char throughput[100];
  161. throughput[0] = '\0';
  162. if (g_benchmark_total_time_ns > 0 && g_flops_processed > 0) {
  163. double mflops_processed = static_cast<double>(g_flops_processed)/1e6;
  164. double seconds = static_cast<double>(g_benchmark_total_time_ns)/1e9;
  165. snprintf(throughput, sizeof(throughput), " %8.2f MFlops/s", mflops_processed/seconds);
  166. }
  167. char full_name[100];
  168. if (fn_range_ != NULL) {
  169. if (arg >= (1<<20)) {
  170. snprintf(full_name, sizeof(full_name), "%s/%dM", name_, arg/(1<<20));
  171. } else if (arg >= (1<<10)) {
  172. snprintf(full_name, sizeof(full_name), "%s/%dK", name_, arg/(1<<10));
  173. } else {
  174. snprintf(full_name, sizeof(full_name), "%s/%d", name_, arg);
  175. }
  176. } else {
  177. snprintf(full_name, sizeof(full_name), "%s", name_);
  178. }
  179. printf("%-*s %10d %10" PRId64 "%s\n", g_name_column_width, full_name,
  180. iterations, g_benchmark_total_time_ns/iterations, throughput);
  181. fflush(stdout);
  182. }
  183. } // namespace testing
  184. void SetBenchmarkFlopsProcessed(int64_t x) {
  185. g_flops_processed = x;
  186. }
  187. void StopBenchmarkTiming() {
  188. if (g_benchmark_start_time_ns != 0) {
  189. g_benchmark_total_time_ns += NanoTime() - g_benchmark_start_time_ns;
  190. }
  191. g_benchmark_start_time_ns = 0;
  192. }
  193. void StartBenchmarkTiming() {
  194. if (g_benchmark_start_time_ns == 0) {
  195. g_benchmark_start_time_ns = NanoTime();
  196. }
  197. }
  198. int main(int argc, char* argv[]) {
  199. if (gBenchmarks().empty()) {
  200. fprintf(stderr, "No benchmarks registered!\n");
  201. exit(EXIT_FAILURE);
  202. }
  203. for (BenchmarkMapIt it = gBenchmarks().begin(); it != gBenchmarks().end(); ++it) {
  204. int name_width = static_cast<int>(strlen(it->second->Name()));
  205. g_name_column_width = std::max(g_name_column_width, name_width);
  206. }
  207. bool need_header = true;
  208. for (BenchmarkMapIt it = gBenchmarks().begin(); it != gBenchmarks().end(); ++it) {
  209. ::testing::Benchmark* b = it->second;
  210. if (b->ShouldRun(argc, argv)) {
  211. if (need_header) {
  212. printf("%-*s %10s %10s\n", g_name_column_width, "", "iterations", "ns/op");
  213. fflush(stdout);
  214. need_header = false;
  215. }
  216. b->Run();
  217. }
  218. }
  219. if (need_header) {
  220. fprintf(stderr, "No matching benchmarks!\n");
  221. fprintf(stderr, "Available benchmarks:\n");
  222. for (BenchmarkMapIt it = gBenchmarks().begin(); it != gBenchmarks().end(); ++it) {
  223. fprintf(stderr, " %s\n", it->second->Name());
  224. }
  225. exit(EXIT_FAILURE);
  226. }
  227. return 0;
  228. }