2023-02-07 11:45:18 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
|
|
|
// Tests that we can specify the number of iterations with
|
|
|
|
// --benchmark_min_time=<NUM>x.
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestReporter : public benchmark::ConsoleReporter {
|
|
|
|
public:
|
|
|
|
virtual bool ReportContext(const Context& context) BENCHMARK_OVERRIDE {
|
|
|
|
return ConsoleReporter::ReportContext(context);
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
|
|
|
|
assert(report.size() == 1);
|
|
|
|
iter_nums_.push_back(report[0].iterations);
|
|
|
|
ConsoleReporter::ReportRuns(report);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestReporter() {}
|
|
|
|
|
|
|
|
virtual ~TestReporter() {}
|
|
|
|
|
2023-02-13 11:18:07 +00:00
|
|
|
const std::vector<benchmark::IterationCount>& GetIters() const {
|
|
|
|
return iter_nums_;
|
|
|
|
}
|
2023-02-07 11:45:18 +00:00
|
|
|
|
|
|
|
private:
|
2023-02-13 11:18:07 +00:00
|
|
|
std::vector<benchmark::IterationCount> iter_nums_;
|
2023-02-07 11:45:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
static void BM_MyBench(benchmark::State& state) {
|
|
|
|
for (auto s : state) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_MyBench);
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// Make a fake argv and append the new --benchmark_min_time=<foo> to it.
|
|
|
|
int fake_argc = argc + 1;
|
2023-05-11 07:40:05 +00:00
|
|
|
const char** fake_argv = new const char*[static_cast<size_t>(fake_argc)];
|
2023-02-07 11:45:18 +00:00
|
|
|
for (int i = 0; i < argc; ++i) fake_argv[i] = argv[i];
|
|
|
|
fake_argv[argc] = "--benchmark_min_time=4x";
|
|
|
|
|
|
|
|
benchmark::Initialize(&fake_argc, const_cast<char**>(fake_argv));
|
|
|
|
|
|
|
|
TestReporter test_reporter;
|
|
|
|
const size_t returned_count =
|
|
|
|
benchmark::RunSpecifiedBenchmarks(&test_reporter, "BM_MyBench");
|
|
|
|
assert(returned_count == 1);
|
|
|
|
|
|
|
|
// Check the executed iters.
|
2023-02-13 11:18:07 +00:00
|
|
|
const std::vector<benchmark::IterationCount> iters = test_reporter.GetIters();
|
2023-02-07 11:45:18 +00:00
|
|
|
assert(!iters.empty() && iters[0] == 4);
|
|
|
|
|
|
|
|
delete[] fake_argv;
|
|
|
|
return 0;
|
|
|
|
}
|