2015-03-13 06:03:33 +08:00
|
|
|
|
2015-03-18 02:02:02 +08:00
|
|
|
#include "benchmark/benchmark_api.h"
|
2015-03-13 06:03:33 +08:00
|
|
|
|
|
|
|
#define BASIC_BENCHMARK_TEST(x) \
|
|
|
|
BENCHMARK(x)->Arg(8)->Arg(512)->Arg(8192)
|
|
|
|
|
|
|
|
void BM_empty(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(state.iterations());
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_empty);
|
|
|
|
BENCHMARK(BM_empty)->ThreadPerCpu();
|
|
|
|
|
|
|
|
void BM_spin_empty(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int x = 0; x < state.range(0); ++x) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(x);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_empty);
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
|
|
|
|
|
|
|
|
void BM_spin_pause_before(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
while(state.KeepRunning()) {
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_before);
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_before)->ThreadPerCpu();
|
|
|
|
|
|
|
|
|
|
|
|
void BM_spin_pause_during(benchmark::State& state) {
|
|
|
|
while(state.KeepRunning()) {
|
|
|
|
state.PauseTiming();
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
state.ResumeTiming();
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_during);
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_during)->ThreadPerCpu();
|
|
|
|
|
2015-03-27 02:26:07 +08:00
|
|
|
void BM_pause_during(benchmark::State& state) {
|
|
|
|
while(state.KeepRunning()) {
|
|
|
|
state.PauseTiming();
|
|
|
|
state.ResumeTiming();
|
|
|
|
}
|
|
|
|
}
|
2015-03-27 02:56:52 +08:00
|
|
|
BENCHMARK(BM_pause_during);
|
|
|
|
BENCHMARK(BM_pause_during)->ThreadPerCpu();
|
2015-03-28 04:37:53 +08:00
|
|
|
BENCHMARK(BM_pause_during)->UseRealTime();
|
|
|
|
BENCHMARK(BM_pause_during)->UseRealTime()->ThreadPerCpu();
|
2015-03-27 02:26:07 +08:00
|
|
|
|
2015-03-13 06:03:33 +08:00
|
|
|
void BM_spin_pause_after(benchmark::State& state) {
|
|
|
|
while(state.KeepRunning()) {
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_after);
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
|
|
|
|
|
|
|
|
|
|
|
|
void BM_spin_pause_before_and_after(benchmark::State& state) {
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
while(state.KeepRunning()) {
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
2016-08-05 03:30:14 +08:00
|
|
|
for (int i = 0; i < state.range(0); ++i) {
|
2015-03-28 04:35:46 +08:00
|
|
|
benchmark::DoNotOptimize(i);
|
2015-03-13 06:03:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after);
|
|
|
|
BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after)->ThreadPerCpu();
|
|
|
|
|
|
|
|
|
|
|
|
void BM_empty_stop_start(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) { }
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_empty_stop_start);
|
|
|
|
BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
|
|
|
|
|
|
|
|
BENCHMARK_MAIN()
|