skip_with_error_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #undef NDEBUG
  2. #include <cassert>
  3. #include <vector>
  4. #include "../src/check.h" // NOTE: check.h is for internal use only!
  5. #include "benchmark/benchmark.h"
  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())
  27. << "expected " << name << " got " << run.benchmark_name();
  28. CHECK(error_occurred == run.error_occurred);
  29. CHECK(error_message == run.error_message);
  30. if (error_occurred) {
  31. // CHECK(run.iterations == 0);
  32. } else {
  33. CHECK(run.iterations != 0);
  34. }
  35. }
  36. };
  37. std::vector<TestCase> ExpectedResults;
  38. int AddCases(const char* base_name, std::initializer_list<TestCase> const& v) {
  39. for (auto TC : v) {
  40. TC.name = base_name + TC.name;
  41. ExpectedResults.push_back(std::move(TC));
  42. }
  43. return 0;
  44. }
  45. #define CONCAT(x, y) CONCAT2(x, y)
  46. #define CONCAT2(x, y) x##y
  47. #define ADD_CASES(...) 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", {{"", true, "error message"}});
  57. void BM_error_before_running_batch(benchmark::State& state) {
  58. state.SkipWithError("error message");
  59. while (state.KeepRunningBatch(17)) {
  60. assert(false);
  61. }
  62. }
  63. BENCHMARK(BM_error_before_running_batch);
  64. ADD_CASES("BM_error_before_running_batch", {{"", true, "error message"}});
  65. void BM_error_before_running_range_for(benchmark::State& state) {
  66. state.SkipWithError("error message");
  67. for (auto _ : state) {
  68. assert(false);
  69. }
  70. }
  71. BENCHMARK(BM_error_before_running_range_for);
  72. ADD_CASES("BM_error_before_running_range_for", {{"", true, "error message"}});
  73. void BM_error_during_running(benchmark::State& state) {
  74. int first_iter = true;
  75. while (state.KeepRunning()) {
  76. if (state.range(0) == 1 && state.thread_index <= (state.threads / 2)) {
  77. assert(first_iter);
  78. first_iter = false;
  79. state.SkipWithError("error message");
  80. } else {
  81. state.PauseTiming();
  82. state.ResumeTiming();
  83. }
  84. }
  85. }
  86. BENCHMARK(BM_error_during_running)->Arg(1)->Arg(2)->ThreadRange(1, 8);
  87. ADD_CASES("BM_error_during_running", {{"/1/threads:1", true, "error message"},
  88. {"/1/threads:2", true, "error message"},
  89. {"/1/threads:4", true, "error message"},
  90. {"/1/threads:8", true, "error message"},
  91. {"/2/threads:1", false, ""},
  92. {"/2/threads:2", false, ""},
  93. {"/2/threads:4", false, ""},
  94. {"/2/threads:8", false, ""}});
  95. void BM_error_during_running_ranged_for(benchmark::State& state) {
  96. assert(state.max_iterations > 3 && "test requires at least a few iterations");
  97. int first_iter = true;
  98. // NOTE: Users should not write the for loop explicitly.
  99. for (auto It = state.begin(), End = state.end(); It != End; ++It) {
  100. if (state.range(0) == 1) {
  101. assert(first_iter);
  102. first_iter = false;
  103. state.SkipWithError("error message");
  104. // Test the unfortunate but documented behavior that the ranged-for loop
  105. // doesn't automatically terminate when SkipWithError is set.
  106. assert(++It != End);
  107. break; // Required behavior
  108. }
  109. }
  110. }
  111. BENCHMARK(BM_error_during_running_ranged_for)->Arg(1)->Arg(2)->Iterations(5);
  112. ADD_CASES("BM_error_during_running_ranged_for",
  113. {{"/1/iterations:5", true, "error message"},
  114. {"/2/iterations:5", false, ""}});
  115. void BM_error_after_running(benchmark::State& state) {
  116. for (auto _ : state) {
  117. benchmark::DoNotOptimize(state.iterations());
  118. }
  119. if (state.thread_index <= (state.threads / 2))
  120. state.SkipWithError("error message");
  121. }
  122. BENCHMARK(BM_error_after_running)->ThreadRange(1, 8);
  123. ADD_CASES("BM_error_after_running", {{"/threads:1", true, "error message"},
  124. {"/threads:2", true, "error message"},
  125. {"/threads:4", true, "error message"},
  126. {"/threads:8", true, "error message"}});
  127. void BM_error_while_paused(benchmark::State& state) {
  128. bool first_iter = true;
  129. while (state.KeepRunning()) {
  130. if (state.range(0) == 1 && state.thread_index <= (state.threads / 2)) {
  131. assert(first_iter);
  132. first_iter = false;
  133. state.PauseTiming();
  134. state.SkipWithError("error message");
  135. } else {
  136. state.PauseTiming();
  137. state.ResumeTiming();
  138. }
  139. }
  140. }
  141. BENCHMARK(BM_error_while_paused)->Arg(1)->Arg(2)->ThreadRange(1, 8);
  142. ADD_CASES("BM_error_while_paused", {{"/1/threads:1", true, "error message"},
  143. {"/1/threads:2", true, "error message"},
  144. {"/1/threads:4", true, "error message"},
  145. {"/1/threads:8", true, "error message"},
  146. {"/2/threads:1", false, ""},
  147. {"/2/threads:2", false, ""},
  148. {"/2/threads:4", false, ""},
  149. {"/2/threads:8", false, ""}});
  150. int main(int argc, char* argv[]) {
  151. benchmark::Initialize(&argc, argv);
  152. TestReporter test_reporter;
  153. benchmark::RunSpecifiedBenchmarks(&test_reporter);
  154. typedef benchmark::BenchmarkReporter::Run Run;
  155. auto EB = ExpectedResults.begin();
  156. for (Run const& run : test_reporter.all_runs_) {
  157. assert(EB != ExpectedResults.end());
  158. EB->CheckRun(run);
  159. ++EB;
  160. }
  161. assert(EB == ExpectedResults.end());
  162. return 0;
  163. }