2016-05-23 21:27:43 +00:00
|
|
|
// Testing:
|
|
|
|
// State::PauseTiming()
|
|
|
|
// State::ResumeTiming()
|
|
|
|
// Test that CHECK's within these function diagnose when they are called
|
|
|
|
// outside of the KeepRunning() loop.
|
|
|
|
//
|
|
|
|
// NOTE: Users should NOT include or use src/check.h. This is only done in
|
|
|
|
// order to test library internals.
|
2016-05-23 21:05:55 +00:00
|
|
|
|
2016-05-23 21:38:30 +00:00
|
|
|
#include <cstdlib>
|
2016-10-07 18:04:50 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#include "../src/check.h"
|
2017-07-04 22:31:47 +00:00
|
|
|
#include "benchmark/benchmark.h"
|
2016-05-23 21:38:30 +00:00
|
|
|
|
|
|
|
#if defined(__GNUC__) && !defined(__EXCEPTIONS)
|
|
|
|
#define TEST_HAS_NO_EXCEPTIONS
|
|
|
|
#endif
|
2016-05-23 21:05:55 +00:00
|
|
|
|
2016-05-23 21:27:43 +00:00
|
|
|
void TestHandler() {
|
2016-05-23 21:38:30 +00:00
|
|
|
#ifndef TEST_HAS_NO_EXCEPTIONS
|
2016-05-23 21:05:55 +00:00
|
|
|
throw std::logic_error("");
|
2016-05-23 21:38:30 +00:00
|
|
|
#else
|
|
|
|
std::abort();
|
|
|
|
#endif
|
2016-05-23 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void try_invalid_pause_resume(benchmark::State& state) {
|
2021-11-10 16:22:31 +00:00
|
|
|
#if !defined(TEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) && \
|
|
|
|
!defined(TEST_HAS_NO_EXCEPTIONS)
|
2016-05-23 21:05:55 +00:00
|
|
|
try {
|
|
|
|
state.PauseTiming();
|
|
|
|
std::abort();
|
2016-10-07 18:04:50 +00:00
|
|
|
} catch (std::logic_error const&) {
|
|
|
|
}
|
2016-05-23 21:05:55 +00:00
|
|
|
try {
|
|
|
|
state.ResumeTiming();
|
|
|
|
std::abort();
|
2016-10-07 18:04:50 +00:00
|
|
|
} catch (std::logic_error const&) {
|
|
|
|
}
|
2016-05-23 21:05:55 +00:00
|
|
|
#else
|
2016-10-07 18:04:50 +00:00
|
|
|
(void)state; // avoid unused warning
|
2016-05-23 21:05:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void BM_diagnostic_test(benchmark::State& state) {
|
|
|
|
static bool called_once = false;
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2023-03-06 14:47:54 +00:00
|
|
|
auto iterations = state.iterations();
|
|
|
|
benchmark::DoNotOptimize(iterations);
|
2016-05-23 21:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
|
|
|
called_once = true;
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_diagnostic_test);
|
|
|
|
|
2017-10-17 18:17:02 +00:00
|
|
|
void BM_diagnostic_test_keep_running(benchmark::State& state) {
|
|
|
|
static bool called_once = false;
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
2021-11-10 16:22:31 +00:00
|
|
|
while (state.KeepRunning()) {
|
2023-03-06 14:47:54 +00:00
|
|
|
auto iterations = state.iterations();
|
|
|
|
benchmark::DoNotOptimize(iterations);
|
2017-10-17 18:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (called_once == false) try_invalid_pause_resume(state);
|
|
|
|
|
|
|
|
called_once = true;
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_diagnostic_test_keep_running);
|
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
2016-05-23 21:27:43 +00:00
|
|
|
benchmark::internal::GetAbortHandler() = &TestHandler;
|
2016-05-23 21:05:55 +00:00
|
|
|
benchmark::Initialize(&argc, argv);
|
|
|
|
benchmark::RunSpecifiedBenchmarks();
|
2016-05-23 21:07:54 +00:00
|
|
|
}
|