skip_with_error_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #undef NDEBUG
  2. #include "benchmark/benchmark.h"
  3. #include "../src/check.h" // NOTE: check.h is for internal use only!
  4. #include <cassert>
  5. #include <vector>
  6. namespace {
  7. class TestReporter : public benchmark::ConsoleReporter {
  8. public:
  9. virtual bool ReportContext(const Context& context) {
  10. return ConsoleReporter::ReportContext(context);
  11. };
  12. virtual void ReportRuns(const std::vector<Run>& report) {
  13. all_runs_.insert(all_runs_.end(), begin(report), end(report));
  14. ConsoleReporter::ReportRuns(report);
  15. }
  16. TestReporter() {}
  17. virtual ~TestReporter() {}
  18. mutable std::vector<Run> all_runs_;
  19. };
  20. struct TestCase {
  21. std::string name;
  22. bool error_occurred;
  23. std::string error_message;
  24. typedef benchmark::BenchmarkReporter::Run Run;
  25. void CheckRun(Run const& run) const {
  26. CHECK(name == run.benchmark_name) << "expected " << name << " got " << run.benchmark_name;
  27. CHECK(error_occurred == run.error_occurred);
  28. CHECK(error_message == run.error_message);
  29. if (error_occurred) {
  30. //CHECK(run.iterations == 0);
  31. } else {
  32. CHECK(run.iterations != 0);
  33. }
  34. }
  35. };
  36. std::vector<TestCase> ExpectedResults;
  37. int AddCases(const char* base_name, std::initializer_list<TestCase> const& v) {
  38. for (auto TC : v) {
  39. TC.name = base_name + TC.name;
  40. ExpectedResults.push_back(std::move(TC));
  41. }
  42. return 0;
  43. }
  44. #define CONCAT(x, y) CONCAT2(x, y)
  45. #define CONCAT2(x, y) x##y
  46. #define ADD_CASES(...) \
  47. int CONCAT(dummy, __LINE__) = AddCases(__VA_ARGS__)
  48. } // end namespace
  49. void BM_error_before_running(benchmark::State& state) {
  50. state.SkipWithError("error message");
  51. while (state.KeepRunning()) {
  52. assert(false);
  53. }
  54. }
  55. BENCHMARK(BM_error_before_running);
  56. ADD_CASES("BM_error_before_running",
  57. {{"", true, "error message"}});
  58. void BM_error_during_running(benchmark::State& state) {
  59. int first_iter = true;
  60. while (state.KeepRunning()) {
  61. if (state.range_x() == 1 && state.thread_index <= (state.threads / 2)) {
  62. assert(first_iter);
  63. first_iter = false;
  64. state.SkipWithError("error message");
  65. } else {
  66. state.PauseTiming();
  67. state.ResumeTiming();
  68. }
  69. }
  70. }
  71. BENCHMARK(BM_error_during_running)->Arg(1)->Arg(2)->ThreadRange(1, 8);
  72. ADD_CASES(
  73. "BM_error_during_running",
  74. {{"/1/threads:1", true, "error message"},
  75. {"/1/threads:2", true, "error message"},
  76. {"/1/threads:4", true, "error message"},
  77. {"/1/threads:8", true, "error message"},
  78. {"/2/threads:1", false, ""},
  79. {"/2/threads:2", false, ""},
  80. {"/2/threads:4", false, ""},
  81. {"/2/threads:8", false, ""}}
  82. );
  83. void BM_error_after_running(benchmark::State& state) {
  84. while (state.KeepRunning()) {
  85. benchmark::DoNotOptimize(state.iterations());
  86. }
  87. if (state.thread_index <= (state.threads / 2))
  88. state.SkipWithError("error message");
  89. }
  90. BENCHMARK(BM_error_after_running)->ThreadRange(1, 8);
  91. ADD_CASES(
  92. "BM_error_after_running",
  93. {{"/threads:1", true, "error message"},
  94. {"/threads:2", true, "error message"},
  95. {"/threads:4", true, "error message"},
  96. {"/threads:8", true, "error message"}}
  97. );
  98. void BM_error_while_paused(benchmark::State& state) {
  99. bool first_iter = true;
  100. while (state.KeepRunning()) {
  101. if (state.range_x() == 1 && state.thread_index <= (state.threads / 2)) {
  102. assert(first_iter);
  103. first_iter = false;
  104. state.PauseTiming();
  105. state.SkipWithError("error message");
  106. } else {
  107. state.PauseTiming();
  108. state.ResumeTiming();
  109. }
  110. }
  111. }
  112. BENCHMARK(BM_error_while_paused)->Arg(1)->Arg(2)->ThreadRange(1, 8);
  113. ADD_CASES(
  114. "BM_error_while_paused",
  115. {{"/1/threads:1", true, "error message"},
  116. {"/1/threads:2", true, "error message"},
  117. {"/1/threads:4", true, "error message"},
  118. {"/1/threads:8", true, "error message"},
  119. {"/2/threads:1", false, ""},
  120. {"/2/threads:2", false, ""},
  121. {"/2/threads:4", false, ""},
  122. {"/2/threads:8", false, ""}}
  123. );
  124. int main(int argc, char* argv[]) {
  125. benchmark::Initialize(&argc, argv);
  126. TestReporter test_reporter;
  127. benchmark::RunSpecifiedBenchmarks(&test_reporter);
  128. typedef benchmark::BenchmarkReporter::Run Run;
  129. auto EB = ExpectedResults.begin();
  130. for (Run const& run : test_reporter.all_runs_) {
  131. assert(EB != ExpectedResults.end());
  132. EB->CheckRun(run);
  133. ++EB;
  134. }
  135. assert(EB == ExpectedResults.end());
  136. return 0;
  137. }