benchmark_random_interleaving_gtest.cc
Go to the documentation of this file.
1 #include <queue>
2 #include <string>
3 #include <vector>
4 
5 #include "../src/commandlineflags.h"
6 #include "../src/string_util.h"
7 #include "benchmark/benchmark.h"
8 #include "gmock/gmock.h"
9 #include "gtest/gtest.h"
10 
11 namespace benchmark {
12 
13 BM_DECLARE_bool(benchmark_enable_random_interleaving);
14 BM_DECLARE_string(benchmark_filter);
15 BM_DECLARE_int32(benchmark_repetitions);
16 
17 namespace internal {
18 namespace {
19 
20 class EventQueue : public std::queue<std::string> {
21  public:
22  void Put(const std::string& event) { push(event); }
23 
24  void Clear() {
25  while (!empty()) {
26  pop();
27  }
28  }
29 
30  std::string Get() {
31  std::string event = front();
32  pop();
33  return event;
34  }
35 };
36 
37 EventQueue* queue = new EventQueue();
38 
39 class NullReporter : public BenchmarkReporter {
40  public:
41  bool ReportContext(const Context& /*context*/) override { return true; }
42  void ReportRuns(const std::vector<Run>& /* report */) override {}
43 };
44 
45 class BenchmarkTest : public testing::Test {
46  public:
47  static void SetupHook(int /* num_threads */) { queue->push("Setup"); }
48 
49  static void TeardownHook(int /* num_threads */) { queue->push("Teardown"); }
50 
51  void Execute(const std::string& pattern) {
52  queue->Clear();
53 
54  BenchmarkReporter* reporter = new NullReporter;
55  FLAGS_benchmark_filter = pattern;
56  RunSpecifiedBenchmarks(reporter);
57  delete reporter;
58 
59  queue->Put("DONE"); // End marker
60  }
61 };
62 
63 void BM_Match1(benchmark::State& state) {
64  const int64_t arg = state.range(0);
65 
66  for (auto _ : state) {
67  }
68  queue->Put(StrFormat("BM_Match1/%d", static_cast<int>(arg)));
69 }
70 BENCHMARK(BM_Match1)
71  ->Iterations(100)
72  ->Arg(1)
73  ->Arg(2)
74  ->Arg(3)
75  ->Range(10, 80)
76  ->Args({90})
77  ->Args({100});
78 
79 TEST_F(BenchmarkTest, Match1) {
80  Execute("BM_Match1");
81  ASSERT_EQ("BM_Match1/1", queue->Get());
82  ASSERT_EQ("BM_Match1/2", queue->Get());
83  ASSERT_EQ("BM_Match1/3", queue->Get());
84  ASSERT_EQ("BM_Match1/10", queue->Get());
85  ASSERT_EQ("BM_Match1/64", queue->Get());
86  ASSERT_EQ("BM_Match1/80", queue->Get());
87  ASSERT_EQ("BM_Match1/90", queue->Get());
88  ASSERT_EQ("BM_Match1/100", queue->Get());
89  ASSERT_EQ("DONE", queue->Get());
90 }
91 
92 TEST_F(BenchmarkTest, Match1WithRepetition) {
93  FLAGS_benchmark_repetitions = 2;
94 
95  Execute("BM_Match1/(64|80)");
96  ASSERT_EQ("BM_Match1/64", queue->Get());
97  ASSERT_EQ("BM_Match1/64", queue->Get());
98  ASSERT_EQ("BM_Match1/80", queue->Get());
99  ASSERT_EQ("BM_Match1/80", queue->Get());
100  ASSERT_EQ("DONE", queue->Get());
101 }
102 
103 TEST_F(BenchmarkTest, Match1WithRandomInterleaving) {
104  FLAGS_benchmark_enable_random_interleaving = true;
105  FLAGS_benchmark_repetitions = 100;
106 
107  std::map<std::string, int> element_count;
108  std::map<std::string, int> interleaving_count;
109  Execute("BM_Match1/(64|80)");
110  for (int i = 0; i < 100; ++i) {
111  std::vector<std::string> interleaving;
112  interleaving.push_back(queue->Get());
113  interleaving.push_back(queue->Get());
114  element_count[interleaving[0].c_str()]++;
115  element_count[interleaving[1].c_str()]++;
116  interleaving_count[StrFormat("%s,%s", interleaving[0].c_str(),
117  interleaving[1].c_str())]++;
118  }
119  EXPECT_EQ(element_count["BM_Match1/64"], 100) << "Unexpected repetitions.";
120  EXPECT_EQ(element_count["BM_Match1/80"], 100) << "Unexpected repetitions.";
121  EXPECT_GE(interleaving_count.size(), 2) << "Interleaving was not randomized.";
122  ASSERT_EQ("DONE", queue->Get());
123 }
124 
125 } // namespace
126 } // namespace internal
127 } // namespace benchmark
benchmark
Definition: bm_alarm.cc:55
check_tracer_sanity.pattern
pattern
Definition: check_tracer_sanity.py:25
benchmark::BM_DECLARE_string
BM_DECLARE_string(benchmark_perf_counters)
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::BM_DECLARE_int32
BM_DECLARE_int32(benchmark_repetitions)
benchmark::BM_DECLARE_bool
BM_DECLARE_bool(benchmark_report_aggregates_only)
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
absl::synchronization_internal::Get
static GraphId Get(const IdMap &id, int num)
Definition: abseil-cpp/absl/synchronization/internal/graphcycles_test.cc:44
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark/src/benchmark.cc:437
hpack_encoder_fixtures::Args
Args({0, 16384})
queue
Definition: sync_test.cc:39
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
arg
Definition: cmdline.cc:40
google_benchmark.example.empty
def empty(state)
Definition: example.py:31
push
int push(void *desc, unsigned char *buf, unsigned len)
Definition: bloaty/third_party/zlib/test/infcover.c:463
queue
struct queue queue
benchmark::StrFormat
std::string StrFormat(const char *format,...)
Definition: benchmark/src/string_util.cc:158
google::protobuf.internal.python_message.Clear
Clear
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:1430
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
internal
Definition: benchmark/test/output_test_helper.cc:20
BENCHMARK
#define BENCHMARK(n)
Definition: benchmark/include/benchmark/benchmark.h:1170
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
TEST_F
#define TEST_F(test_fixture, test_name)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2367


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:46