Rename ResultsCheckerEntry to Results.

This commit is contained in:
Joao Paulo Magalhaes 2017-04-29 20:01:18 +01:00
parent f3b82a8eda
commit 180719d0d6
3 changed files with 38 additions and 31 deletions

View File

@ -62,22 +62,24 @@ void RunOutputTests(int argc, char* argv[]);
// ------------------------- Results checking ------------------------------ //
// ========================================================================= //
struct ResultsCheckerEntry;
typedef std::function< void(ResultsCheckerEntry const&) > ResultsCheckFn;
struct Results;
typedef std::function< void(Results const&) > ResultsCheckFn;
// Add a function to check the (CSV) results of a benchmark. These
// functions will be called only after the output was successfully
// checked.
size_t AddChecker(const char* bm_name, ResultsCheckFn fn);
// bm_name_pattern: a name or a regex which will be matched agains
// all the benchmark names. Matching benchmarks
// will be the subject of a call to fn
size_t AddChecker(const char* bm_name_pattern, ResultsCheckFn fn);
// Class to test the results of a benchmark.
// It inspects the results by looking at the CSV output of a subscribed
// benchmark.
struct ResultsCheckerEntry {
std::string name;
// Class to hold the (CSV!) results of a benchmark.
// It is passed in calls to checker functions.
struct Results {
std::string name; // the benchmark name
std::map< std::string, std::string > values;
ResultsCheckerEntry(const std::string& n) : name(n) {}
Results(const std::string& n) : name(n) {}
int NumThreads() const;
@ -99,21 +101,14 @@ struct ResultsCheckerEntry {
}
// get a result by name, parsed as a specific type.
// For counters, use GetCounterAs instead.
template< class T > T GetAs(const char* entry_name) const {
auto *sv = Get(entry_name);
CHECK(sv != nullptr && !sv->empty());
std::stringstream ss;
ss << *sv;
T out;
ss >> out;
CHECK(!ss.fail());
return out;
}
// NOTE: for counters, use GetCounterAs instead.
template <class T>
T GetAs(const char* entry_name) const;
// counters are written as doubles, so they have to be read first
// as a double, and only then converted to the asked type.
template< class T > T GetCounterAs(const char* entry_name) const {
template <class T>
T GetCounterAs(const char* entry_name) const {
double dval = GetAs< double >(entry_name);
T tval = static_cast< T >(dval);
return tval;
@ -123,6 +118,18 @@ struct ResultsCheckerEntry {
double GetTime(const char* which) const;
};
template <class T>
T Results::GetAs(const char* entry_name) const {
auto *sv = Get(entry_name);
CHECK(sv != nullptr && !sv->empty());
std::stringstream ss;
ss << *sv;
T out;
ss >> out;
CHECK(!ss.fail());
return out;
}
//----------------------------------
// Macros to help in result checking. Do not use them with arguments causing
// side-effects.

View File

@ -169,7 +169,7 @@ class ResultsChecker {
};
std::vector< PatternAndFn > check_patterns;
std::vector< ResultsCheckerEntry > results;
std::vector< Results > results;
std::vector< std::string > field_names;
void Add(const std::string& entry_pattern, ResultsCheckFn fn);
@ -178,7 +178,7 @@ class ResultsChecker {
private:
ResultsCheckerEntry* Find_(const std::string& entry_name);
Results* Find_(const std::string& entry_name);
void SetHeader_(const std::string& csv_header);
void SetValues_(const std::string& entry_csv_line);
@ -293,7 +293,7 @@ size_t AddChecker(const char* bm_name, ResultsCheckFn fn)
return rc.results.size();
}
int ResultsCheckerEntry::NumThreads() const {
int Results::NumThreads() const {
auto pos = name.find("/threads:");
if(pos == name.npos) return 1;
auto end = name.find('/', pos + 9);
@ -305,7 +305,7 @@ int ResultsCheckerEntry::NumThreads() const {
return num;
}
double ResultsCheckerEntry::GetTime(const char* which) const {
double Results::GetTime(const char* which) const {
double val = GetAs< double >(which);
auto unit = Get("time_unit");
CHECK(unit);

View File

@ -35,7 +35,7 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_Simple\",$"},
{"\"foo\": %float$", MR_Next},
{"}", MR_Next}});
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Simple\",%csv_report,%float,%float$"}});
CHECK_BENCHMARK_RESULTS("BM_Counters_Simple", [](ResultsCheckerEntry const& e) {
CHECK_BENCHMARK_RESULTS("BM_Counters_Simple", [](Results const& e) {
double its = e.GetAs< double >("iterations");
CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
// check that the value of bar is within 0.1% of the expected value
@ -72,7 +72,7 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_WithBytesAndItemsPSec\",$"},
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_WithBytesAndItemsPSec\","
"%csv_bytes_items_report,%float,%float$"}});
CHECK_BENCHMARK_RESULTS("BM_Counters_WithBytesAndItemsPSec",
[](ResultsCheckerEntry const& e) {
[](Results const& e) {
double t = e.DurationCPUTime(); // this (and not real time) is the time used
CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
CHECK_COUNTER_VALUE(e, int, "bar", EQ, num_calls1);
@ -104,7 +104,7 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_Rate\",$"},
{"}", MR_Next}});
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Rate\",%csv_report,%float,%float$"}});
CHECK_BENCHMARK_RESULTS("BM_Counters_Rate",
[](ResultsCheckerEntry const& e) {
[](Results const& e) {
double t = e.DurationCPUTime(); // this (and not real time) is the time used
// check that the values are within 0.1% of the expected values
CHECK_COUNTER_VALUE_EPS(e, "foo", EQ_EPS, 1./t, 0.001);
@ -133,7 +133,7 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_Threads/threads:%int\",$"},
{"}", MR_Next}});
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Threads/threads:%int\",%csv_report,%float,%float$"}});
CHECK_BENCHMARK_RESULTS("BM_Counters_Threads/threads:%int",
[](ResultsCheckerEntry const& e) {
[](Results const& e) {
CHECK_COUNTER_VALUE(e, int, "foo", EQ, e.NumThreads());
CHECK_COUNTER_VALUE(e, int, "bar", EQ, 2 * e.NumThreads());
});
@ -161,7 +161,7 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_AvgThreads/threads:%int\",$"},
{"}", MR_Next}});
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_AvgThreads/threads:%int\",%csv_report,%float,%float$"}});
CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreads/threads:%int",
[](ResultsCheckerEntry const& e) {
[](Results const& e) {
CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
CHECK_COUNTER_VALUE(e, int, "bar", EQ, 2);
});
@ -189,7 +189,7 @@ ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_AvgThreadsRate/threads:%int\",$
{"}", MR_Next}});
ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_AvgThreadsRate/threads:%int\",%csv_report,%float,%float$"}});
CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreadsRate/threads:%int",
[](ResultsCheckerEntry const& e) {
[](Results const& e) {
CHECK_COUNTER_VALUE_EPS(e, "foo", EQ_EPS, 1./e.DurationCPUTime(), 0.001);
CHECK_COUNTER_VALUE_EPS(e, "bar", EQ_EPS, 2./e.DurationCPUTime(), 0.001);
});