2015-03-27 11:37:26 +08:00
|
|
|
#include "benchmark/benchmark_api.h"
|
2016-05-03 01:53:45 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2015-03-27 11:37:26 +08:00
|
|
|
|
2017-04-18 12:29:28 +08:00
|
|
|
#if defined(NDEBUG)
|
|
|
|
#undef NDEBUG
|
|
|
|
#endif
|
|
|
|
#include <cassert>
|
|
|
|
|
2015-03-27 11:37:26 +08:00
|
|
|
void BM_basic(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 05:47:27 +08:00
|
|
|
|
|
|
|
void BM_basic_slow(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
std::chrono::milliseconds sleep_duration(state.range(0));
|
2016-03-26 05:47:27 +08:00
|
|
|
while (state.KeepRunning()) {
|
2016-07-23 05:42:26 +08:00
|
|
|
std::this_thread::sleep_for(
|
2016-10-08 02:04:50 +08:00
|
|
|
std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
|
2016-03-26 05:47:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 11:37:26 +08:00
|
|
|
BENCHMARK(BM_basic);
|
|
|
|
BENCHMARK(BM_basic)->Arg(42);
|
2016-03-26 05:47:27 +08:00
|
|
|
BENCHMARK(BM_basic_slow)->Arg(10)->Unit(benchmark::kNanosecond);
|
|
|
|
BENCHMARK(BM_basic_slow)->Arg(100)->Unit(benchmark::kMicrosecond);
|
|
|
|
BENCHMARK(BM_basic_slow)->Arg(1000)->Unit(benchmark::kMillisecond);
|
2015-03-27 11:37:26 +08:00
|
|
|
BENCHMARK(BM_basic)->Range(1, 8);
|
2016-05-14 21:56:34 +08:00
|
|
|
BENCHMARK(BM_basic)->RangeMultiplier(2)->Range(1, 8);
|
2015-03-27 11:37:26 +08:00
|
|
|
BENCHMARK(BM_basic)->DenseRange(10, 15);
|
2016-08-05 03:30:14 +08:00
|
|
|
BENCHMARK(BM_basic)->Args({42, 42});
|
|
|
|
BENCHMARK(BM_basic)->Ranges({{64, 512}, {64, 512}});
|
2015-03-27 11:37:26 +08:00
|
|
|
BENCHMARK(BM_basic)->MinTime(0.7);
|
|
|
|
BENCHMARK(BM_basic)->UseRealTime();
|
|
|
|
BENCHMARK(BM_basic)->ThreadRange(2, 4);
|
|
|
|
BENCHMARK(BM_basic)->ThreadPerCpu();
|
2016-05-25 11:52:23 +08:00
|
|
|
BENCHMARK(BM_basic)->Repetitions(3);
|
2015-03-27 11:37:26 +08:00
|
|
|
|
2015-09-24 02:44:26 +08:00
|
|
|
void CustomArgs(benchmark::internal::Benchmark* b) {
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
b->Arg(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCHMARK(BM_basic)->Apply(CustomArgs);
|
|
|
|
|
2017-04-18 12:29:28 +08:00
|
|
|
void BM_explicit_iteration_count(benchmark::State& st) {
|
|
|
|
// Test that benchmarks specified with an explicit iteration count are
|
|
|
|
// only run once.
|
|
|
|
static bool invoked_before = false;
|
|
|
|
assert(!invoked_before);
|
|
|
|
invoked_before = true;
|
|
|
|
|
|
|
|
// Test that the requested iteration count is respected.
|
|
|
|
assert(st.max_iterations == 42);
|
|
|
|
size_t actual_iterations = 0;
|
|
|
|
while (st.KeepRunning())
|
|
|
|
++actual_iterations;
|
|
|
|
assert(st.iterations() == st.max_iterations);
|
|
|
|
assert(st.iterations() == 42);
|
|
|
|
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_explicit_iteration_count)->Iterations(42);
|
|
|
|
|
2015-03-27 11:37:26 +08:00
|
|
|
BENCHMARK_MAIN()
|