1 /* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #include <assert.h>
24 #include <math.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 
28 #include <algorithm>
29 #include <chrono>
30 
31 using std::chrono::duration;
32 using std::chrono::duration_cast;
33 using std::chrono::nanoseconds;
34 using std::chrono::steady_clock;
35 
36 static bool timer_running = false;
37 static double seconds_used;
38 static steady_clock::time_point timer_start;
39 static size_t bytes_processed = 0;
40 
StartBenchmarkTiming()41 void StartBenchmarkTiming() {
42   assert(!timer_running);
43   timer_running = true;
44   timer_start = steady_clock::now();
45 }
46 
StopBenchmarkTiming()47 void StopBenchmarkTiming() {
48   if (timer_running) {
49     auto used = steady_clock::now() - timer_start;
50     seconds_used += duration<double>(used).count();
51     timer_running = false;
52   }
53 }
54 
SetBytesProcessed(size_t bytes)55 void SetBytesProcessed(size_t bytes) { bytes_processed = bytes; }
56 
internal_do_microbenchmark(const char * name,void (* func)(size_t))57 void internal_do_microbenchmark(const char *name, void (*func)(size_t)) {
58 #if !defined(DBUG_OFF)
59   printf(
60       "WARNING: Running microbenchmark in debug mode. "
61       "Timings will be misleading.\n");
62 #endif
63 
64   // Do 100 iterations as rough calibration. (Often, this will over- or
65   // undershoot by as much as 50%, but that's fine.)
66   static constexpr size_t calibration_iterations = 100;
67   seconds_used = 0.0;
68   StartBenchmarkTiming();
69   func(calibration_iterations);
70   StopBenchmarkTiming();
71   double seconds_used_per_iteration = seconds_used / calibration_iterations;
72 
73   // Scale so that we end up around one second per benchmark
74   // (but never less than 100).
75   size_t num_iterations =
76       std::max<size_t>(lrint(1.0 / seconds_used_per_iteration), 100);
77 
78   // Do the actual run.
79   seconds_used = 0.0;
80   StartBenchmarkTiming();
81   func(num_iterations);
82   StopBenchmarkTiming();
83 
84   printf("%-40s %10ld iterations %10.0f ns/iter", name,
85          static_cast<long>(num_iterations),
86          1e9 * seconds_used / double(num_iterations));
87 
88   if (bytes_processed > 0) {
89     double bytes_per_second = bytes_processed / seconds_used;
90     if (bytes_per_second > (512 << 20))  // 0.5 GB/sec.
91       printf(" %8.2f GB/sec", bytes_per_second / (1 << 30));
92     else
93       printf(" %8.2f MB/sec", bytes_per_second / (1 << 20));
94     bytes_processed = 0;  // Reset for next test.
95   }
96 
97   printf("\n");
98 }
99