2021-06-02 15:06:45 +00:00
|
|
|
#include <algorithm>
|
2015-03-13 00:27:29 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdint>
|
2015-10-05 08:43:06 +00:00
|
|
|
#include <cstdlib>
|
2015-03-10 03:30:14 +00:00
|
|
|
#include <iostream>
|
2015-03-17 16:31:46 +00:00
|
|
|
#include <limits>
|
2015-03-10 03:30:14 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2021-06-02 15:06:45 +00:00
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
2015-03-10 03:30:14 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-03-17 18:15:16 +00:00
|
|
|
class TestReporter : public benchmark::ConsoleReporter {
|
2015-03-10 03:30:14 +00:00
|
|
|
public:
|
2023-01-09 17:52:18 +00:00
|
|
|
bool ReportContext(const Context& context) override {
|
2015-03-10 03:30:14 +00:00
|
|
|
return ConsoleReporter::ReportContext(context);
|
|
|
|
};
|
|
|
|
|
2023-01-09 17:52:18 +00:00
|
|
|
void ReportRuns(const std::vector<Run>& report) override {
|
2015-03-10 03:30:14 +00:00
|
|
|
++count_;
|
2022-05-01 18:56:30 +00:00
|
|
|
max_family_index_ = std::max(max_family_index_, report[0].family_index);
|
2015-03-10 03:30:14 +00:00
|
|
|
ConsoleReporter::ReportRuns(report);
|
|
|
|
};
|
|
|
|
|
2021-06-02 15:06:45 +00:00
|
|
|
TestReporter() : count_(0), max_family_index_(0) {}
|
2015-03-10 03:30:14 +00:00
|
|
|
|
2023-01-09 17:52:18 +00:00
|
|
|
~TestReporter() override {}
|
2015-03-10 03:30:14 +00:00
|
|
|
|
2022-05-01 18:56:30 +00:00
|
|
|
int GetCount() const { return count_; }
|
2015-03-10 03:30:14 +00:00
|
|
|
|
2022-05-01 18:56:30 +00:00
|
|
|
int64_t GetMaxFamilyIndex() const { return max_family_index_; }
|
2021-06-02 15:06:45 +00:00
|
|
|
|
2015-03-10 03:30:14 +00:00
|
|
|
private:
|
2022-05-01 18:56:30 +00:00
|
|
|
mutable int count_;
|
|
|
|
mutable int64_t max_family_index_;
|
2015-03-10 03:30:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
2015-03-31 04:05:02 +00:00
|
|
|
static void NoPrefix(benchmark::State& state) {
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
2015-03-10 03:30:14 +00:00
|
|
|
}
|
2015-03-31 04:05:02 +00:00
|
|
|
BENCHMARK(NoPrefix);
|
2015-03-10 03:30:14 +00:00
|
|
|
|
2015-03-31 04:05:02 +00:00
|
|
|
static void BM_Foo(benchmark::State& state) {
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
2015-03-10 03:30:14 +00:00
|
|
|
}
|
2015-03-31 04:05:02 +00:00
|
|
|
BENCHMARK(BM_Foo);
|
|
|
|
|
|
|
|
static void BM_Bar(benchmark::State& state) {
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
2015-03-31 04:05:02 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_Bar);
|
|
|
|
|
|
|
|
static void BM_FooBar(benchmark::State& state) {
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
2015-03-31 04:05:02 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_FooBar);
|
|
|
|
|
|
|
|
static void BM_FooBa(benchmark::State& state) {
|
2017-10-17 18:17:02 +00:00
|
|
|
for (auto _ : state) {
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
2015-03-31 04:05:02 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_FooBa);
|
|
|
|
|
2021-11-10 16:22:31 +00:00
|
|
|
int main(int argc, char** argv) {
|
2016-05-24 06:42:11 +00:00
|
|
|
bool list_only = false;
|
2016-10-07 18:04:50 +00:00
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
list_only |= std::string(argv[i]).find("--benchmark_list_tests") !=
|
|
|
|
std::string::npos;
|
2016-05-24 06:42:11 +00:00
|
|
|
|
2015-03-10 03:30:14 +00:00
|
|
|
benchmark::Initialize(&argc, argv);
|
|
|
|
|
|
|
|
TestReporter test_reporter;
|
2022-05-01 18:56:30 +00:00
|
|
|
const int64_t returned_count =
|
|
|
|
static_cast<int64_t>(benchmark::RunSpecifiedBenchmarks(&test_reporter));
|
2015-03-10 03:30:14 +00:00
|
|
|
|
2015-10-07 07:12:56 +00:00
|
|
|
if (argc == 2) {
|
|
|
|
// Make sure we ran all of the tests
|
|
|
|
std::stringstream ss(argv[1]);
|
2022-05-01 18:56:30 +00:00
|
|
|
int64_t expected_return;
|
2016-05-24 06:42:11 +00:00
|
|
|
ss >> expected_return;
|
2015-10-07 07:12:56 +00:00
|
|
|
|
2016-05-24 06:42:11 +00:00
|
|
|
if (returned_count != expected_return) {
|
|
|
|
std::cerr << "ERROR: Expected " << expected_return
|
|
|
|
<< " tests to match the filter but returned_count = "
|
|
|
|
<< returned_count << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-05-01 18:56:30 +00:00
|
|
|
const int64_t expected_reports = list_only ? 0 : expected_return;
|
|
|
|
const int64_t reports_count = test_reporter.GetCount();
|
2016-05-24 06:42:11 +00:00
|
|
|
if (reports_count != expected_reports) {
|
|
|
|
std::cerr << "ERROR: Expected " << expected_reports
|
|
|
|
<< " tests to be run but reported_count = " << reports_count
|
|
|
|
<< std::endl;
|
2015-10-07 07:12:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-06-02 15:06:45 +00:00
|
|
|
|
2022-05-01 18:56:30 +00:00
|
|
|
const int64_t max_family_index = test_reporter.GetMaxFamilyIndex();
|
|
|
|
const int64_t num_families = reports_count == 0 ? 0 : 1 + max_family_index;
|
2021-06-02 15:06:45 +00:00
|
|
|
if (num_families != expected_reports) {
|
|
|
|
std::cerr << "ERROR: Expected " << expected_reports
|
|
|
|
<< " test families to be run but num_families = "
|
|
|
|
<< num_families << std::endl;
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-10 03:30:14 +00:00
|
|
|
}
|
2016-05-24 06:42:11 +00:00
|
|
|
|
2015-10-07 07:12:56 +00:00
|
|
|
return 0;
|
2015-03-10 03:30:14 +00:00
|
|
|
}
|