Skip to content

Commit

Permalink
Reformat thanks to clang format.
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenarnst committed May 24, 2024
1 parent 32afbca commit b086a63
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 35 deletions.
14 changes: 7 additions & 7 deletions include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ template <class Q> int BM_Sequential(benchmark::State& state) {
}
BENCHMARK_TEMPLATE(BM_Sequential, WaitQueue<int>)->Range(1<<0, 1<<10);
Use `Benchmark::MinTime(double t)` to set the minimum time used to determine how long
to run the benchmark. This option overrides the `benchmark_min_time` flag.
Use `Benchmark::MinTime(double t)` to set the minimum time used to determine how
long to run the benchmark. This option overrides the `benchmark_min_time` flag.
If a benchmark measures time manually, use `Benchmark::MinRelAccuracy(double r)` to set
the required minimum relative accuracy used to determine how long to run the benchmark.
This option overrides the `benchmark_min_rel_accuracy` flag.
If a benchmark measures time manually, use `Benchmark::MinRelAccuracy(double r)`
to set the required minimum relative accuracy used to determine how long to run
the benchmark. This option overrides the `benchmark_min_rel_accuracy` flag.
void BM_test(benchmark::State& state) {
... body ...
Expand Down Expand Up @@ -1208,8 +1208,8 @@ class BENCHMARK_EXPORT Benchmark {
// the `benchmark_min_rel_accuracy` flag.
// REQUIRES: `r > 0`, `Iterations` has not been called on this benchmark, and
// time is measured manually, i.e., `UseManualTime` has been called on this
// benchmark and each benchmark iteration should call `SetIterationTime(seconds)`
// to report the measured time.
// benchmark and each benchmark iteration should call
// `SetIterationTime(seconds)` to report the measured time.
Benchmark* MinRelAccuracy(double r);

// Set the minimum amount of time to run the benchmark before taking runtimes
Expand Down
10 changes: 5 additions & 5 deletions src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ BM_DEFINE_bool(benchmark_list_tests, false);
// linked into the binary are run.
BM_DEFINE_string(benchmark_filter, "");

// Specification of either an exact number of iterations (specified as `<integer>x`)
// or a minimum number of seconds (specified as `<float>s`) used to determine how
// long to run the benchmark.
// Specification of either an exact number of iterations (specified as
// `<integer>x`) or a minimum number of seconds (specified as `<float>s`) used
// to determine how long to run the benchmark.
//
// If the latter format (ie., min seconds) is used, the system may run the benchmark longer
// until the results are considered significant.
// If the latter format (ie., min seconds) is used, the system may run
// the benchmark longer until the results are considered significant.
//
// For backward compatibility, the `s` suffix may be omitted, in which case,
// the specified number is interpreted as the number of seconds.
Expand Down
3 changes: 2 additions & 1 deletion src/benchmark_api_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ BenchmarkInstance::BenchmarkInstance(Benchmark* benchmark, int family_idx,
}

if (!IsZero(benchmark->min_rel_accuracy_)) {
name_.min_rel_accuracy = StrFormat("min_rel_accuracy:%0.3f", benchmark_.min_rel_accuracy_);
name_.min_rel_accuracy =
StrFormat("min_rel_accuracy:%0.3f", benchmark_.min_rel_accuracy_);
}

if (!IsZero(benchmark->min_warmup_time_)) {
Expand Down
32 changes: 20 additions & 12 deletions src/benchmark_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ BenchmarkRunner::BenchmarkRunner(
reports_for_family(reports_for_family_),
parsed_benchtime_flag(ParseBenchMinTime(FLAGS_benchmark_min_time)),
min_time(ComputeMinTime(b, parsed_benchtime_flag)),
min_rel_accuracy(!IsZero(b.min_rel_accuracy())
? b.min_rel_accuracy()
: FLAGS_benchmark_min_rel_accuracy),
min_rel_accuracy(!IsZero(b.min_rel_accuracy())
? b.min_rel_accuracy()
: FLAGS_benchmark_min_rel_accuracy),
min_warmup_time((!IsZero(b.min_time()) && b.min_warmup_time() > 0.0)
? b.min_warmup_time()
: FLAGS_benchmark_min_warmup_time),
Expand Down Expand Up @@ -332,7 +332,8 @@ IterationCount BenchmarkRunner::PredictNumItersNeeded(
multiplier = is_significant ? multiplier : 10.0;

if (!IsZero(GetMinRelAccuracy())) {
multiplier = std::max(multiplier, GetRelAccuracy(i) * 1.4 / GetMinRelAccuracy());
multiplier =
std::max(multiplier, GetRelAccuracy(i) * 1.4 / GetMinRelAccuracy());
}

// So what seems to be the sufficiently-large iteration count? Round up.
Expand All @@ -354,9 +355,10 @@ bool BenchmarkRunner::ShouldReportIterationResults(
return i.results.skipped_ ||
// Too many iterations already.
i.iters >= kMaxIterations ||
// We have applied for enough time and the relative accuracy is good enough.
// Relative accuracy is checked only for user provided timers.
(HasSufficientTimeToApply(i) && (!b.use_manual_time() || HasSufficientRelAccuracy(i)));
// We have applied for enough time and the relative accuracy is good
// enough. Relative accuracy is checked only for user provided timers.
(HasSufficientTimeToApply(i) &&
(!b.use_manual_time() || HasSufficientRelAccuracy(i)));
}

double BenchmarkRunner::GetMinTimeToApply() const {
Expand All @@ -369,19 +371,25 @@ double BenchmarkRunner::GetMinTimeToApply() const {
}

double BenchmarkRunner::GetRelAccuracy(const IterationResults& i) const {
return std::sqrt(i.seconds_pow2 / i.iters - std::pow(i.seconds / i.iters, 2.)) / (i.seconds / i.iters) / sqrt(i.iters);
return std::sqrt(i.seconds_pow2 / i.iters -
std::pow(i.seconds / i.iters, 2.)) /
(i.seconds / i.iters) / sqrt(i.iters);
}

bool BenchmarkRunner::HasSufficientTimeToApply(const IterationResults& i) const {
bool BenchmarkRunner::HasSufficientTimeToApply(
const IterationResults& i) const {
return i.seconds >= GetMinTimeToApply() ||
// CPU time is specified but the elapsed real time greatly exceeds
// the minimum time.
// Note that user provided timers are except from this test.
(!b.use_manual_time() && i.results.real_time_used >= 5 * GetMinTimeToApply());
(!b.use_manual_time() &&
i.results.real_time_used >= 5 * GetMinTimeToApply());
}

bool BenchmarkRunner::HasSufficientRelAccuracy(const IterationResults& i) const {
return (IsZero(GetMinRelAccuracy()) || (GetRelAccuracy(i) <= GetMinRelAccuracy()));
bool BenchmarkRunner::HasSufficientRelAccuracy(
const IterationResults& i) const {
return (IsZero(GetMinRelAccuracy()) ||
(GetRelAccuracy(i) <= GetMinRelAccuracy()));
}

void BenchmarkRunner::FinishWarmUp(const IterationCount& i) {
Expand Down
4 changes: 2 additions & 2 deletions src/thread_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class ThreadTimer {

// Called by each thread
void SetIterationTime(double seconds) {
manual_time_used_ += seconds;
manual_time_used_pow2_ += std::pow(seconds, 2.);
manual_time_used_ += seconds;
manual_time_used_pow2_ += std::pow(seconds, 2.);
}

bool running() const { return running_; }
Expand Down
23 changes: 15 additions & 8 deletions test/benchmark_min_rel_accuracy_flag_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#include "benchmark/benchmark.h"

// Tests that if a benchmark measures time manually, we can specify the required relative accuracy with
// --benchmark_min_rel_accuracy=<min_rel_accuracy>.
// Tests that if a benchmark measures time manually, we can specify the required
// relative accuracy with --benchmark_min_rel_accuracy=<min_rel_accuracy>.
namespace {

class TestReporter : public benchmark::ConsoleReporter {
Expand All @@ -22,7 +22,8 @@ class TestReporter : public benchmark::ConsoleReporter {
assert(report.size() == 1);
iters_.push_back(report[0].iterations);
real_accumulated_times_.push_back(report[0].real_accumulated_time);
manual_accumulated_time_pow2s_.push_back(report[0].manual_accumulated_time_pow2);
manual_accumulated_time_pow2s_.push_back(
report[0].manual_accumulated_time_pow2);
ConsoleReporter::ReportRuns(report);
};

Expand Down Expand Up @@ -61,7 +62,8 @@ static void BM_MyBench(benchmark::State& state) {
BENCHMARK(BM_MyBench)->UseManualTime();

int main(int argc, char** argv) {
// Make a fake argv and append the new --benchmark_min_rel_accuracy=<min_rel_accuracy> to it.
// Make a fake argv and append the new
// --benchmark_min_rel_accuracy=<min_rel_accuracy> to it.
int fake_argc = argc + 2;
const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)];
for (int i = 0; i < argc; ++i) fake_argv[i] = argv[i];
Expand All @@ -77,10 +79,15 @@ int main(int argc, char** argv) {

// Check the executed iters.
const benchmark::IterationCount iters = test_reporter.GetIters()[0];
const double real_accumulated_time = test_reporter.GetRealAccumulatedTimes()[0];
const double manual_accumulated_time_pow2 = test_reporter.GetManualAccumulatedTimePow2s()[0];

const double rel_accuracy = std::sqrt(manual_accumulated_time_pow2 / iters - std::pow(real_accumulated_time / iters, 2.)) / (real_accumulated_time / iters) / sqrt(iters);
const double real_accumulated_time =
test_reporter.GetRealAccumulatedTimes()[0];
const double manual_accumulated_time_pow2 =
test_reporter.GetManualAccumulatedTimePow2s()[0];

const double rel_accuracy =
std::sqrt(manual_accumulated_time_pow2 / iters -
std::pow(real_accumulated_time / iters, 2.)) /
(real_accumulated_time / iters) / sqrt(iters);
assert(rel_accuracy <= 0.01);

delete[] fake_argv;
Expand Down

0 comments on commit b086a63

Please sign in to comment.