Debian Codegraph @

unittest/gunit/benchmark.cc

raw symbols SCIP
1 /* Copyright (c) 2016, 2026, Oracle and/or its affiliates.
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 designed to work 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 either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <assert.h>
25 #include <math.h>
26 #include <stddef.h>
27 #include <stdio.h>
28
29 #include <algorithm>
30 #include <chrono>
31
32 using std::chrono::duration;
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
41 void StartBenchmarkTiming() {
42 assert(!timer_running);
43 timer_running = true;
44 timer_start = steady_clock::now();
45 }
46
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
55 void SetBytesProcessed(size_t bytes) { bytes_processed = bytes; }
56
57 void internal_do_microbenchmark(const char *name, void (*func)(size_t)) {
58 #if !defined(NDEBUG)
59 printf(
60 "WARNING: Running microbenchmark in debug mode. "
61 "Timings will be misleading.\n");
62
63 // There's no point in timing in debug mode, so just run 10 times
64 // so that we don't waste build time (this should give us enough runs
65 // to verify that we don't crash).
66 seconds_used = 0.0;
67 size_t num_iterations = 10;
68 StartBenchmarkTiming();
69 func(num_iterations);
70 StopBenchmarkTiming();
71 #else
72 // Do 100 iterations as rough calibration. (Often, this will over- or
73 // undershoot by as much as 50%, but that's fine.)
74 static constexpr size_t calibration_iterations = 100;
75 seconds_used = 0.0;
76 StartBenchmarkTiming();
77 func(calibration_iterations);
78 StopBenchmarkTiming();
79 double seconds_used_per_iteration = seconds_used / calibration_iterations;
80
81 size_t num_iterations;
82
83 // Do the actual run, unless we already took more than one second.
84 if (seconds_used < 1.0) {
85 // Scale so that we end up around one second per benchmark
86 // (but never less than 100).
87 num_iterations =
88 std::max<size_t>(lrint(1.0 / seconds_used_per_iteration), 100);
89 seconds_used = 0.0;
90 StartBenchmarkTiming();
91 func(num_iterations);
92 StopBenchmarkTiming();
93 } else {
94 // The calibration already took too long, so just reuse its results.
95 num_iterations = calibration_iterations;
96 }
97 #endif
98
99 printf("%-40s %10ld iterations %10.0f ns/iter", name,
100 static_cast<long>(num_iterations),
101 1e9 * seconds_used / double(num_iterations));
102
103 if (bytes_processed > 0) {
104 double bytes_per_second = bytes_processed / seconds_used;
105 if (bytes_per_second > (512 << 20)) // 0.5 GB/sec.
106 printf(" %8.2f GB/sec", bytes_per_second / (1 << 30));
107 else
108 printf(" %8.2f MB/sec", bytes_per_second / (1 << 20));
109 bytes_processed = 0; // Reset for next test.
110 }
111
112 printf("\n");
113 }