skip_with_error_test.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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) BENCHMARK_OVERRIDE {
  10. return ConsoleReporter::ReportContext(context);
  11. };
  12. virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
  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. BM_CHECK(name == run.benchmark_name())
  27. << "expected " << name << " got " << run.benchmark_name();
  28. BM_CHECK(error_occurred == run.error_occurred);
  29. BM_CHECK(error_message == run.error_message);
  30. if (error_occurred) {
  31. // BM_CHECK(run.iterations == 0);
  32. } else {
  33. BM_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_no_running(benchmark::State& state) {
  50. state.SkipWithError("error message");
  51. }
  52. BENCHMARK(BM_error_no_running);
  53. ADD_CASES("BM_error_no_running", {{"", true, "error message"}});
  54. void BM_error_before_running(benchmark::State& state) {
  55. state.SkipWithError("error message");
  56. while (state.KeepRunning()) {
  57. assert(false);
  58. }
  59. }
  60. BENCHMARK(BM_error_before_running);
  61. ADD_CASES("BM_error_before_running", {{"", true, "error message"}});
  62. void BM_error_before_running_batch(benchmark::State& state) {
  63. state.SkipWithError("error message");
  64. while (state.KeepRunningBatch(17)) {
  65. assert(false);
  66. }
  67. }
  68. BENCHMARK(BM_error_before_running_batch);
  69. ADD_CASES("BM_error_before_running_batch", {{"", true, "error message"}});
  70. void BM_error_before_running_range_for(benchmark::State& state) {
  71. state.SkipWithError("error message");
  72. for (auto _ : state) {
  73. assert(false);
  74. }
  75. }
  76. BENCHMARK(BM_error_before_running_range_for);
  77. ADD_CASES("BM_error_before_running_range_for", {{"", true, "error message"}});
  78. void BM_error_during_running(benchmark::State& state) {
  79. int first_iter = true;
  80. while (state.KeepRunning()) {
  81. if (state.range(0) == 1 && state.thread_index() <= (state.threads() / 2)) {
  82. assert(first_iter);
  83. first_iter = false;
  84. state.SkipWithError("error message");
  85. } else {
  86. state.PauseTiming();
  87. state.ResumeTiming();
  88. }
  89. }
  90. }
  91. BENCHMARK(BM_error_during_running)->Arg(1)->Arg(2)->ThreadRange(1, 8);
  92. ADD_CASES("BM_error_during_running", {{"/1/threads:1", true, "error message"},
  93. {"/1/threads:2", true, "error message"},
  94. {"/1/threads:4", true, "error message"},
  95. {"/1/threads:8", true, "error message"},
  96. {"/2/threads:1", false, ""},
  97. {"/2/threads:2", false, ""},
  98. {"/2/threads:4", false, ""},
  99. {"/2/threads:8", false, ""}});
  100. void BM_error_during_running_ranged_for(benchmark::State& state) {
  101. assert(state.max_iterations > 3 && "test requires at least a few iterations");
  102. int first_iter = true;
  103. // NOTE: Users should not write the for loop explicitly.
  104. for (auto It = state.begin(), End = state.end(); It != End; ++It) {
  105. if (state.range(0) == 1) {
  106. assert(first_iter);
  107. first_iter = false;
  108. state.SkipWithError("error message");
  109. // Test the unfortunate but documented behavior that the ranged-for loop
  110. // doesn't automatically terminate when SkipWithError is set.
  111. assert(++It != End);
  112. break; // Required behavior
  113. }
  114. }
  115. }
  116. BENCHMARK(BM_error_during_running_ranged_for)->Arg(1)->Arg(2)->Iterations(5);
  117. ADD_CASES("BM_error_during_running_ranged_for",
  118. {{"/1/iterations:5", true, "error message"},
  119. {"/2/iterations:5", false, ""}});
  120. void BM_error_after_running(benchmark::State& state) {
  121. for (auto _ : state) {
  122. benchmark::DoNotOptimize(state.iterations());
  123. }
  124. if (state.thread_index() <= (state.threads() / 2))
  125. state.SkipWithError("error message");
  126. }
  127. BENCHMARK(BM_error_after_running)->ThreadRange(1, 8);
  128. ADD_CASES("BM_error_after_running", {{"/threads:1", true, "error message"},
  129. {"/threads:2", true, "error message"},
  130. {"/threads:4", true, "error message"},
  131. {"/threads:8", true, "error message"}});
  132. void BM_error_while_paused(benchmark::State& state) {
  133. bool first_iter = true;
  134. while (state.KeepRunning()) {
  135. if (state.range(0) == 1 && state.thread_index() <= (state.threads() / 2)) {
  136. assert(first_iter);
  137. first_iter = false;
  138. state.PauseTiming();
  139. state.SkipWithError("error message");
  140. } else {
  141. state.PauseTiming();
  142. state.ResumeTiming();
  143. }
  144. }
  145. }
  146. BENCHMARK(BM_error_while_paused)->Arg(1)->Arg(2)->ThreadRange(1, 8);
  147. ADD_CASES("BM_error_while_paused", {{"/1/threads:1", true, "error message"},
  148. {"/1/threads:2", true, "error message"},
  149. {"/1/threads:4", true, "error message"},
  150. {"/1/threads:8", true, "error message"},
  151. {"/2/threads:1", false, ""},
  152. {"/2/threads:2", false, ""},
  153. {"/2/threads:4", false, ""},
  154. {"/2/threads:8", false, ""}});
  155. int main(int argc, char* argv[]) {
  156. benchmark::Initialize(&argc, argv);
  157. TestReporter test_reporter;
  158. benchmark::RunSpecifiedBenchmarks(&test_reporter);
  159. typedef benchmark::BenchmarkReporter::Run Run;
  160. auto EB = ExpectedResults.begin();
  161. for (Run const& run : test_reporter.all_runs_) {
  162. assert(EB != ExpectedResults.end());
  163. EB->CheckRun(run);
  164. ++EB;
  165. }
  166. assert(EB == ExpectedResults.end());
  167. return 0;
  168. }