You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

519 lines
18 KiB

2 months ago
  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_(std::move(reps)) {}
  135. bool ReportContext(const Context& context) 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) override {
  149. for (auto rep : reporters_) rep->ReportRuns(report);
  150. }
  151. void Finalize() 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(std::move(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, const 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,
  190. const ResultsCheckFn& fn) {
  191. check_patterns.emplace_back(entry_pattern, fn);
  192. }
  193. // check the results of all subscribed benchmarks
  194. void ResultsChecker::CheckResults(std::stringstream& output) {
  195. // first reset the stream to the start
  196. {
  197. auto start = std::stringstream::pos_type(0);
  198. // clear before calling tellg()
  199. output.clear();
  200. // seek to zero only when needed
  201. if (output.tellg() > start) output.seekg(start);
  202. // and just in case
  203. output.clear();
  204. }
  205. // now go over every line and publish it to the ResultsChecker
  206. std::string line;
  207. bool on_first = true;
  208. while (output.eof() == false) {
  209. BM_CHECK(output.good());
  210. std::getline(output, line);
  211. if (on_first) {
  212. SetHeader_(line); // this is important
  213. on_first = false;
  214. continue;
  215. }
  216. SetValues_(line);
  217. }
  218. // finally we can call the subscribed check functions
  219. for (const auto& p : check_patterns) {
  220. BM_VLOG(2) << "--------------------------------\n";
  221. BM_VLOG(2) << "checking for benchmarks matching " << p.regex_str << "...\n";
  222. for (const auto& r : results) {
  223. if (!p.regex->Match(r.name)) {
  224. BM_VLOG(2) << p.regex_str << " is not matched by " << r.name << "\n";
  225. continue;
  226. }
  227. BM_VLOG(2) << p.regex_str << " is matched by " << r.name << "\n";
  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 std::string& bm_name, const 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 { return GetAs<double>("iterations"); }
  289. double Results::GetTime(BenchmarkTime which) const {
  290. BM_CHECK(which == kCpuTime || which == kRealTime);
  291. const char* which_str = which == kCpuTime ? "cpu_time" : "real_time";
  292. double val = GetAs<double>(which_str);
  293. auto unit = Get("time_unit");
  294. BM_CHECK(unit);
  295. if (*unit == "ns") {
  296. return val * 1.e-9;
  297. }
  298. if (*unit == "us") {
  299. return val * 1.e-6;
  300. }
  301. if (*unit == "ms") {
  302. return val * 1.e-3;
  303. }
  304. if (*unit == "s") {
  305. return val;
  306. }
  307. BM_CHECK(1 == 0) << "unknown time unit: " << *unit;
  308. return 0;
  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. BENCHMARK_DISABLE_DEPRECATED_WARNING
  350. void RunOutputTests(int argc, char* argv[]) {
  351. using internal::GetTestCaseList;
  352. benchmark::Initialize(&argc, argv);
  353. auto options = benchmark::internal::GetOutputOptions(/*force_no_color*/ true);
  354. benchmark::ConsoleReporter CR(options);
  355. benchmark::JSONReporter JR;
  356. benchmark::CSVReporter CSVR;
  357. struct ReporterTest {
  358. std::string name;
  359. std::vector<TestCase>& output_cases;
  360. std::vector<TestCase>& error_cases;
  361. benchmark::BenchmarkReporter& reporter;
  362. std::stringstream out_stream;
  363. std::stringstream err_stream;
  364. ReporterTest(const std::string& n, std::vector<TestCase>& out_tc,
  365. std::vector<TestCase>& err_tc,
  366. benchmark::BenchmarkReporter& br)
  367. : name(n), output_cases(out_tc), error_cases(err_tc), reporter(br) {
  368. reporter.SetOutputStream(&out_stream);
  369. reporter.SetErrorStream(&err_stream);
  370. }
  371. } TestCases[] = {
  372. {std::string("ConsoleReporter"), GetTestCaseList(TC_ConsoleOut),
  373. GetTestCaseList(TC_ConsoleErr), CR},
  374. {std::string("JSONReporter"), GetTestCaseList(TC_JSONOut),
  375. GetTestCaseList(TC_JSONErr), JR},
  376. {std::string("CSVReporter"), GetTestCaseList(TC_CSVOut),
  377. GetTestCaseList(TC_CSVErr), CSVR},
  378. };
  379. // Create the test reporter and run the benchmarks.
  380. std::cout << "Running benchmarks...\n";
  381. internal::TestReporter test_rep({&CR, &JR, &CSVR});
  382. benchmark::RunSpecifiedBenchmarks(&test_rep);
  383. for (auto& rep_test : TestCases) {
  384. std::string msg =
  385. std::string("\nTesting ") + rep_test.name + std::string(" Output\n");
  386. std::string banner(msg.size() - 1, '-');
  387. std::cout << banner << msg << banner << "\n";
  388. std::cerr << rep_test.err_stream.str();
  389. std::cout << rep_test.out_stream.str();
  390. internal::CheckCases(rep_test.error_cases, rep_test.err_stream);
  391. internal::CheckCases(rep_test.output_cases, rep_test.out_stream);
  392. std::cout << "\n";
  393. }
  394. // now that we know the output is as expected, we can dispatch
  395. // the checks to subscribees.
  396. auto& csv = TestCases[2];
  397. // would use == but gcc spits a warning
  398. BM_CHECK(csv.name == std::string("CSVReporter"));
  399. internal::GetResultsChecker().CheckResults(csv.out_stream);
  400. }
  401. BENCHMARK_RESTORE_DEPRECATED_WARNING
  402. int SubstrCnt(const std::string& haystack, const std::string& pat) {
  403. if (pat.length() == 0) return 0;
  404. int count = 0;
  405. for (size_t offset = haystack.find(pat); offset != std::string::npos;
  406. offset = haystack.find(pat, offset + pat.length()))
  407. ++count;
  408. return count;
  409. }
  410. static char ToHex(int ch) {
  411. return ch < 10 ? static_cast<char>('0' + ch)
  412. : static_cast<char>('a' + (ch - 10));
  413. }
  414. static char RandomHexChar() {
  415. static std::mt19937 rd{std::random_device{}()};
  416. static std::uniform_int_distribution<int> mrand{0, 15};
  417. return ToHex(mrand(rd));
  418. }
  419. static std::string GetRandomFileName() {
  420. std::string model = "test.%%%%%%";
  421. for (auto& ch : model) {
  422. if (ch == '%') ch = RandomHexChar();
  423. }
  424. return model;
  425. }
  426. static bool FileExists(std::string const& name) {
  427. std::ifstream in(name.c_str());
  428. return in.good();
  429. }
  430. static std::string GetTempFileName() {
  431. // This function attempts to avoid race conditions where two tests
  432. // create the same file at the same time. However, it still introduces races
  433. // similar to tmpnam.
  434. int retries = 3;
  435. while (--retries) {
  436. std::string name = GetRandomFileName();
  437. if (!FileExists(name)) return name;
  438. }
  439. std::cerr << "Failed to create unique temporary file name" << std::endl;
  440. std::abort();
  441. }
  442. std::string GetFileReporterOutput(int argc, char* argv[]) {
  443. std::vector<char*> new_argv(argv, argv + argc);
  444. assert(static_cast<decltype(new_argv)::size_type>(argc) == new_argv.size());
  445. std::string tmp_file_name = GetTempFileName();
  446. std::cout << "Will be using this as the tmp file: " << tmp_file_name << '\n';
  447. std::string tmp = "--benchmark_out=";
  448. tmp += tmp_file_name;
  449. new_argv.emplace_back(const_cast<char*>(tmp.c_str()));
  450. argc = int(new_argv.size());
  451. benchmark::Initialize(&argc, new_argv.data());
  452. benchmark::RunSpecifiedBenchmarks();
  453. // Read the output back from the file, and delete the file.
  454. std::ifstream tmp_stream(tmp_file_name);
  455. std::string output = std::string((std::istreambuf_iterator<char>(tmp_stream)),
  456. std::istreambuf_iterator<char>());
  457. std::remove(tmp_file_name.c_str());
  458. return output;
  459. }