benchmark_random_interleaving_gtest.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <queue>
  2. #include <string>
  3. #include <vector>
  4. #include "../src/commandlineflags.h"
  5. #include "../src/string_util.h"
  6. #include "benchmark/benchmark.h"
  7. #include "gmock/gmock.h"
  8. #include "gtest/gtest.h"
  9. namespace benchmark {
  10. BM_DECLARE_bool(benchmark_enable_random_interleaving);
  11. BM_DECLARE_string(benchmark_filter);
  12. BM_DECLARE_int32(benchmark_repetitions);
  13. namespace internal {
  14. namespace {
  15. class EventQueue : public std::queue<std::string> {
  16. public:
  17. void Put(const std::string& event) { push(event); }
  18. void Clear() {
  19. while (!empty()) {
  20. pop();
  21. }
  22. }
  23. std::string Get() {
  24. std::string event = front();
  25. pop();
  26. return event;
  27. }
  28. };
  29. EventQueue* queue = new EventQueue();
  30. class NullReporter : public BenchmarkReporter {
  31. public:
  32. bool ReportContext(const Context& /*context*/) override { return true; }
  33. void ReportRuns(const std::vector<Run>& /* report */) override {}
  34. };
  35. class BenchmarkTest : public testing::Test {
  36. public:
  37. static void SetupHook(int /* num_threads */) { queue->push("Setup"); }
  38. static void TeardownHook(int /* num_threads */) { queue->push("Teardown"); }
  39. void Execute(const std::string& pattern) {
  40. queue->Clear();
  41. BenchmarkReporter* reporter = new NullReporter;
  42. FLAGS_benchmark_filter = pattern;
  43. RunSpecifiedBenchmarks(reporter);
  44. delete reporter;
  45. queue->Put("DONE"); // End marker
  46. }
  47. };
  48. void BM_Match1(benchmark::State& state) {
  49. const int64_t arg = state.range(0);
  50. for (auto _ : state) {
  51. }
  52. queue->Put(StrFormat("BM_Match1/%d", static_cast<int>(arg)));
  53. }
  54. BENCHMARK(BM_Match1)
  55. ->Iterations(100)
  56. ->Arg(1)
  57. ->Arg(2)
  58. ->Arg(3)
  59. ->Range(10, 80)
  60. ->Args({90})
  61. ->Args({100});
  62. TEST_F(BenchmarkTest, Match1) {
  63. Execute("BM_Match1");
  64. ASSERT_EQ("BM_Match1/1", queue->Get());
  65. ASSERT_EQ("BM_Match1/2", queue->Get());
  66. ASSERT_EQ("BM_Match1/3", queue->Get());
  67. ASSERT_EQ("BM_Match1/10", queue->Get());
  68. ASSERT_EQ("BM_Match1/64", queue->Get());
  69. ASSERT_EQ("BM_Match1/80", queue->Get());
  70. ASSERT_EQ("BM_Match1/90", queue->Get());
  71. ASSERT_EQ("BM_Match1/100", queue->Get());
  72. ASSERT_EQ("DONE", queue->Get());
  73. }
  74. TEST_F(BenchmarkTest, Match1WithRepetition) {
  75. FLAGS_benchmark_repetitions = 2;
  76. Execute("BM_Match1/(64|80)");
  77. ASSERT_EQ("BM_Match1/64", queue->Get());
  78. ASSERT_EQ("BM_Match1/64", queue->Get());
  79. ASSERT_EQ("BM_Match1/80", queue->Get());
  80. ASSERT_EQ("BM_Match1/80", queue->Get());
  81. ASSERT_EQ("DONE", queue->Get());
  82. }
  83. TEST_F(BenchmarkTest, Match1WithRandomInterleaving) {
  84. FLAGS_benchmark_enable_random_interleaving = true;
  85. FLAGS_benchmark_repetitions = 100;
  86. std::map<std::string, int> element_count;
  87. std::map<std::string, int> interleaving_count;
  88. Execute("BM_Match1/(64|80)");
  89. for (int i = 0; i < 100; ++i) {
  90. std::vector<std::string> interleaving;
  91. interleaving.push_back(queue->Get());
  92. interleaving.push_back(queue->Get());
  93. element_count[interleaving[0].c_str()]++;
  94. element_count[interleaving[1].c_str()]++;
  95. interleaving_count[StrFormat("%s,%s", interleaving[0].c_str(),
  96. interleaving[1].c_str())]++;
  97. }
  98. EXPECT_EQ(element_count["BM_Match1/64"], 100) << "Unexpected repetitions.";
  99. EXPECT_EQ(element_count["BM_Match1/80"], 100) << "Unexpected repetitions.";
  100. EXPECT_GE(interleaving_count.size(), 2) << "Interleaving was not randomized.";
  101. ASSERT_EQ("DONE", queue->Get());
  102. }
  103. } // namespace
  104. } // namespace internal
  105. } // namespace benchmark