Commit f8147e89 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Andrii Grynenko

BenchmarkSuspender::dismissing.

Summary:
[Folly] BenchmarkSuspender::dismissing.

Pass a lambda to it, and the lambda will be executed while the benchmark-suspender is dismissed. Just a bit of sugar around `BenchmarkSuspender::dismiss` and `BenchmarkSuspender::rehire`.

BENCHMARK(name_void, iters) {
BenchmarkSuspender braces;
# benchmark timer is suspended
braces.dismissing([&] {
# benchmark timer is running
doSomething();
});
# benchmark timer is suspended
}

BENCHMARK(name_value, iters) {
BenchmarkSuspender braces;
# benchmark timer is suspended
auto value = braces.dismissing([&] {
# benchmark timer is running
return doSomething();
});
# benchmark timer is suspended
}

Test Plan:
Unit tests:
* `folly/test/BenchmarkTest.cpp` (actually a benchmark)

Reviewed By: njormrod@fb.com

Subscribers: net-systems@, folly-diffs@, yfeldblum, chalfant

FB internal diff: D2024166

Signature: t1:2024166:1430163281:24df0ac98cbe36372f780372ee8f7dd3722b7868
parent dda97e8e
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#ifndef FOLLY_BENCHMARK_H_ #ifndef FOLLY_BENCHMARK_H_
#define FOLLY_BENCHMARK_H_ #define FOLLY_BENCHMARK_H_
#include <folly/ScopeGuard.h>
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/Preprocessor.h> // for FB_ANONYMOUS_VARIABLE #include <folly/Preprocessor.h> // for FB_ANONYMOUS_VARIABLE
#include <cassert> #include <cassert>
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <gflags/gflags.h> #include <gflags/gflags.h>
#include <limits> #include <limits>
#include <type_traits>
DECLARE_bool(benchmark); DECLARE_bool(benchmark);
...@@ -149,6 +151,13 @@ struct BenchmarkSuspender { ...@@ -149,6 +151,13 @@ struct BenchmarkSuspender {
CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &start)); CHECK_EQ(0, clock_gettime(detail::DEFAULT_CLOCK_ID, &start));
} }
template <class F>
auto dismissing(F f) -> typename std::result_of<F()>::type {
SCOPE_EXIT { rehire(); };
dismiss();
return f();
}
/** /**
* This is for use inside of if-conditions, used in BENCHMARK macros. * This is for use inside of if-conditions, used in BENCHMARK macros.
* If-conditions bypass the explicit on operator bool. * If-conditions bypass the explicit on operator bool.
......
...@@ -17,7 +17,11 @@ ...@@ -17,7 +17,11 @@
#include <folly/Benchmark.h> #include <folly/Benchmark.h>
#include <folly/Foreach.h> #include <folly/Foreach.h>
#include <folly/String.h> #include <folly/String.h>
#include <algorithm>
#include <iostream> #include <iostream>
#include <numeric>
#include <random>
#include <vector>
using namespace folly; using namespace folly;
using namespace std; using namespace std;
...@@ -128,6 +132,38 @@ BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 1); ...@@ -128,6 +132,38 @@ BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 1);
BENCHMARK_PARAM_MULTI(paramMulti, 5); BENCHMARK_PARAM_MULTI(paramMulti, 5);
BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 5); BENCHMARK_RELATIVE_PARAM_MULTI(paramMultiRel, 5);
BENCHMARK_DRAW_LINE();
BENCHMARK(BenchmarkSuspender_dismissing_void, iter) {
BenchmarkSuspender braces;
mt19937_64 rng;
while (iter--) {
vector<size_t> v(1 << 12, 0);
iota(v.begin(), v.end(), 0);
shuffle(v.begin(), v.end(), rng);
braces.dismissing([&] {
sort(v.begin(), v.end());
});
}
}
BENCHMARK(BenchmarkSuspender_dismissing_value, iter) {
BenchmarkSuspender braces;
mt19937_64 rng;
while (iter--) {
vector<size_t> v(1 << 12, 0);
iota(v.begin(), v.end(), 0);
shuffle(v.begin(), v.end(), rng);
auto s = braces.dismissing([&] {
sort(v.begin(), v.end());
return accumulate(v.begin(), v.end(), 0, [](size_t a, size_t e) {
return a + e;
});
});
doNotOptimizeAway(s);
}
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
runBenchmarks(); runBenchmarks();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment