2018-09-12 13:26:17 +00:00
|
|
|
#include <cstdio>
|
2018-06-01 10:14:19 +00:00
|
|
|
#include <cstring>
|
2018-09-12 13:26:17 +00:00
|
|
|
#include <fstream>
|
2016-08-28 19:24:16 +00:00
|
|
|
#include <iostream>
|
2016-10-07 18:04:50 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2018-11-30 03:51:44 +00:00
|
|
|
#include <random>
|
2016-08-28 19:24:16 +00:00
|
|
|
#include <sstream>
|
2018-09-12 13:26:17 +00:00
|
|
|
#include <streambuf>
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2018-06-01 10:14:19 +00:00
|
|
|
#include "../src/benchmark_api_internal.h"
|
2016-10-07 18:04:50 +00:00
|
|
|
#include "../src/check.h" // NOTE: check.h is for internal use only!
|
2021-06-24 17:55:37 +00:00
|
|
|
#include "../src/log.h" // NOTE: log.h is for internal use only
|
2016-10-07 18:04:50 +00:00
|
|
|
#include "../src/re.h" // NOTE: re.h is for internal use only
|
|
|
|
#include "output_test.h"
|
2016-08-28 19:24:16 +00:00
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// ------------------------------ Internals -------------------------------- //
|
|
|
|
// ========================================================================= //
|
2016-10-07 18:04:50 +00:00
|
|
|
namespace internal {
|
|
|
|
namespace {
|
2016-08-28 19:24:16 +00:00
|
|
|
|
|
|
|
using TestCaseList = std::vector<TestCase>;
|
|
|
|
|
|
|
|
// Use a vector because the order elements are added matters during iteration.
|
|
|
|
// std::map/unordered_map don't guarantee that.
|
|
|
|
// For example:
|
|
|
|
// SetSubstitutions({{"%HelloWorld", "Hello"}, {"%Hello", "Hi"}});
|
|
|
|
// Substitute("%HelloWorld") // Always expands to Hello.
|
|
|
|
using SubMap = std::vector<std::pair<std::string, std::string>>;
|
|
|
|
|
|
|
|
TestCaseList& GetTestCaseList(TestCaseID ID) {
|
2016-10-07 18:04:50 +00:00
|
|
|
// Uses function-local statics to ensure initialization occurs
|
|
|
|
// before first use.
|
|
|
|
static TestCaseList lists[TC_NumID];
|
|
|
|
return lists[ID];
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SubMap& GetSubstitutions() {
|
2016-10-07 18:04:50 +00:00
|
|
|
// Don't use 'dec_re' from header because it may not yet be initialized.
|
2018-06-01 10:14:19 +00:00
|
|
|
// clang-format off
|
2017-03-28 00:30:54 +00:00
|
|
|
static std::string safe_dec_re = "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?";
|
2018-12-14 03:49:21 +00:00
|
|
|
static std::string time_re = "([0-9]+[.])?[0-9]+";
|
2021-09-06 08:33:27 +00:00
|
|
|
static std::string percentage_re = "[0-9]+[.][0-9]{2}";
|
2016-10-07 18:04:50 +00:00
|
|
|
static SubMap map = {
|
|
|
|
{"%float", "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?"},
|
2017-04-28 14:02:27 +00:00
|
|
|
// human-readable float
|
|
|
|
{"%hrfloat", "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?[kMGTPEZYmunpfazy]?"},
|
2021-09-06 08:33:27 +00:00
|
|
|
{"%percentage", percentage_re},
|
2016-10-07 18:04:50 +00:00
|
|
|
{"%int", "[ ]*[0-9]+"},
|
|
|
|
{" %s ", "[ ]+"},
|
2018-12-14 03:49:21 +00:00
|
|
|
{"%time", "[ ]*" + time_re + "[ ]+ns"},
|
|
|
|
{"%console_report", "[ ]*" + time_re + "[ ]+ns [ ]*" + time_re + "[ ]+ns [ ]*[0-9]+"},
|
2021-09-06 08:33:27 +00:00
|
|
|
{"%console_percentage_report", "[ ]*" + percentage_re + "[ ]+% [ ]*" + percentage_re + "[ ]+% [ ]*[0-9]+"},
|
2020-12-21 17:15:58 +00:00
|
|
|
{"%console_us_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us [ ]*[0-9]+"},
|
|
|
|
{"%console_ms_report", "[ ]*" + time_re + "[ ]+ms [ ]*" + time_re + "[ ]+ms [ ]*[0-9]+"},
|
|
|
|
{"%console_s_report", "[ ]*" + time_re + "[ ]+s [ ]*" + time_re + "[ ]+s [ ]*[0-9]+"},
|
2018-12-14 03:49:21 +00:00
|
|
|
{"%console_time_only_report", "[ ]*" + time_re + "[ ]+ns [ ]*" + time_re + "[ ]+ns"},
|
|
|
|
{"%console_us_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us [ ]*[0-9]+"},
|
|
|
|
{"%console_us_time_only_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us"},
|
2017-04-27 18:24:06 +00:00
|
|
|
{"%csv_header",
|
|
|
|
"name,iterations,real_time,cpu_time,time_unit,bytes_per_second,"
|
|
|
|
"items_per_second,label,error_occurred,error_message"},
|
2017-03-28 00:30:54 +00:00
|
|
|
{"%csv_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,,,,,"},
|
|
|
|
{"%csv_us_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",us,,,,,"},
|
2020-12-21 17:15:58 +00:00
|
|
|
{"%csv_ms_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ms,,,,,"},
|
|
|
|
{"%csv_s_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",s,,,,,"},
|
2016-10-28 16:13:57 +00:00
|
|
|
{"%csv_bytes_report",
|
2017-03-28 00:30:54 +00:00
|
|
|
"[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns," + safe_dec_re + ",,,,"},
|
2016-10-28 16:13:57 +00:00
|
|
|
{"%csv_items_report",
|
2017-03-28 00:30:54 +00:00
|
|
|
"[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,," + safe_dec_re + ",,,"},
|
2017-04-27 18:25:20 +00:00
|
|
|
{"%csv_bytes_items_report",
|
|
|
|
"[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns," + safe_dec_re +
|
|
|
|
"," + safe_dec_re + ",,,"},
|
2017-03-28 00:30:54 +00:00
|
|
|
{"%csv_label_report_begin", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,,,"},
|
2016-10-28 16:13:57 +00:00
|
|
|
{"%csv_label_report_end", ",,"}};
|
2018-06-01 10:14:19 +00:00
|
|
|
// clang-format on
|
2016-10-07 18:04:50 +00:00
|
|
|
return map;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string PerformSubstitutions(std::string source) {
|
2016-10-07 18:04:50 +00:00
|
|
|
SubMap const& subs = GetSubstitutions();
|
|
|
|
using SizeT = std::string::size_type;
|
|
|
|
for (auto const& KV : subs) {
|
|
|
|
SizeT pos;
|
|
|
|
SizeT next_start = 0;
|
|
|
|
while ((pos = source.find(KV.first, next_start)) != std::string::npos) {
|
|
|
|
next_start = pos + KV.second.size();
|
|
|
|
source.replace(pos, KV.first.size(), KV.second);
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
|
|
|
return source;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckCase(std::stringstream& remaining_output, TestCase const& TC,
|
2016-10-07 18:04:50 +00:00
|
|
|
TestCaseList const& not_checks) {
|
|
|
|
std::string first_line;
|
|
|
|
bool on_first = true;
|
|
|
|
std::string line;
|
|
|
|
while (remaining_output.eof() == false) {
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(remaining_output.good());
|
2016-10-07 18:04:50 +00:00
|
|
|
std::getline(remaining_output, line);
|
|
|
|
if (on_first) {
|
|
|
|
first_line = line;
|
|
|
|
on_first = false;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
2016-10-07 18:04:50 +00:00
|
|
|
for (const auto& NC : not_checks) {
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(!NC.regex->Match(line))
|
2016-10-07 18:04:50 +00:00
|
|
|
<< "Unexpected match for line \"" << line << "\" for MR_Not regex \""
|
|
|
|
<< NC.regex_str << "\""
|
|
|
|
<< "\n actual regex string \"" << TC.substituted_regex << "\""
|
|
|
|
<< "\n started matching near: " << first_line;
|
|
|
|
}
|
|
|
|
if (TC.regex->Match(line)) return;
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(TC.match_rule != MR_Next)
|
2016-10-07 18:04:50 +00:00
|
|
|
<< "Expected line \"" << line << "\" to match regex \"" << TC.regex_str
|
|
|
|
<< "\""
|
2016-08-28 19:24:16 +00:00
|
|
|
<< "\n actual regex string \"" << TC.substituted_regex << "\""
|
|
|
|
<< "\n started matching near: " << first_line;
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(remaining_output.eof() == false)
|
2016-10-07 18:04:50 +00:00
|
|
|
<< "End of output reached before match for regex \"" << TC.regex_str
|
|
|
|
<< "\" was found"
|
|
|
|
<< "\n actual regex string \"" << TC.substituted_regex << "\""
|
|
|
|
<< "\n started matching near: " << first_line;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckCases(TestCaseList const& checks, std::stringstream& output) {
|
2016-10-07 18:04:50 +00:00
|
|
|
std::vector<TestCase> not_checks;
|
|
|
|
for (size_t i = 0; i < checks.size(); ++i) {
|
|
|
|
const auto& TC = checks[i];
|
|
|
|
if (TC.match_rule == MR_Not) {
|
|
|
|
not_checks.push_back(TC);
|
|
|
|
continue;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
2016-10-07 18:04:50 +00:00
|
|
|
CheckCase(output, TC, not_checks);
|
|
|
|
not_checks.clear();
|
|
|
|
}
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class TestReporter : public benchmark::BenchmarkReporter {
|
2016-10-07 18:04:50 +00:00
|
|
|
public:
|
2016-08-28 19:24:16 +00:00
|
|
|
TestReporter(std::vector<benchmark::BenchmarkReporter*> reps)
|
2021-12-06 11:18:04 +00:00
|
|
|
: reporters_(std::move(reps)) {}
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2023-01-09 17:52:18 +00:00
|
|
|
bool ReportContext(const Context& context) override {
|
2016-08-28 19:24:16 +00:00
|
|
|
bool last_ret = false;
|
|
|
|
bool first = true;
|
|
|
|
for (auto rep : reporters_) {
|
|
|
|
bool new_ret = rep->ReportContext(context);
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(first || new_ret == last_ret)
|
2016-08-28 19:24:16 +00:00
|
|
|
<< "Reports return different values for ReportContext";
|
|
|
|
first = false;
|
|
|
|
last_ret = new_ret;
|
|
|
|
}
|
2016-10-07 18:04:50 +00:00
|
|
|
(void)first;
|
2016-08-28 19:24:16 +00:00
|
|
|
return last_ret;
|
|
|
|
}
|
|
|
|
|
2023-01-09 17:52:18 +00:00
|
|
|
void ReportRuns(const std::vector<Run>& report) override {
|
2016-10-07 18:04:50 +00:00
|
|
|
for (auto rep : reporters_) rep->ReportRuns(report);
|
|
|
|
}
|
2023-01-09 17:52:18 +00:00
|
|
|
void Finalize() override {
|
2016-10-07 18:04:50 +00:00
|
|
|
for (auto rep : reporters_) rep->Finalize();
|
|
|
|
}
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
private:
|
2018-06-01 10:14:19 +00:00
|
|
|
std::vector<benchmark::BenchmarkReporter*> reporters_;
|
2016-08-28 19:24:16 +00:00
|
|
|
};
|
2018-06-01 10:14:19 +00:00
|
|
|
} // namespace
|
2017-04-28 14:02:27 +00:00
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
|
// ========================================================================= //
|
|
|
|
// -------------------------- Results checking ----------------------------- //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
// Utility class to manage subscribers for checking benchmark results.
|
|
|
|
// It works by parsing the CSV output to read the results.
|
|
|
|
class ResultsChecker {
|
|
|
|
public:
|
2018-06-01 10:14:19 +00:00
|
|
|
struct PatternAndFn : public TestCase { // reusing TestCase for its regexes
|
2017-04-29 19:47:32 +00:00
|
|
|
PatternAndFn(const std::string& rx, ResultsCheckFn fn_)
|
2021-12-06 11:18:04 +00:00
|
|
|
: TestCase(rx), fn(std::move(fn_)) {}
|
2017-04-29 18:02:07 +00:00
|
|
|
ResultsCheckFn fn;
|
|
|
|
};
|
2017-04-28 14:02:27 +00:00
|
|
|
|
2018-06-01 10:14:19 +00:00
|
|
|
std::vector<PatternAndFn> check_patterns;
|
|
|
|
std::vector<Results> results;
|
|
|
|
std::vector<std::string> field_names;
|
2017-04-29 18:02:07 +00:00
|
|
|
|
2021-12-06 11:18:04 +00:00
|
|
|
void Add(const std::string& entry_pattern, const ResultsCheckFn& fn);
|
2017-04-28 14:02:27 +00:00
|
|
|
|
|
|
|
void CheckResults(std::stringstream& output);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void SetHeader_(const std::string& csv_header);
|
|
|
|
void SetValues_(const std::string& entry_csv_line);
|
|
|
|
|
2018-06-01 10:14:19 +00:00
|
|
|
std::vector<std::string> SplitCsv_(const std::string& line);
|
2017-04-28 14:02:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// store the static ResultsChecker in a function to prevent initialization
|
|
|
|
// order problems
|
|
|
|
ResultsChecker& GetResultsChecker() {
|
|
|
|
static ResultsChecker rc;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a results checker for a benchmark
|
2021-12-06 11:18:04 +00:00
|
|
|
void ResultsChecker::Add(const std::string& entry_pattern,
|
|
|
|
const ResultsCheckFn& fn) {
|
2017-04-29 18:02:07 +00:00
|
|
|
check_patterns.emplace_back(entry_pattern, fn);
|
2017-04-28 14:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the results of all subscribed benchmarks
|
2017-05-01 21:27:56 +00:00
|
|
|
void ResultsChecker::CheckResults(std::stringstream& output) {
|
2017-04-28 14:02:27 +00:00
|
|
|
// first reset the stream to the start
|
|
|
|
{
|
2018-11-22 02:38:25 +00:00
|
|
|
auto start = std::stringstream::pos_type(0);
|
2017-04-28 14:02:27 +00:00
|
|
|
// clear before calling tellg()
|
|
|
|
output.clear();
|
|
|
|
// seek to zero only when needed
|
2018-06-01 10:14:19 +00:00
|
|
|
if (output.tellg() > start) output.seekg(start);
|
2017-04-28 14:02:27 +00:00
|
|
|
// and just in case
|
|
|
|
output.clear();
|
|
|
|
}
|
|
|
|
// now go over every line and publish it to the ResultsChecker
|
|
|
|
std::string line;
|
|
|
|
bool on_first = true;
|
|
|
|
while (output.eof() == false) {
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(output.good());
|
2017-04-28 14:02:27 +00:00
|
|
|
std::getline(output, line);
|
|
|
|
if (on_first) {
|
2018-06-01 10:14:19 +00:00
|
|
|
SetHeader_(line); // this is important
|
2017-04-28 14:02:27 +00:00
|
|
|
on_first = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SetValues_(line);
|
|
|
|
}
|
|
|
|
// finally we can call the subscribed check functions
|
2018-06-01 10:14:19 +00:00
|
|
|
for (const auto& p : check_patterns) {
|
2021-06-24 17:55:37 +00:00
|
|
|
BM_VLOG(2) << "--------------------------------\n";
|
|
|
|
BM_VLOG(2) << "checking for benchmarks matching " << p.regex_str << "...\n";
|
2018-06-01 10:14:19 +00:00
|
|
|
for (const auto& r : results) {
|
|
|
|
if (!p.regex->Match(r.name)) {
|
2021-06-24 17:55:37 +00:00
|
|
|
BM_VLOG(2) << p.regex_str << " is not matched by " << r.name << "\n";
|
2017-04-29 18:02:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-01-16 12:28:48 +00:00
|
|
|
BM_VLOG(2) << p.regex_str << " is matched by " << r.name << "\n";
|
2021-06-24 17:55:37 +00:00
|
|
|
BM_VLOG(1) << "Checking results of " << r.name << ": ... \n";
|
2017-04-29 18:02:07 +00:00
|
|
|
p.fn(r);
|
2021-06-24 17:55:37 +00:00
|
|
|
BM_VLOG(1) << "Checking results of " << r.name << ": OK.\n";
|
2017-04-29 18:02:07 +00:00
|
|
|
}
|
2017-04-28 14:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for the names in this header
|
|
|
|
void ResultsChecker::SetHeader_(const std::string& csv_header) {
|
2017-04-29 18:02:07 +00:00
|
|
|
field_names = SplitCsv_(csv_header);
|
2017-04-28 14:02:27 +00:00
|
|
|
}
|
|
|
|
|
2017-05-01 21:33:19 +00:00
|
|
|
// set the values for a benchmark
|
2017-04-28 14:02:27 +00:00
|
|
|
void ResultsChecker::SetValues_(const std::string& entry_csv_line) {
|
2018-06-01 10:14:19 +00:00
|
|
|
if (entry_csv_line.empty()) return; // some lines are empty
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(!field_names.empty());
|
2017-04-28 14:02:27 +00:00
|
|
|
auto vals = SplitCsv_(entry_csv_line);
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK_EQ(vals.size(), field_names.size());
|
2018-06-01 10:14:19 +00:00
|
|
|
results.emplace_back(vals[0]); // vals[0] is the benchmark name
|
|
|
|
auto& entry = results.back();
|
2017-04-28 14:02:27 +00:00
|
|
|
for (size_t i = 1, e = vals.size(); i < e; ++i) {
|
2017-04-29 18:02:07 +00:00
|
|
|
entry.values[field_names[i]] = vals[i];
|
2017-04-28 14:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a quick'n'dirty csv splitter (eliminating quotes)
|
2018-06-01 10:14:19 +00:00
|
|
|
std::vector<std::string> ResultsChecker::SplitCsv_(const std::string& line) {
|
|
|
|
std::vector<std::string> out;
|
|
|
|
if (line.empty()) return out;
|
|
|
|
if (!field_names.empty()) out.reserve(field_names.size());
|
2017-04-28 14:02:27 +00:00
|
|
|
size_t prev = 0, pos = line.find_first_of(','), curr = pos;
|
2018-06-01 10:14:19 +00:00
|
|
|
while (pos != line.npos) {
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(curr > 0);
|
2018-06-01 10:14:19 +00:00
|
|
|
if (line[prev] == '"') ++prev;
|
|
|
|
if (line[curr - 1] == '"') --curr;
|
|
|
|
out.push_back(line.substr(prev, curr - prev));
|
2017-04-28 14:02:27 +00:00
|
|
|
prev = pos + 1;
|
|
|
|
pos = line.find_first_of(',', pos + 1);
|
|
|
|
curr = pos;
|
|
|
|
}
|
|
|
|
curr = line.size();
|
2018-06-01 10:14:19 +00:00
|
|
|
if (line[prev] == '"') ++prev;
|
|
|
|
if (line[curr - 1] == '"') --curr;
|
|
|
|
out.push_back(line.substr(prev, curr - prev));
|
2017-04-28 14:02:27 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
} // end namespace internal
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2023-03-14 13:10:27 +00:00
|
|
|
size_t AddChecker(const std::string& bm_name, const ResultsCheckFn& fn) {
|
2018-06-01 10:14:19 +00:00
|
|
|
auto& rc = internal::GetResultsChecker();
|
2017-04-28 14:02:27 +00:00
|
|
|
rc.Add(bm_name, fn);
|
|
|
|
return rc.results.size();
|
|
|
|
}
|
|
|
|
|
2017-04-29 19:01:18 +00:00
|
|
|
int Results::NumThreads() const {
|
2017-04-29 18:40:39 +00:00
|
|
|
auto pos = name.find("/threads:");
|
2018-06-01 10:14:19 +00:00
|
|
|
if (pos == name.npos) return 1;
|
2017-04-29 18:40:39 +00:00
|
|
|
auto end = name.find('/', pos + 9);
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << name.substr(pos + 9, end);
|
|
|
|
int num = 1;
|
|
|
|
ss >> num;
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(!ss.fail());
|
2017-04-29 18:40:39 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2021-11-10 16:22:31 +00:00
|
|
|
double Results::NumIterations() const { return GetAs<double>("iterations"); }
|
2018-06-27 14:45:30 +00:00
|
|
|
|
2017-05-01 21:32:40 +00:00
|
|
|
double Results::GetTime(BenchmarkTime which) const {
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(which == kCpuTime || which == kRealTime);
|
2018-06-01 10:14:19 +00:00
|
|
|
const char* which_str = which == kCpuTime ? "cpu_time" : "real_time";
|
|
|
|
double val = GetAs<double>(which_str);
|
2017-04-29 18:40:39 +00:00
|
|
|
auto unit = Get("time_unit");
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(unit);
|
2018-06-01 10:14:19 +00:00
|
|
|
if (*unit == "ns") {
|
2017-04-29 18:40:39 +00:00
|
|
|
return val * 1.e-9;
|
2023-01-16 12:28:48 +00:00
|
|
|
}
|
|
|
|
if (*unit == "us") {
|
2017-04-29 18:40:39 +00:00
|
|
|
return val * 1.e-6;
|
2023-01-16 12:28:48 +00:00
|
|
|
}
|
|
|
|
if (*unit == "ms") {
|
2017-04-29 18:40:39 +00:00
|
|
|
return val * 1.e-3;
|
2023-01-16 12:28:48 +00:00
|
|
|
}
|
|
|
|
if (*unit == "s") {
|
2017-04-29 18:40:39 +00:00
|
|
|
return val;
|
|
|
|
}
|
2023-01-16 12:28:48 +00:00
|
|
|
BM_CHECK(1 == 0) << "unknown time unit: " << *unit;
|
|
|
|
return 0;
|
2017-04-29 18:40:39 +00:00
|
|
|
}
|
|
|
|
|
2016-08-28 19:24:16 +00:00
|
|
|
// ========================================================================= //
|
|
|
|
// -------------------------- Public API Definitions------------------------ //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
|
|
|
TestCase::TestCase(std::string re, int rule)
|
2016-10-07 18:04:50 +00:00
|
|
|
: regex_str(std::move(re)),
|
|
|
|
match_rule(rule),
|
2016-08-28 19:24:16 +00:00
|
|
|
substituted_regex(internal::PerformSubstitutions(regex_str)),
|
2016-10-07 18:04:50 +00:00
|
|
|
regex(std::make_shared<benchmark::Regex>()) {
|
|
|
|
std::string err_str;
|
2018-06-01 10:14:19 +00:00
|
|
|
regex->Init(substituted_regex, &err_str);
|
2021-06-24 17:21:59 +00:00
|
|
|
BM_CHECK(err_str.empty())
|
|
|
|
<< "Could not construct regex \"" << substituted_regex << "\""
|
|
|
|
<< "\n originally \"" << regex_str << "\""
|
|
|
|
<< "\n got error: " << err_str;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AddCases(TestCaseID ID, std::initializer_list<TestCase> il) {
|
2016-10-07 18:04:50 +00:00
|
|
|
auto& L = internal::GetTestCaseList(ID);
|
|
|
|
L.insert(L.end(), il);
|
|
|
|
return 0;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
int SetSubstitutions(
|
|
|
|
std::initializer_list<std::pair<std::string, std::string>> il) {
|
|
|
|
auto& subs = internal::GetSubstitutions();
|
2016-10-08 03:56:22 +00:00
|
|
|
for (auto KV : il) {
|
2016-10-07 18:04:50 +00:00
|
|
|
bool exists = false;
|
2016-10-08 03:56:22 +00:00
|
|
|
KV.second = internal::PerformSubstitutions(KV.second);
|
2016-10-07 18:04:50 +00:00
|
|
|
for (auto& EKV : subs) {
|
|
|
|
if (EKV.first == KV.first) {
|
2016-10-08 03:56:22 +00:00
|
|
|
EKV.second = std::move(KV.second);
|
2016-10-07 18:04:50 +00:00
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
2016-10-08 03:56:22 +00:00
|
|
|
if (!exists) subs.push_back(std::move(KV));
|
2016-10-07 18:04:50 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-07 19:55:40 +00:00
|
|
|
// Disable deprecated warnings temporarily because we need to reference
|
2020-04-14 09:20:22 +00:00
|
|
|
// CSVReporter but don't want to trigger -Werror=-Wdeprecated-declarations
|
2021-10-21 09:10:38 +00:00
|
|
|
BENCHMARK_DISABLE_DEPRECATED_WARNING
|
|
|
|
|
2016-08-28 19:24:16 +00:00
|
|
|
void RunOutputTests(int argc, char* argv[]) {
|
|
|
|
using internal::GetTestCaseList;
|
|
|
|
benchmark::Initialize(&argc, argv);
|
2018-06-01 10:14:19 +00:00
|
|
|
auto options = benchmark::internal::GetOutputOptions(/*force_no_color*/ true);
|
2017-05-02 19:31:54 +00:00
|
|
|
benchmark::ConsoleReporter CR(options);
|
2016-08-28 19:24:16 +00:00
|
|
|
benchmark::JSONReporter JR;
|
|
|
|
benchmark::CSVReporter CSVR;
|
|
|
|
struct ReporterTest {
|
2023-03-14 13:10:27 +00:00
|
|
|
std::string name;
|
2016-08-28 19:24:16 +00:00
|
|
|
std::vector<TestCase>& output_cases;
|
|
|
|
std::vector<TestCase>& error_cases;
|
|
|
|
benchmark::BenchmarkReporter& reporter;
|
|
|
|
std::stringstream out_stream;
|
|
|
|
std::stringstream err_stream;
|
|
|
|
|
2023-03-14 13:10:27 +00:00
|
|
|
ReporterTest(const std::string& n, std::vector<TestCase>& out_tc,
|
2016-08-28 19:24:16 +00:00
|
|
|
std::vector<TestCase>& err_tc,
|
|
|
|
benchmark::BenchmarkReporter& br)
|
|
|
|
: name(n), output_cases(out_tc), error_cases(err_tc), reporter(br) {
|
2016-10-07 18:04:50 +00:00
|
|
|
reporter.SetOutputStream(&out_stream);
|
|
|
|
reporter.SetErrorStream(&err_stream);
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
|
|
|
} TestCases[] = {
|
2023-03-14 13:10:27 +00:00
|
|
|
{std::string("ConsoleReporter"), GetTestCaseList(TC_ConsoleOut),
|
2016-10-07 18:04:50 +00:00
|
|
|
GetTestCaseList(TC_ConsoleErr), CR},
|
2023-03-14 13:10:27 +00:00
|
|
|
{std::string("JSONReporter"), GetTestCaseList(TC_JSONOut),
|
|
|
|
GetTestCaseList(TC_JSONErr), JR},
|
|
|
|
{std::string("CSVReporter"), GetTestCaseList(TC_CSVOut),
|
|
|
|
GetTestCaseList(TC_CSVErr), CSVR},
|
2016-08-28 19:24:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create the test reporter and run the benchmarks.
|
|
|
|
std::cout << "Running benchmarks...\n";
|
|
|
|
internal::TestReporter test_rep({&CR, &JR, &CSVR});
|
|
|
|
benchmark::RunSpecifiedBenchmarks(&test_rep);
|
|
|
|
|
|
|
|
for (auto& rep_test : TestCases) {
|
2023-03-14 13:10:27 +00:00
|
|
|
std::string msg =
|
|
|
|
std::string("\nTesting ") + rep_test.name + std::string(" Output\n");
|
2016-10-07 18:04:50 +00:00
|
|
|
std::string banner(msg.size() - 1, '-');
|
|
|
|
std::cout << banner << msg << banner << "\n";
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
std::cerr << rep_test.err_stream.str();
|
|
|
|
std::cout << rep_test.out_stream.str();
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
internal::CheckCases(rep_test.error_cases, rep_test.err_stream);
|
|
|
|
internal::CheckCases(rep_test.output_cases, rep_test.out_stream);
|
2016-08-28 19:24:16 +00:00
|
|
|
|
2016-10-07 18:04:50 +00:00
|
|
|
std::cout << "\n";
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
2017-04-28 14:02:27 +00:00
|
|
|
|
|
|
|
// now that we know the output is as expected, we can dispatch
|
|
|
|
// the checks to subscribees.
|
2018-06-01 10:14:19 +00:00
|
|
|
auto& csv = TestCases[2];
|
2017-04-29 19:47:32 +00:00
|
|
|
// would use == but gcc spits a warning
|
2023-03-14 13:10:27 +00:00
|
|
|
BM_CHECK(csv.name == std::string("CSVReporter"));
|
2017-04-28 14:02:27 +00:00
|
|
|
internal::GetResultsChecker().CheckResults(csv.out_stream);
|
2016-08-28 19:24:16 +00:00
|
|
|
}
|
2018-09-12 13:26:17 +00:00
|
|
|
|
2021-10-21 09:10:38 +00:00
|
|
|
BENCHMARK_RESTORE_DEPRECATED_WARNING
|
2019-08-07 19:55:40 +00:00
|
|
|
|
2018-09-12 13:26:17 +00:00
|
|
|
int SubstrCnt(const std::string& haystack, const std::string& pat) {
|
|
|
|
if (pat.length() == 0) return 0;
|
|
|
|
int count = 0;
|
|
|
|
for (size_t offset = haystack.find(pat); offset != std::string::npos;
|
|
|
|
offset = haystack.find(pat, offset + pat.length()))
|
|
|
|
++count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:51:44 +00:00
|
|
|
static char ToHex(int ch) {
|
|
|
|
return ch < 10 ? static_cast<char>('0' + ch)
|
|
|
|
: static_cast<char>('a' + (ch - 10));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char RandomHexChar() {
|
|
|
|
static std::mt19937 rd{std::random_device{}()};
|
|
|
|
static std::uniform_int_distribution<int> mrand{0, 15};
|
|
|
|
return ToHex(mrand(rd));
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetRandomFileName() {
|
|
|
|
std::string model = "test.%%%%%%";
|
2021-11-10 16:22:31 +00:00
|
|
|
for (auto& ch : model) {
|
|
|
|
if (ch == '%') ch = RandomHexChar();
|
2018-11-30 03:51:44 +00:00
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool FileExists(std::string const& name) {
|
|
|
|
std::ifstream in(name.c_str());
|
|
|
|
return in.good();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string GetTempFileName() {
|
|
|
|
// This function attempts to avoid race conditions where two tests
|
|
|
|
// create the same file at the same time. However, it still introduces races
|
|
|
|
// similar to tmpnam.
|
|
|
|
int retries = 3;
|
|
|
|
while (--retries) {
|
|
|
|
std::string name = GetRandomFileName();
|
2021-11-10 16:22:31 +00:00
|
|
|
if (!FileExists(name)) return name;
|
2018-11-30 03:51:44 +00:00
|
|
|
}
|
|
|
|
std::cerr << "Failed to create unique temporary file name" << std::endl;
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:17 +00:00
|
|
|
std::string GetFileReporterOutput(int argc, char* argv[]) {
|
|
|
|
std::vector<char*> new_argv(argv, argv + argc);
|
|
|
|
assert(static_cast<decltype(new_argv)::size_type>(argc) == new_argv.size());
|
|
|
|
|
2018-11-30 03:51:44 +00:00
|
|
|
std::string tmp_file_name = GetTempFileName();
|
2018-09-12 13:26:17 +00:00
|
|
|
std::cout << "Will be using this as the tmp file: " << tmp_file_name << '\n';
|
|
|
|
|
|
|
|
std::string tmp = "--benchmark_out=";
|
|
|
|
tmp += tmp_file_name;
|
|
|
|
new_argv.emplace_back(const_cast<char*>(tmp.c_str()));
|
|
|
|
|
|
|
|
argc = int(new_argv.size());
|
|
|
|
|
|
|
|
benchmark::Initialize(&argc, new_argv.data());
|
|
|
|
benchmark::RunSpecifiedBenchmarks();
|
|
|
|
|
|
|
|
// Read the output back from the file, and delete the file.
|
|
|
|
std::ifstream tmp_stream(tmp_file_name);
|
|
|
|
std::string output = std::string((std::istreambuf_iterator<char>(tmp_stream)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
std::remove(tmp_file_name.c_str());
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|