output_test_helper.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <fstream>
  4. #include <iostream>
  5. #include <map>
  6. #include <memory>
  7. #include <random>
  8. #include <sstream>
  9. #include <streambuf>
  10. #include "../src/benchmark_api_internal.h"
  11. #include "../src/check.h" // NOTE: check.h is for internal use only!
  12. #include "../src/log.h" // NOTE: log.h is for internal use only
  13. #include "../src/re.h" // NOTE: re.h is for internal use only
  14. #include "output_test.h"
  15. // ========================================================================= //
  16. // ------------------------------ Internals -------------------------------- //
  17. // ========================================================================= //
  18. namespace internal {
  19. namespace {
  20. using TestCaseList = std::vector<TestCase>;
  21. // Use a vector because the order elements are added matters during iteration.
  22. // std::map/unordered_map don't guarantee that.
  23. // For example:
  24. // SetSubstitutions({{"%HelloWorld", "Hello"}, {"%Hello", "Hi"}});
  25. // Substitute("%HelloWorld") // Always expands to Hello.
  26. using SubMap = std::vector<std::pair<std::string, std::string>>;
  27. TestCaseList& GetTestCaseList(TestCaseID ID) {
  28. // Uses function-local statics to ensure initialization occurs
  29. // before first use.
  30. static TestCaseList lists[TC_NumID];
  31. return lists[ID];
  32. }
  33. SubMap& GetSubstitutions() {
  34. // Don't use 'dec_re' from header because it may not yet be initialized.
  35. // clang-format off
  36. static std::string safe_dec_re = "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?";
  37. static std::string time_re = "([0-9]+[.])?[0-9]+";
  38. static std::string percentage_re = "[0-9]+[.][0-9]{2}";
  39. static SubMap map = {
  40. {"%float", "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?"},
  41. // human-readable float
  42. {"%hrfloat", "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?[kMGTPEZYmunpfazy]?"},
  43. {"%percentage", percentage_re},
  44. {"%int", "[ ]*[0-9]+"},
  45. {" %s ", "[ ]+"},
  46. {"%time", "[ ]*" + time_re + "[ ]+ns"},
  47. {"%console_report", "[ ]*" + time_re + "[ ]+ns [ ]*" + time_re + "[ ]+ns [ ]*[0-9]+"},
  48. {"%console_percentage_report", "[ ]*" + percentage_re + "[ ]+% [ ]*" + percentage_re + "[ ]+% [ ]*[0-9]+"},
  49. {"%console_us_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us [ ]*[0-9]+"},
  50. {"%console_ms_report", "[ ]*" + time_re + "[ ]+ms [ ]*" + time_re + "[ ]+ms [ ]*[0-9]+"},
  51. {"%console_s_report", "[ ]*" + time_re + "[ ]+s [ ]*" + time_re + "[ ]+s [ ]*[0-9]+"},
  52. {"%console_time_only_report", "[ ]*" + time_re + "[ ]+ns [ ]*" + time_re + "[ ]+ns"},
  53. {"%console_us_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us [ ]*[0-9]+"},
  54. {"%console_us_time_only_report", "[ ]*" + time_re + "[ ]+us [ ]*" + time_re + "[ ]+us"},
  55. {"%csv_header",
  56. "name,iterations,real_time,cpu_time,time_unit,bytes_per_second,"
  57. "items_per_second,label,error_occurred,error_message"},
  58. {"%csv_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,,,,,"},
  59. {"%csv_us_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",us,,,,,"},
  60. {"%csv_ms_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ms,,,,,"},
  61. {"%csv_s_report", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",s,,,,,"},
  62. {"%csv_bytes_report",
  63. "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns," + safe_dec_re + ",,,,"},
  64. {"%csv_items_report",
  65. "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,," + safe_dec_re + ",,,"},
  66. {"%csv_bytes_items_report",
  67. "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns," + safe_dec_re +
  68. "," + safe_dec_re + ",,,"},
  69. {"%csv_label_report_begin", "[0-9]+," + safe_dec_re + "," + safe_dec_re + ",ns,,,"},
  70. {"%csv_label_report_end", ",,"}};
  71. // clang-format on
  72. return map;
  73. }
  74. std::string PerformSubstitutions(std::string source) {
  75. SubMap const& subs = GetSubstitutions();
  76. using SizeT = std::string::size_type;
  77. for (auto const& KV : subs) {
  78. SizeT pos;
  79. SizeT next_start = 0;
  80. while ((pos = source.find(KV.first, next_start)) != std::string::npos) {
  81. next_start = pos + KV.second.size();
  82. source.replace(pos, KV.first.size(), KV.second);
  83. }
  84. }
  85. return source;
  86. }
  87. void CheckCase(std::stringstream& remaining_output, TestCase const& TC,
  88. TestCaseList const& not_checks) {
  89. std::string first_line;
  90. bool on_first = true;
  91. std::string line;
  92. while (remaining_output.eof() == false) {
  93. BM_CHECK(remaining_output.good());
  94. std::getline(remaining_output, line);
  95. if (on_first) {
  96. first_line = line;
  97. on_first = false;
  98. }
  99. for (const auto& NC : not_checks) {
  100. BM_CHECK(!NC.regex->Match(line))
  101. << "Unexpected match for line \"" << line << "\" for MR_Not regex \""
  102. << NC.regex_str << "\""
  103. << "\n actual regex string \"" << TC.substituted_regex << "\""
  104. << "\n started matching near: " << first_line;
  105. }
  106. if (TC.regex->Match(line)) return;
  107. BM_CHECK(TC.match_rule != MR_Next)
  108. << "Expected line \"" << line << "\" to match regex \"" << TC.regex_str
  109. << "\""
  110. << "\n actual regex string \"" << TC.substituted_regex << "\""
  111. << "\n started matching near: " << first_line;
  112. }
  113. BM_CHECK(remaining_output.eof() == false)
  114. << "End of output reached before match for regex \"" << TC.regex_str
  115. << "\" was found"
  116. << "\n actual regex string \"" << TC.substituted_regex << "\""
  117. << "\n started matching near: " << first_line;
  118. }
  119. void CheckCases(TestCaseList const& checks, std::stringstream& output) {
  120. std::vector<TestCase> not_checks;
  121. for (size_t i = 0; i < checks.size(); ++i) {
  122. const auto& TC = checks[i];
  123. if (TC.match_rule == MR_Not) {
  124. not_checks.push_back(TC);
  125. continue;
  126. }
  127. CheckCase(output, TC, not_checks);
  128. not_checks.clear();
  129. }
  130. }
  131. class TestReporter : public benchmark::BenchmarkReporter {
  132. public:
  133. TestReporter(std::vector<benchmark::BenchmarkReporter*> reps)
  134. : reporters_(reps) {}
  135. virtual bool ReportContext(const Context& context) BENCHMARK_OVERRIDE {
  136. bool last_ret = false;
  137. bool first = true;
  138. for (auto rep : reporters_) {
  139. bool new_ret = rep->ReportContext(context);
  140. BM_CHECK(first || new_ret == last_ret)
  141. << "Reports return different values for ReportContext";
  142. first = false;
  143. last_ret = new_ret;
  144. }
  145. (void)first;
  146. return last_ret;
  147. }
  148. void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
  149. for (auto rep : reporters_) rep->ReportRuns(report);
  150. }
  151. void Finalize() BENCHMARK_OVERRIDE {
  152. for (auto rep : reporters_) rep->Finalize();
  153. }
  154. private:
  155. std::vector<benchmark::BenchmarkReporter*> reporters_;
  156. };
  157. } // namespace
  158. } // end namespace internal
  159. // ========================================================================= //
  160. // -------------------------- Results checking ----------------------------- //
  161. // ========================================================================= //
  162. namespace internal {
  163. // Utility class to manage subscribers for checking benchmark results.
  164. // It works by parsing the CSV output to read the results.
  165. class ResultsChecker {
  166. public:
  167. struct PatternAndFn : public TestCase { // reusing TestCase for its regexes
  168. PatternAndFn(const std::string& rx, ResultsCheckFn fn_)
  169. : TestCase(rx), fn(fn_) {}
  170. ResultsCheckFn fn;
  171. };
  172. std::vector<PatternAndFn> check_patterns;
  173. std::vector<Results> results;
  174. std::vector<std::string> field_names;
  175. void Add(const std::string& entry_pattern, ResultsCheckFn fn);
  176. void CheckResults(std::stringstream& output);
  177. private:
  178. void SetHeader_(const std::string& csv_header);
  179. void SetValues_(const std::string& entry_csv_line);
  180. std::vector<std::string> SplitCsv_(const std::string& line);
  181. };
  182. // store the static ResultsChecker in a function to prevent initialization
  183. // order problems
  184. ResultsChecker& GetResultsChecker() {
  185. static ResultsChecker rc;
  186. return rc;
  187. }
  188. // add a results checker for a benchmark
  189. void ResultsChecker::Add(const std::string& entry_pattern, ResultsCheckFn fn) {
  190. check_patterns.emplace_back(entry_pattern, fn);
  191. }
  192. // check the results of all subscribed benchmarks
  193. void ResultsChecker::CheckResults(std::stringstream& output) {
  194. // first reset the stream to the start
  195. {
  196. auto start = std::stringstream::pos_type(0);
  197. // clear before calling tellg()
  198. output.clear();
  199. // seek to zero only when needed
  200. if (output.tellg() > start) output.seekg(start);
  201. // and just in case
  202. output.clear();
  203. }
  204. // now go over every line and publish it to the ResultsChecker
  205. std::string line;
  206. bool on_first = true;
  207. while (output.eof() == false) {
  208. BM_CHECK(output.good());
  209. std::getline(output, line);
  210. if (on_first) {
  211. SetHeader_(line); // this is important
  212. on_first = false;
  213. continue;
  214. }
  215. SetValues_(line);
  216. }
  217. // finally we can call the subscribed check functions
  218. for (const auto& p : check_patterns) {
  219. BM_VLOG(2) << "--------------------------------\n";
  220. BM_VLOG(2) << "checking for benchmarks matching " << p.regex_str << "...\n";
  221. for (const auto& r : results) {
  222. if (!p.regex->Match(r.name)) {
  223. BM_VLOG(2) << p.regex_str << " is not matched by " << r.name << "\n";
  224. continue;
  225. } else {
  226. BM_VLOG(2) << p.regex_str << " is matched by " << r.name << "\n";
  227. }
  228. BM_VLOG(1) << "Checking results of " << r.name << ": ... \n";
  229. p.fn(r);
  230. BM_VLOG(1) << "Checking results of " << r.name << ": OK.\n";
  231. }
  232. }
  233. }
  234. // prepare for the names in this header
  235. void ResultsChecker::SetHeader_(const std::string& csv_header) {
  236. field_names = SplitCsv_(csv_header);
  237. }
  238. // set the values for a benchmark
  239. void ResultsChecker::SetValues_(const std::string& entry_csv_line) {
  240. if (entry_csv_line.empty()) return; // some lines are empty
  241. BM_CHECK(!field_names.empty());
  242. auto vals = SplitCsv_(entry_csv_line);
  243. BM_CHECK_EQ(vals.size(), field_names.size());
  244. results.emplace_back(vals[0]); // vals[0] is the benchmark name
  245. auto& entry = results.back();
  246. for (size_t i = 1, e = vals.size(); i < e; ++i) {
  247. entry.values[field_names[i]] = vals[i];
  248. }
  249. }
  250. // a quick'n'dirty csv splitter (eliminating quotes)
  251. std::vector<std::string> ResultsChecker::SplitCsv_(const std::string& line) {
  252. std::vector<std::string> out;
  253. if (line.empty()) return out;
  254. if (!field_names.empty()) out.reserve(field_names.size());
  255. size_t prev = 0, pos = line.find_first_of(','), curr = pos;
  256. while (pos != line.npos) {
  257. BM_CHECK(curr > 0);
  258. if (line[prev] == '"') ++prev;
  259. if (line[curr - 1] == '"') --curr;
  260. out.push_back(line.substr(prev, curr - prev));
  261. prev = pos + 1;
  262. pos = line.find_first_of(',', pos + 1);
  263. curr = pos;
  264. }
  265. curr = line.size();
  266. if (line[prev] == '"') ++prev;
  267. if (line[curr - 1] == '"') --curr;
  268. out.push_back(line.substr(prev, curr - prev));
  269. return out;
  270. }
  271. } // end namespace internal
  272. size_t AddChecker(const char* bm_name, ResultsCheckFn fn) {
  273. auto& rc = internal::GetResultsChecker();
  274. rc.Add(bm_name, fn);
  275. return rc.results.size();
  276. }
  277. int Results::NumThreads() const {
  278. auto pos = name.find("/threads:");
  279. if (pos == name.npos) return 1;
  280. auto end = name.find('/', pos + 9);
  281. std::stringstream ss;
  282. ss << name.substr(pos + 9, end);
  283. int num = 1;
  284. ss >> num;
  285. BM_CHECK(!ss.fail());
  286. return num;
  287. }
  288. double Results::NumIterations() const {
  289. return GetAs<double>("iterations");
  290. }
  291. double Results::GetTime(BenchmarkTime which) const {
  292. BM_CHECK(which == kCpuTime || which == kRealTime);
  293. const char* which_str = which == kCpuTime ? "cpu_time" : "real_time";
  294. double val = GetAs<double>(which_str);
  295. auto unit = Get("time_unit");
  296. BM_CHECK(unit);
  297. if (*unit == "ns") {
  298. return val * 1.e-9;
  299. } else if (*unit == "us") {
  300. return val * 1.e-6;
  301. } else if (*unit == "ms") {
  302. return val * 1.e-3;
  303. } else if (*unit == "s") {
  304. return val;
  305. } else {
  306. BM_CHECK(1 == 0) << "unknown time unit: " << *unit;
  307. return 0;
  308. }
  309. }
  310. // ========================================================================= //
  311. // -------------------------- Public API Definitions------------------------ //
  312. // ========================================================================= //
  313. TestCase::TestCase(std::string re, int rule)
  314. : regex_str(std::move(re)),
  315. match_rule(rule),
  316. substituted_regex(internal::PerformSubstitutions(regex_str)),
  317. regex(std::make_shared<benchmark::Regex>()) {
  318. std::string err_str;
  319. regex->Init(substituted_regex, &err_str);
  320. BM_CHECK(err_str.empty())
  321. << "Could not construct regex \"" << substituted_regex << "\""
  322. << "\n originally \"" << regex_str << "\""
  323. << "\n got error: " << err_str;
  324. }
  325. int AddCases(TestCaseID ID, std::initializer_list<TestCase> il) {
  326. auto& L = internal::GetTestCaseList(ID);
  327. L.insert(L.end(), il);
  328. return 0;
  329. }
  330. int SetSubstitutions(
  331. std::initializer_list<std::pair<std::string, std::string>> il) {
  332. auto& subs = internal::GetSubstitutions();
  333. for (auto KV : il) {
  334. bool exists = false;
  335. KV.second = internal::PerformSubstitutions(KV.second);
  336. for (auto& EKV : subs) {
  337. if (EKV.first == KV.first) {
  338. EKV.second = std::move(KV.second);
  339. exists = true;
  340. break;
  341. }
  342. }
  343. if (!exists) subs.push_back(std::move(KV));
  344. }
  345. return 0;
  346. }
  347. // Disable deprecated warnings temporarily because we need to reference
  348. // CSVReporter but don't want to trigger -Werror=-Wdeprecated-declarations
  349. #ifdef __GNUC__
  350. #pragma GCC diagnostic push
  351. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  352. #endif
  353. void RunOutputTests(int argc, char* argv[]) {
  354. using internal::GetTestCaseList;
  355. benchmark::Initialize(&argc, argv);
  356. auto options = benchmark::internal::GetOutputOptions(/*force_no_color*/ true);
  357. benchmark::ConsoleReporter CR(options);
  358. benchmark::JSONReporter JR;
  359. benchmark::CSVReporter CSVR;
  360. struct ReporterTest {
  361. const char* name;
  362. std::vector<TestCase>& output_cases;
  363. std::vector<TestCase>& error_cases;
  364. benchmark::BenchmarkReporter& reporter;
  365. std::stringstream out_stream;
  366. std::stringstream err_stream;
  367. ReporterTest(const char* n, std::vector<TestCase>& out_tc,
  368. std::vector<TestCase>& err_tc,
  369. benchmark::BenchmarkReporter& br)
  370. : name(n), output_cases(out_tc), error_cases(err_tc), reporter(br) {
  371. reporter.SetOutputStream(&out_stream);
  372. reporter.SetErrorStream(&err_stream);
  373. }
  374. } TestCases[] = {
  375. {"ConsoleReporter", GetTestCaseList(TC_ConsoleOut),
  376. GetTestCaseList(TC_ConsoleErr), CR},
  377. {"JSONReporter", GetTestCaseList(TC_JSONOut), GetTestCaseList(TC_JSONErr),
  378. JR},
  379. {"CSVReporter", GetTestCaseList(TC_CSVOut), GetTestCaseList(TC_CSVErr),
  380. CSVR},
  381. };
  382. // Create the test reporter and run the benchmarks.
  383. std::cout << "Running benchmarks...\n";
  384. internal::TestReporter test_rep({&CR, &JR, &CSVR});
  385. benchmark::RunSpecifiedBenchmarks(&test_rep);
  386. for (auto& rep_test : TestCases) {
  387. std::string msg = std::string("\nTesting ") + rep_test.name + " Output\n";
  388. std::string banner(msg.size() - 1, '-');
  389. std::cout << banner << msg << banner << "\n";
  390. std::cerr << rep_test.err_stream.str();
  391. std::cout << rep_test.out_stream.str();
  392. internal::CheckCases(rep_test.error_cases, rep_test.err_stream);
  393. internal::CheckCases(rep_test.output_cases, rep_test.out_stream);
  394. std::cout << "\n";
  395. }
  396. // now that we know the output is as expected, we can dispatch
  397. // the checks to subscribees.
  398. auto& csv = TestCases[2];
  399. // would use == but gcc spits a warning
  400. BM_CHECK(std::strcmp(csv.name, "CSVReporter") == 0);
  401. internal::GetResultsChecker().CheckResults(csv.out_stream);
  402. }
  403. #ifdef __GNUC__
  404. #pragma GCC diagnostic pop
  405. #endif
  406. int SubstrCnt(const std::string& haystack, const std::string& pat) {
  407. if (pat.length() == 0) return 0;
  408. int count = 0;
  409. for (size_t offset = haystack.find(pat); offset != std::string::npos;
  410. offset = haystack.find(pat, offset + pat.length()))
  411. ++count;
  412. return count;
  413. }
  414. static char ToHex(int ch) {
  415. return ch < 10 ? static_cast<char>('0' + ch)
  416. : static_cast<char>('a' + (ch - 10));
  417. }
  418. static char RandomHexChar() {
  419. static std::mt19937 rd{std::random_device{}()};
  420. static std::uniform_int_distribution<int> mrand{0, 15};
  421. return ToHex(mrand(rd));
  422. }
  423. static std::string GetRandomFileName() {
  424. std::string model = "test.%%%%%%";
  425. for (auto & ch : model) {
  426. if (ch == '%')
  427. ch = RandomHexChar();
  428. }
  429. return model;
  430. }
  431. static bool FileExists(std::string const& name) {
  432. std::ifstream in(name.c_str());
  433. return in.good();
  434. }
  435. static std::string GetTempFileName() {
  436. // This function attempts to avoid race conditions where two tests
  437. // create the same file at the same time. However, it still introduces races
  438. // similar to tmpnam.
  439. int retries = 3;
  440. while (--retries) {
  441. std::string name = GetRandomFileName();
  442. if (!FileExists(name))
  443. return name;
  444. }
  445. std::cerr << "Failed to create unique temporary file name" << std::endl;
  446. std::abort();
  447. }
  448. std::string GetFileReporterOutput(int argc, char* argv[]) {
  449. std::vector<char*> new_argv(argv, argv + argc);
  450. assert(static_cast<decltype(new_argv)::size_type>(argc) == new_argv.size());
  451. std::string tmp_file_name = GetTempFileName();
  452. std::cout << "Will be using this as the tmp file: " << tmp_file_name << '\n';
  453. std::string tmp = "--benchmark_out=";
  454. tmp += tmp_file_name;
  455. new_argv.emplace_back(const_cast<char*>(tmp.c_str()));
  456. argc = int(new_argv.size());
  457. benchmark::Initialize(&argc, new_argv.data());
  458. benchmark::RunSpecifiedBenchmarks();
  459. // Read the output back from the file, and delete the file.
  460. std::ifstream tmp_stream(tmp_file_name);
  461. std::string output = std::string((std::istreambuf_iterator<char>(tmp_stream)),
  462. std::istreambuf_iterator<char>());
  463. std::remove(tmp_file_name.c_str());
  464. return output;
  465. }