2023-08-11 11:46:36 +00:00
|
|
|
#include <cstdarg>
|
2021-04-28 08:25:29 +00:00
|
|
|
#undef NDEBUG
|
|
|
|
|
2023-07-07 08:58:16 +00:00
|
|
|
#include "../src/commandlineflags.h"
|
2023-08-11 11:46:36 +00:00
|
|
|
#include "../src/perf_counters.h"
|
2021-04-28 08:25:29 +00:00
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
#include "output_test.h"
|
|
|
|
|
2023-07-07 08:58:16 +00:00
|
|
|
namespace benchmark {
|
|
|
|
|
|
|
|
BM_DECLARE_string(benchmark_perf_counters);
|
|
|
|
|
|
|
|
} // namespace benchmark
|
|
|
|
|
2021-06-29 10:06:53 +00:00
|
|
|
static void BM_Simple(benchmark::State& state) {
|
2021-04-28 08:25:29 +00:00
|
|
|
for (auto _ : state) {
|
2024-02-19 15:22:35 +00:00
|
|
|
auto iterations = double(state.iterations()) * double(state.iterations());
|
2023-03-06 14:47:54 +00:00
|
|
|
benchmark::DoNotOptimize(iterations);
|
2021-04-28 08:25:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_Simple);
|
|
|
|
ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Simple\",$"}});
|
|
|
|
|
2023-08-11 11:46:36 +00:00
|
|
|
const int kIters = 1000000;
|
|
|
|
|
|
|
|
void BM_WithoutPauseResume(benchmark::State& state) {
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
for (auto i = 0; i < kIters; ++i) {
|
|
|
|
n = 1 - n;
|
|
|
|
benchmark::DoNotOptimize(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCHMARK(BM_WithoutPauseResume);
|
|
|
|
ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_WithoutPauseResume\",$"}});
|
|
|
|
|
|
|
|
void BM_WithPauseResume(benchmark::State& state) {
|
|
|
|
int m = 0, n = 0;
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
for (auto i = 0; i < kIters; ++i) {
|
|
|
|
n = 1 - n;
|
|
|
|
benchmark::DoNotOptimize(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.PauseTiming();
|
|
|
|
for (auto j = 0; j < kIters; ++j) {
|
|
|
|
m = 1 - m;
|
|
|
|
benchmark::DoNotOptimize(m);
|
|
|
|
}
|
|
|
|
state.ResumeTiming();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCHMARK(BM_WithPauseResume);
|
|
|
|
|
|
|
|
ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_WithPauseResume\",$"}});
|
|
|
|
|
2021-06-29 10:06:53 +00:00
|
|
|
static void CheckSimple(Results const& e) {
|
2021-04-28 08:25:29 +00:00
|
|
|
CHECK_COUNTER_VALUE(e, double, "CYCLES", GT, 0);
|
|
|
|
}
|
2023-08-11 11:46:36 +00:00
|
|
|
|
|
|
|
double withoutPauseResumeInstrCount = 0.0;
|
|
|
|
double withPauseResumeInstrCount = 0.0;
|
|
|
|
|
2023-08-21 14:35:42 +00:00
|
|
|
static void SaveInstrCountWithoutResume(Results const& e) {
|
|
|
|
withoutPauseResumeInstrCount = e.GetAs<double>("INSTRUCTIONS");
|
2023-08-11 11:46:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 14:35:42 +00:00
|
|
|
static void SaveInstrCountWithResume(Results const& e) {
|
|
|
|
withPauseResumeInstrCount = e.GetAs<double>("INSTRUCTIONS");
|
2023-08-11 11:46:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 08:25:29 +00:00
|
|
|
CHECK_BENCHMARK_RESULTS("BM_Simple", &CheckSimple);
|
2023-08-21 14:35:42 +00:00
|
|
|
CHECK_BENCHMARK_RESULTS("BM_WithoutPauseResume", &SaveInstrCountWithoutResume);
|
|
|
|
CHECK_BENCHMARK_RESULTS("BM_WithPauseResume", &SaveInstrCountWithResume);
|
2021-04-28 08:25:29 +00:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
if (!benchmark::internal::PerfCounters::kSupported) {
|
|
|
|
return 0;
|
|
|
|
}
|
2023-08-21 14:04:50 +00:00
|
|
|
benchmark::FLAGS_benchmark_perf_counters = "CYCLES,INSTRUCTIONS";
|
2023-07-07 08:58:16 +00:00
|
|
|
benchmark::internal::PerfCounters::Initialize();
|
2021-04-28 08:25:29 +00:00
|
|
|
RunOutputTests(argc, argv);
|
2023-08-11 11:46:36 +00:00
|
|
|
|
|
|
|
BM_CHECK_GT(withPauseResumeInstrCount, kIters);
|
|
|
|
BM_CHECK_GT(withoutPauseResumeInstrCount, kIters);
|
|
|
|
BM_CHECK_LT(withPauseResumeInstrCount, 1.5 * withoutPauseResumeInstrCount);
|
2021-04-28 08:25:29 +00:00
|
|
|
}
|