2015-03-10 11:30:14 +08:00
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
2015-03-13 08:27:29 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
2015-03-10 11:30:14 +08:00
|
|
|
|
|
|
|
#include <iostream>
|
2015-03-18 00:31:46 +08:00
|
|
|
#include <limits>
|
2015-03-10 11:30:14 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-03-18 02:15:16 +08:00
|
|
|
class TestReporter : public benchmark::ConsoleReporter {
|
2015-03-10 11:30:14 +08:00
|
|
|
public:
|
2015-03-18 04:16:36 +08:00
|
|
|
virtual bool ReportContext(const Context& context) {
|
2015-03-10 11:30:14 +08:00
|
|
|
return ConsoleReporter::ReportContext(context);
|
|
|
|
};
|
|
|
|
|
2015-03-18 04:16:36 +08:00
|
|
|
virtual void ReportRuns(const std::vector<Run>& report) {
|
2015-03-10 11:30:14 +08:00
|
|
|
++count_;
|
|
|
|
ConsoleReporter::ReportRuns(report);
|
|
|
|
};
|
|
|
|
|
|
|
|
TestReporter() : count_(0) {}
|
|
|
|
|
|
|
|
virtual ~TestReporter() {}
|
|
|
|
|
|
|
|
size_t GetCount() const {
|
|
|
|
return count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable size_t count_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
2015-03-31 12:05:02 +08:00
|
|
|
|
|
|
|
static void NoPrefix(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {}
|
2015-03-10 11:30:14 +08:00
|
|
|
}
|
2015-03-31 12:05:02 +08:00
|
|
|
BENCHMARK(NoPrefix);
|
2015-03-10 11:30:14 +08:00
|
|
|
|
2015-03-31 12:05:02 +08:00
|
|
|
static void BM_Foo(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {}
|
2015-03-10 11:30:14 +08:00
|
|
|
}
|
2015-03-31 12:05:02 +08:00
|
|
|
BENCHMARK(BM_Foo);
|
|
|
|
|
|
|
|
|
|
|
|
static void BM_Bar(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_Bar);
|
|
|
|
|
|
|
|
|
|
|
|
static void BM_FooBar(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_FooBar);
|
|
|
|
|
|
|
|
|
|
|
|
static void BM_FooBa(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_FooBa);
|
|
|
|
|
|
|
|
|
2015-03-10 11:30:14 +08:00
|
|
|
|
2015-10-01 04:14:50 +08:00
|
|
|
int main(int argc, char* argv[]) {
|
2015-03-10 11:30:14 +08:00
|
|
|
benchmark::Initialize(&argc, argv);
|
|
|
|
|
|
|
|
TestReporter test_reporter;
|
|
|
|
benchmark::RunSpecifiedBenchmarks(&test_reporter);
|
|
|
|
|
|
|
|
// Make sure we ran all of the tests
|
|
|
|
const size_t count = test_reporter.GetCount();
|
|
|
|
const size_t expected = (argc == 2) ? std::stoul(argv[1]) : count;
|
|
|
|
if (count != expected) {
|
|
|
|
std::cerr << "ERROR: Expected " << expected << " tests to be ran but only "
|
|
|
|
<< count << " completed" << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|