1
0
mirror of https://github.com/google/benchmark.git synced 2025-04-29 06:20:32 +08:00

Document and diagnose benchmarks that abort early

This commit is contained in:
Eric Fiselier 2015-03-12 16:54:12 -04:00
parent 438adf4c4c
commit ca73737968
2 changed files with 21 additions and 12 deletions
include/benchmark
src

View File

@ -172,16 +172,11 @@ class BenchmarkFamilies;
// benchmark to use.
class State {
public:
State(size_t max_iters, bool has_x, int x, bool has_y, int y, int thread_i)
: started_(false), total_iterations_(0),
has_range_x_(has_x), range_x_(x),
has_range_y_(has_y), range_y_(y),
bytes_processed_(0), items_processed_(0),
thread_index(thread_i),
max_iterations(max_iters)
{}
State(size_t max_iters, bool has_x, int x, bool has_y, int y, int thread_i);
// Returns true iff the benchmark should continue through another iteration.
// NOTE: A benchmark may not return from the test until KeepRunning() has
// returned false.
bool KeepRunning() {
if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
ResumeTiming();
@ -191,6 +186,8 @@ public:
if (BENCHMARK_BUILTIN_EXPECT(!res, false)) {
assert(started_);
PauseTiming();
// Total iterations now is one greater than max iterations. Fix this.
total_iterations_ = max_iterations;
}
return res;
}

View File

@ -248,9 +248,8 @@ class TimerManager {
MutexLock l(lock_);
num_finalized_++;
if (num_finalized_ == num_threads_) {
if (running_) {
InternalStop();
}
CHECK(!running_) <<
"The timer should be stopped before the timer is finalized";
done_->Notify();
}
}
@ -584,7 +583,8 @@ void RunInThread(const benchmark::internal::Benchmark::Instance* b,
ThreadStats* total) EXCLUDES(GetBenchmarkLock()) {
State st(iters, b->has_arg1, b->arg1, b->has_arg2, b->arg2, thread_id);
b->function(st);
CHECK(st.iterations() == st.max_iterations) <<
"Benchmark returned before State::KeepRunning() returned false!";
{
MutexLock l(GetBenchmarkLock());
total->bytes_processed += st.bytes_processed();
@ -715,6 +715,18 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b,
} // namespace
State::State(size_t max_iters, bool has_x, int x, bool has_y, int y,
int thread_i)
: started_(false), total_iterations_(0),
has_range_x_(has_x), range_x_(x),
has_range_y_(has_y), range_y_(y),
bytes_processed_(0), items_processed_(0),
thread_index(thread_i),
max_iterations(max_iters)
{
CHECK(max_iterations != 0) << "At least one iteration must be run";
}
void State::PauseTiming() {
// Add in time accumulated so far
CHECK(running_benchmark);