2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_BENCHMARK_H_
18 #define FOLLY_BENCHMARK_H_
20 #include <folly/Portability.h>
21 #include <folly/Preprocessor.h> // for FB_ANONYMOUS_VARIABLE
22 #include <folly/ScopeGuard.h>
23 #include <folly/portability/Time.h>
27 #include <boost/function_types/function_arity.hpp>
29 #include <glog/logging.h>
30 #include <gflags/gflags.h>
32 #include <type_traits>
34 DECLARE_bool(benchmark);
39 * Runs all benchmarks defined. Usually put in main().
44 * Runs all benchmarks defined if and only if the --benchmark flag has
45 * been passed to the program. Usually put in main().
47 inline bool runBenchmarksOnFlag() {
48 if (FLAGS_benchmark) {
51 return FLAGS_benchmark;
57 * This is the clock ID used for measuring time. On older kernels, the
58 * resolution of this clock will be very coarse, which will cause the
61 enum Clock { DEFAULT_CLOCK_ID = CLOCK_REALTIME };
63 typedef std::pair<uint64_t, unsigned int> TimeIterPair;
66 * Adds a benchmark wrapped in a std::function. Only used
67 * internally. Pass by value is intentional.
69 void addBenchmarkImpl(const char* file,
71 std::function<TimeIterPair(unsigned int)>);
74 * Takes the difference between two timespec values. end is assumed to
77 inline uint64_t timespecDiff(timespec end, timespec start) {
78 if (end.tv_sec == start.tv_sec) {
79 assert(end.tv_nsec >= start.tv_nsec);
80 return end.tv_nsec - start.tv_nsec;
82 assert(end.tv_sec > start.tv_sec);
83 auto diff = uint64_t(end.tv_sec - start.tv_sec);
85 std::numeric_limits<uint64_t>::max() / 1000000000UL);
86 return diff * 1000000000UL
87 + end.tv_nsec - start.tv_nsec;
91 * Takes the difference between two sets of timespec values. The first
92 * two come from a high-resolution clock whereas the other two come
93 * from a low-resolution clock. The crux of the matter is that
94 * high-res values may be bogus as documented in
95 * http://linux.die.net/man/3/clock_gettime. The trouble is when the
96 * running process migrates from one CPU to another, which is more
97 * likely for long-running processes. Therefore we watch for high
98 * differences between the two timings.
100 * This function is subject to further improvements.
102 inline uint64_t timespecDiff(timespec end, timespec start,
103 timespec endCoarse, timespec startCoarse) {
104 auto fine = timespecDiff(end, start);
105 auto coarse = timespecDiff(endCoarse, startCoarse);
106 if (coarse - fine >= 1000000) {
107 // The fine time is in all likelihood bogus
113 } // namespace detail
116 * Supporting type for BENCHMARK_SUSPEND defined below.
118 struct BenchmarkSuspender {
119 BenchmarkSuspender() {
120 CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &start));
123 BenchmarkSuspender(const BenchmarkSuspender &) = delete;
124 BenchmarkSuspender(BenchmarkSuspender && rhs) noexcept {
126 rhs.start.tv_nsec = rhs.start.tv_sec = 0;
129 BenchmarkSuspender& operator=(const BenchmarkSuspender &) = delete;
130 BenchmarkSuspender& operator=(BenchmarkSuspender && rhs) {
131 if (start.tv_nsec > 0 || start.tv_sec > 0) {
135 rhs.start.tv_nsec = rhs.start.tv_sec = 0;
139 ~BenchmarkSuspender() {
140 if (start.tv_nsec > 0 || start.tv_sec > 0) {
146 assert(start.tv_nsec > 0 || start.tv_sec > 0);
148 start.tv_nsec = start.tv_sec = 0;
152 assert(start.tv_nsec == 0 || start.tv_sec == 0);
153 CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &start));
157 auto dismissing(F f) -> typename std::result_of<F()>::type {
158 SCOPE_EXIT { rehire(); };
164 * This is for use inside of if-conditions, used in BENCHMARK macros.
165 * If-conditions bypass the explicit on operator bool.
167 explicit operator bool() const {
172 * Accumulates nanoseconds spent outside benchmark.
174 typedef uint64_t NanosecondsSpent;
175 static NanosecondsSpent nsSpent;
180 CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &end));
181 nsSpent += detail::timespecDiff(end, start);
189 * Adds a benchmark. Usually not called directly but instead through
190 * the macro BENCHMARK defined below. The lambda function involved
191 * must take exactly one parameter of type unsigned, and the benchmark
192 * uses it with counter semantics (iteration occurs inside the
195 template <typename Lambda>
196 typename std::enable_if<
197 boost::function_types::function_arity<decltype(&Lambda::operator())>::value
200 addBenchmark(const char* file, const char* name, Lambda&& lambda) {
201 auto execute = [=](unsigned int times) {
202 BenchmarkSuspender::nsSpent = 0;
206 // CORE MEASUREMENT STARTS
207 auto const r1 = clock_gettime(detail::DEFAULT_CLOCK_ID, &start);
208 niter = lambda(times);
209 auto const r2 = clock_gettime(detail::DEFAULT_CLOCK_ID, &end);
210 // CORE MEASUREMENT ENDS
215 return detail::TimeIterPair(
216 detail::timespecDiff(end, start) - BenchmarkSuspender::nsSpent,
220 detail::addBenchmarkImpl(file, name,
221 std::function<detail::TimeIterPair(unsigned int)>(execute));
225 * Adds a benchmark. Usually not called directly but instead through
226 * the macro BENCHMARK defined below. The lambda function involved
227 * must take zero parameters, and the benchmark calls it repeatedly
228 * (iteration occurs outside the function).
230 template <typename Lambda>
231 typename std::enable_if<
232 boost::function_types::function_arity<decltype(&Lambda::operator())>::value
235 addBenchmark(const char* file, const char* name, Lambda&& lambda) {
236 addBenchmark(file, name, [=](unsigned int times) {
237 unsigned int niter = 0;
238 while (times-- > 0) {
246 * Call doNotOptimizeAway(var) against variables that you use for
247 * benchmarking but otherwise are useless. The compiler tends to do a
248 * good job at eliminating unused variables, and this function fools
249 * it into thinking var is in fact needed.
253 #pragma optimize("", off)
256 void doNotOptimizeAway(T&& datum) {
260 #pragma optimize("", on)
262 #elif defined(__clang__)
265 __attribute__((__optnone__)) void doNotOptimizeAway(T&& /* datum */) {}
270 void doNotOptimizeAway(T&& datum) {
271 asm volatile("" : "+r" (datum));
279 * Introduces a benchmark function. Used internally, see BENCHMARK and
282 #define BENCHMARK_IMPL(funName, stringName, rv, paramType, paramName) \
283 static void funName(paramType); \
284 static bool FB_ANONYMOUS_VARIABLE(follyBenchmarkUnused) = ( \
285 ::folly::addBenchmark(__FILE__, stringName, \
286 [](paramType paramName) -> unsigned { funName(paramName); \
289 static void funName(paramType paramName)
292 * Introduces a benchmark function with support for returning the actual
293 * number of iterations. Used internally, see BENCHMARK_MULTI and friends
296 #define BENCHMARK_MULTI_IMPL(funName, stringName, paramType, paramName) \
297 static unsigned funName(paramType); \
298 static bool FB_ANONYMOUS_VARIABLE(follyBenchmarkUnused) = ( \
299 ::folly::addBenchmark(__FILE__, stringName, \
300 [](paramType paramName) { return funName(paramName); }), \
302 static unsigned funName(paramType paramName)
305 * Introduces a benchmark function. Use with either one or two arguments.
306 * The first is the name of the benchmark. Use something descriptive, such
307 * as insertVectorBegin. The second argument may be missing, or could be a
308 * symbolic counter. The counter dictates how many internal iteration the
309 * benchmark does. Example:
311 * BENCHMARK(vectorPushBack) {
316 * BENCHMARK(insertVectorBegin, n) {
318 * FOR_EACH_RANGE (i, 0, n) {
319 * v.insert(v.begin(), 42);
323 #define BENCHMARK(name, ...) \
326 FB_STRINGIZE(name), \
327 FB_ARG_2_OR_1(1, ## __VA_ARGS__), \
328 FB_ONE_OR_NONE(unsigned, ## __VA_ARGS__), \
332 * Like BENCHMARK above, but allows the user to return the actual
333 * number of iterations executed in the function body. This can be
334 * useful if the benchmark function doesn't know upfront how many
335 * iterations it's going to run or if it runs through a certain
336 * number of test cases, e.g.:
338 * BENCHMARK_MULTI(benchmarkSomething) {
339 * std::vector<int> testCases { 0, 1, 1, 2, 3, 5 };
340 * for (int c : testCases) {
343 * return testCases.size();
346 #define BENCHMARK_MULTI(name, ...) \
347 BENCHMARK_MULTI_IMPL( \
349 FB_STRINGIZE(name), \
350 FB_ONE_OR_NONE(unsigned, ## __VA_ARGS__), \
354 * Defines a benchmark that passes a parameter to another one. This is
355 * common for benchmarks that need a "problem size" in addition to
356 * "number of iterations". Consider:
358 * void pushBack(uint n, size_t initialSize) {
360 * BENCHMARK_SUSPEND {
361 * v.resize(initialSize);
363 * FOR_EACH_RANGE (i, 0, n) {
367 * BENCHMARK_PARAM(pushBack, 0)
368 * BENCHMARK_PARAM(pushBack, 1000)
369 * BENCHMARK_PARAM(pushBack, 1000000)
371 * The benchmark above estimates the speed of push_back at different
372 * initial sizes of the vector. The framework will pass 0, 1000, and
373 * 1000000 for initialSize, and the iteration count for n.
375 #define BENCHMARK_PARAM(name, param) \
376 BENCHMARK_NAMED_PARAM(name, param, param)
379 * Same as BENCHMARK_PARAM, but allows to return the actual number of
380 * iterations that have been run.
382 #define BENCHMARK_PARAM_MULTI(name, param) \
383 BENCHMARK_NAMED_PARAM_MULTI(name, param, param)
386 * Like BENCHMARK_PARAM(), but allows a custom name to be specified for each
387 * parameter, rather than using the parameter value.
389 * Useful when the parameter value is not a valid token for string pasting,
390 * of when you want to specify multiple parameter arguments.
394 * void addValue(uint n, int64_t bucketSize, int64_t min, int64_t max) {
395 * Histogram<int64_t> hist(bucketSize, min, max);
397 * FOR_EACH_RANGE (i, 0, n) {
398 * hist.addValue(num);
400 * if (num > max) { num = min; }
404 * BENCHMARK_NAMED_PARAM(addValue, 0_to_100, 1, 0, 100)
405 * BENCHMARK_NAMED_PARAM(addValue, 0_to_1000, 10, 0, 1000)
406 * BENCHMARK_NAMED_PARAM(addValue, 5k_to_20k, 250, 5000, 20000)
408 #define BENCHMARK_NAMED_PARAM(name, param_name, ...) \
410 FB_CONCATENATE(name, FB_CONCATENATE(_, param_name)), \
411 FB_STRINGIZE(name) "(" FB_STRINGIZE(param_name) ")", \
415 name(iters, ## __VA_ARGS__); \
419 * Same as BENCHMARK_NAMED_PARAM, but allows to return the actual number
420 * of iterations that have been run.
422 #define BENCHMARK_NAMED_PARAM_MULTI(name, param_name, ...) \
423 BENCHMARK_MULTI_IMPL( \
424 FB_CONCATENATE(name, FB_CONCATENATE(_, param_name)), \
425 FB_STRINGIZE(name) "(" FB_STRINGIZE(param_name) ")", \
428 return name(iters, ## __VA_ARGS__); \
432 * Just like BENCHMARK, but prints the time relative to a
433 * baseline. The baseline is the most recent BENCHMARK() seen in
434 * lexical order. Example:
436 * // This is the baseline
437 * BENCHMARK(insertVectorBegin, n) {
439 * FOR_EACH_RANGE (i, 0, n) {
440 * v.insert(v.begin(), 42);
444 * BENCHMARK_RELATIVE(insertListBegin, n) {
446 * FOR_EACH_RANGE (i, 0, n) {
447 * s.insert(s.begin(), 42);
451 * Any number of relative benchmark can be associated with a
452 * baseline. Another BENCHMARK() occurrence effectively establishes a
455 #define BENCHMARK_RELATIVE(name, ...) \
458 "%" FB_STRINGIZE(name), \
459 FB_ARG_2_OR_1(1, ## __VA_ARGS__), \
460 FB_ONE_OR_NONE(unsigned, ## __VA_ARGS__), \
464 * Same as BENCHMARK_RELATIVE, but allows to return the actual number
465 * of iterations that have been run.
467 #define BENCHMARK_RELATIVE_MULTI(name, ...) \
468 BENCHMARK_MULTI_IMPL( \
470 "%" FB_STRINGIZE(name), \
471 FB_ONE_OR_NONE(unsigned, ## __VA_ARGS__), \
475 * A combination of BENCHMARK_RELATIVE and BENCHMARK_PARAM.
477 #define BENCHMARK_RELATIVE_PARAM(name, param) \
478 BENCHMARK_RELATIVE_NAMED_PARAM(name, param, param)
481 * Same as BENCHMARK_RELATIVE_PARAM, but allows to return the actual
482 * number of iterations that have been run.
484 #define BENCHMARK_RELATIVE_PARAM_MULTI(name, param) \
485 BENCHMARK_RELATIVE_NAMED_PARAM_MULTI(name, param, param)
488 * A combination of BENCHMARK_RELATIVE and BENCHMARK_NAMED_PARAM.
490 #define BENCHMARK_RELATIVE_NAMED_PARAM(name, param_name, ...) \
492 FB_CONCATENATE(name, FB_CONCATENATE(_, param_name)), \
493 "%" FB_STRINGIZE(name) "(" FB_STRINGIZE(param_name) ")", \
497 name(iters, ## __VA_ARGS__); \
501 * Same as BENCHMARK_RELATIVE_NAMED_PARAM, but allows to return the
502 * actual number of iterations that have been run.
504 #define BENCHMARK_RELATIVE_NAMED_PARAM_MULTI(name, param_name, ...) \
505 BENCHMARK_MULTI_IMPL( \
506 FB_CONCATENATE(name, FB_CONCATENATE(_, param_name)), \
507 "%" FB_STRINGIZE(name) "(" FB_STRINGIZE(param_name) ")", \
510 return name(iters, ## __VA_ARGS__); \
514 * Draws a line of dashes.
516 #define BENCHMARK_DRAW_LINE() \
517 static bool FB_ANONYMOUS_VARIABLE(follyBenchmarkUnused) = ( \
518 ::folly::addBenchmark(__FILE__, "-", []() -> unsigned { return 0; }), \
522 * Allows execution of code that doesn't count torward the benchmark's
523 * time budget. Example:
525 * BENCHMARK_START_GROUP(insertVectorBegin, n) {
527 * BENCHMARK_SUSPEND {
530 * FOR_EACH_RANGE (i, 0, n) {
531 * v.insert(v.begin(), 42);
535 #define BENCHMARK_SUSPEND \
536 if (auto FB_ANONYMOUS_VARIABLE(BENCHMARK_SUSPEND) = \
537 ::folly::BenchmarkSuspender()) {} \
540 #endif // FOLLY_BENCHMARK_H_