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 // @author Andrei Alexandrescu (andrei.alexandrescu@fb.com)
19 #include <folly/Benchmark.h>
20 #include <folly/Foreach.h>
21 #include <folly/json.h>
22 #include <folly/String.h>
25 #include <boost/regex.hpp>
35 DEFINE_bool(benchmark, false, "Run benchmarks.");
36 DEFINE_bool(json, false, "Output in JSON format.");
41 "Only benchmarks whose names match this regex will be run.");
46 "Minimum # of microseconds we'll accept for each benchmark.");
51 "Minimum # of iterations we'll try for each benchmark.");
56 "Maximum # of seconds we'll spend on each benchmark.");
60 BenchmarkSuspender::NanosecondsSpent BenchmarkSuspender::nsSpent;
62 typedef function<detail::TimeIterPair(unsigned int)> BenchmarkFun;
65 vector<tuple<string, string, BenchmarkFun>>& benchmarks() {
66 static vector<tuple<string, string, BenchmarkFun>> _benchmarks;
70 #define FB_FOLLY_GLOBAL_BENCHMARK_BASELINE fbFollyGlobalBenchmarkBaseline
71 #define FB_STRINGIZE_X2(x) FB_STRINGIZE(x)
73 // Add the global baseline
74 BENCHMARK(FB_FOLLY_GLOBAL_BENCHMARK_BASELINE) {
82 int getGlobalBenchmarkBaselineIndex() {
83 const char *global = FB_STRINGIZE_X2(FB_FOLLY_GLOBAL_BENCHMARK_BASELINE);
84 auto it = std::find_if(
87 [global](const tuple<string, string, BenchmarkFun> &v) {
88 return get<1>(v) == global;
91 CHECK(it != benchmarks().end());
92 return it - benchmarks().begin();
95 #undef FB_STRINGIZE_X2
96 #undef FB_FOLLY_GLOBAL_BENCHMARK_BASELINE
98 void detail::addBenchmarkImpl(const char* file, const char* name,
100 benchmarks().emplace_back(file, name, std::move(fun));
104 * Given a point, gives density at that point as a number 0.0 < x <=
105 * 1.0. The result is 1.0 if all samples are equal to where, and
106 * decreases near 0 if all points are far away from it. The density is
107 * computed with the help of a radial basis function.
109 static double density(const double * begin, const double *const end,
110 const double where, const double bandwidth) {
112 assert(bandwidth > 0.0);
114 FOR_EACH_RANGE (i, begin, end) {
115 auto d = (*i - where) / bandwidth;
118 return sum / (end - begin);
122 * Computes mean and variance for a bunch of data points. Note that
123 * mean is currently not being used.
125 static pair<double, double>
126 meanVariance(const double * begin, const double *const end) {
128 double sum = 0.0, sum2 = 0.0;
129 FOR_EACH_RANGE (i, begin, end) {
133 auto const n = end - begin;
134 return make_pair(sum / n, sqrt((sum2 - sum * sum / n) / n));
138 * Computes the mode of a sample set through brute force. Assumes
141 static double mode(const double * begin, const double *const end) {
143 // Lower bound and upper bound for result and their respective
149 // Get the variance so we pass it down to density()
150 auto const sigma = meanVariance(begin, end).second;
152 // No variance means constant signal
156 FOR_EACH_RANGE (i, begin, end) {
157 assert(i == begin || *i >= i[-1]);
158 auto candidate = density(begin, end, *i, sigma * sqrt(2.0));
159 if (candidate > bestDensity) {
161 bestDensity = candidate;
164 // Density is decreasing... we could break here if we definitely
165 // knew this is unimodal.
173 * Given a bunch of benchmark samples, estimate the actual run time.
175 static double estimateTime(double * begin, double * end) {
178 // Current state of the art: get the minimum. After some
179 // experimentation, it seems taking the minimum is the best.
181 return *min_element(begin, end);
183 // What follows after estimates the time as the mode of the
186 // Select the awesomest (i.e. most frequent) result. We do this by
187 // sorting and then computing the longest run length.
190 // Eliminate outliers. A time much larger than the minimum time is
191 // considered an outlier.
192 while (end[-1] > 2.0 * *begin) {
202 /* Code used just for comparison purposes */ {
203 unsigned bestFrequency = 0;
204 unsigned candidateFrequency = 1;
205 double candidateValue = *begin;
206 for (auto current = begin + 1; ; ++current) {
207 if (current == end || *current != candidateValue) {
208 // Done with the current run, see if it was best
209 if (candidateFrequency > bestFrequency) {
210 bestFrequency = candidateFrequency;
211 result = candidateValue;
213 if (current == end) {
217 candidateValue = *current;
218 candidateFrequency = 1;
220 // Cool, inside a run, increase the frequency
221 ++candidateFrequency;
226 result = mode(begin, end);
231 static double runBenchmarkGetNSPerIteration(const BenchmarkFun& fun,
232 const double globalBaseline) {
233 // They key here is accuracy; too low numbers means the accuracy was
234 // coarse. We up the ante until we get to at least minNanoseconds
236 static uint64_t resolutionInNs = 0;
237 if (!resolutionInNs) {
239 CHECK_EQ(0, clock_getres(detail::DEFAULT_CLOCK_ID, &ts));
240 CHECK_EQ(0, ts.tv_sec) << "Clock sucks.";
241 CHECK_LT(0, ts.tv_nsec) << "Clock too fast for its own good.";
242 CHECK_EQ(1, ts.tv_nsec) << "Clock too coarse, upgrade your kernel.";
243 resolutionInNs = ts.tv_nsec;
245 // We choose a minimum minimum (sic) of 100,000 nanoseconds, but if
246 // the clock resolution is worse than that, it will be larger. In
247 // essence we're aiming at making the quantization noise 0.01%.
248 static const auto minNanoseconds =
249 max<uint64_t>(FLAGS_bm_min_usec * 1000UL,
250 min<uint64_t>(resolutionInNs * 100000, 1000000000ULL));
252 // We do measurements in several epochs and take the minimum, to
253 // account for jitter.
254 static const unsigned int epochs = 1000;
255 // We establish a total time budget as we don't want a measurement
256 // to take too long. This will curtail the number of actual epochs.
257 const uint64_t timeBudgetInNs = FLAGS_bm_max_secs * 1000000000ULL;
259 CHECK_EQ(0, clock_gettime(CLOCK_REALTIME, &global));
261 double epochResults[epochs] = { 0 };
262 size_t actualEpochs = 0;
264 for (; actualEpochs < epochs; ++actualEpochs) {
265 for (unsigned int n = FLAGS_bm_min_iters; n < (1UL << 30); n *= 2) {
266 auto const nsecsAndIter = fun(n);
267 if (nsecsAndIter.first < minNanoseconds) {
270 // We got an accurate enough timing, done. But only save if
271 // smaller than the current result.
272 epochResults[actualEpochs] = max(0.0, double(nsecsAndIter.first) /
273 nsecsAndIter.second - globalBaseline);
274 // Done with the current epoch, we got a meaningful timing.
278 CHECK_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
279 if (detail::timespecDiff(now, global) >= timeBudgetInNs) {
280 // No more time budget available.
286 // If the benchmark was basically drowned in baseline noise, it's
287 // possible it became negative.
288 return max(0.0, estimateTime(epochResults, epochResults + actualEpochs));
296 static const ScaleInfo kTimeSuffixes[] {
297 { 365.25 * 24 * 3600, "years" },
298 { 24 * 3600, "days" },
310 static const ScaleInfo kMetricSuffixes[] {
311 { 1E24, "Y" }, // yotta
312 { 1E21, "Z" }, // zetta
313 { 1E18, "X" }, // "exa" written with suffix 'X' so as to not create
314 // confusion with scientific notation
315 { 1E15, "P" }, // peta
316 { 1E12, "T" }, // terra
317 { 1E9, "G" }, // giga
318 { 1E6, "M" }, // mega
319 { 1E3, "K" }, // kilo
321 { 1E-3, "m" }, // milli
322 { 1E-6, "u" }, // micro
323 { 1E-9, "n" }, // nano
324 { 1E-12, "p" }, // pico
325 { 1E-15, "f" }, // femto
326 { 1E-18, "a" }, // atto
327 { 1E-21, "z" }, // zepto
328 { 1E-24, "y" }, // yocto
332 static string humanReadable(double n, unsigned int decimals,
333 const ScaleInfo* scales) {
334 if (std::isinf(n) || std::isnan(n)) {
335 return folly::to<string>(n);
338 const double absValue = fabs(n);
339 const ScaleInfo* scale = scales;
340 while (absValue < scale[0].boundary && scale[1].suffix != nullptr) {
344 const double scaledValue = n / scale->boundary;
345 return stringPrintf("%.*f%s", decimals, scaledValue, scale->suffix);
348 static string readableTime(double n, unsigned int decimals) {
349 return humanReadable(n, decimals, kTimeSuffixes);
352 static string metricReadable(double n, unsigned int decimals) {
353 return humanReadable(n, decimals, kMetricSuffixes);
356 static void printBenchmarkResultsAsTable(
357 const vector<tuple<string, string, double> >& data) {
359 static const unsigned int columns = 76;
361 // Compute the longest benchmark name
362 size_t longestName = 0;
363 FOR_EACH_RANGE (i, 1, benchmarks().size()) {
364 longestName = max(longestName, get<1>(benchmarks()[i]).size());
367 // Print a horizontal rule
368 auto separator = [&](char pad) {
369 puts(string(columns, pad).c_str());
372 // Print header for a file
373 auto header = [&](const string& file) {
375 printf("%-*srelative time/iter iters/s\n",
376 columns - 28, file.c_str());
380 double baselineNsPerIter = numeric_limits<double>::max();
383 for (auto& datum : data) {
384 auto file = get<0>(datum);
385 if (file != lastFile) {
391 string s = get<1>(datum);
396 bool useBaseline /* = void */;
401 baselineNsPerIter = get<2>(datum);
404 s.resize(columns - 29, ' ');
405 auto nsPerIter = get<2>(datum);
406 auto secPerIter = nsPerIter / 1E9;
407 auto itersPerSec = (secPerIter == 0)
408 ? std::numeric_limits<double>::infinity()
411 // Print without baseline
412 printf("%*s %9s %7s\n",
413 static_cast<int>(s.size()), s.c_str(),
414 readableTime(secPerIter, 2).c_str(),
415 metricReadable(itersPerSec, 2).c_str());
417 // Print with baseline
418 auto rel = baselineNsPerIter / nsPerIter * 100.0;
419 printf("%*s %7.2f%% %9s %7s\n",
420 static_cast<int>(s.size()), s.c_str(),
422 readableTime(secPerIter, 2).c_str(),
423 metricReadable(itersPerSec, 2).c_str());
429 static void printBenchmarkResultsAsJson(
430 const vector<tuple<string, string, double> >& data) {
431 dynamic d = dynamic::object;
432 for (auto& datum: data) {
433 d[std::get<1>(datum)] = std::get<2>(datum) * 1000.;
436 printf("%s\n", toPrettyJson(d).c_str());
439 static void printBenchmarkResults(
440 const vector<tuple<string, string, double> >& data) {
443 printBenchmarkResultsAsJson(data);
445 printBenchmarkResultsAsTable(data);
449 void runBenchmarks() {
450 CHECK(!benchmarks().empty());
452 vector<tuple<string, string, double>> results;
453 results.reserve(benchmarks().size() - 1);
455 std::unique_ptr<boost::regex> bmRegex;
456 if (!FLAGS_bm_regex.empty()) {
457 bmRegex.reset(new boost::regex(FLAGS_bm_regex));
460 // PLEASE KEEP QUIET. MEASUREMENTS IN PROGRESS.
462 unsigned int baselineIndex = getGlobalBenchmarkBaselineIndex();
464 auto const globalBaseline =
465 runBenchmarkGetNSPerIteration(get<2>(benchmarks()[baselineIndex]), 0);
466 FOR_EACH_RANGE (i, 0, benchmarks().size()) {
467 if (i == baselineIndex) {
470 double elapsed = 0.0;
471 if (get<1>(benchmarks()[i]) != "-") { // skip separators
472 if (bmRegex && !boost::regex_search(get<1>(benchmarks()[i]), *bmRegex)) {
475 elapsed = runBenchmarkGetNSPerIteration(get<2>(benchmarks()[i]),
478 results.emplace_back(get<0>(benchmarks()[i]),
479 get<1>(benchmarks()[i]), elapsed);
482 // PLEASE MAKE NOISE. MEASUREMENTS DONE.
484 printBenchmarkResults(results);