bm_cq.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 /* This benchmark exists to ensure that the benchmark integration is
20  * working */
21 
22 #include <benchmark/benchmark.h>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/support/log.h>
28 
33 
34 namespace grpc {
35 namespace testing {
36 
38  TrackCounters track_counters;
39  for (auto _ : state) {
41  }
42  track_counters.Finish(state);
43 }
45 
46 /* Create cq using a different constructor */
48  TrackCounters track_counters;
49  for (auto _ : state) {
50  grpc_completion_queue* core_cq =
52  CompletionQueue cq(core_cq);
53  }
54  track_counters.Finish(state);
55 }
57 
59  TrackCounters track_counters;
60  for (auto _ : state) {
61  // TODO(sreek): Templatize this benchmark and pass completion type and
62  // polling type as parameters
65  }
66  track_counters.Finish(state);
67 }
69 
70 static void DoneWithCompletionOnStack(void* /*arg*/,
71  grpc_cq_completion* /*completion*/) {}
72 
73 static void DoneWithCompletionOnHeap(void* /*arg*/,
75  delete completion;
76 }
77 
78 class PhonyTag final : public internal::CompletionQueueTag {
79  public:
80  bool FinalizeResult(void** /*tag*/, bool* /*status*/) override {
81  return true;
82  }
83 };
84 
86  TrackCounters track_counters;
88  grpc_completion_queue* c_cq = cq.cq();
89  for (auto _ : state) {
91  PhonyTag phony_tag;
93  GPR_ASSERT(grpc_cq_begin_op(c_cq, &phony_tag));
95  nullptr, &completion);
96 
97  void* tag;
98  bool ok;
99  cq.Next(&tag, &ok);
100  }
101  track_counters.Finish(state);
102 }
104 
106  TrackCounters track_counters;
107  // TODO(sreek): Templatize this benchmark and pass polling_type as a param
110  for (auto _ : state) {
113  GPR_ASSERT(grpc_cq_begin_op(cq, nullptr));
115  nullptr, &completion);
116 
117  grpc_completion_queue_next(cq, deadline, nullptr);
118  }
120  track_counters.Finish(state);
121 }
123 
125  TrackCounters track_counters;
126  // TODO(sreek): Templatize this benchmark and pass polling_type as a param
129  for (auto _ : state) {
132  GPR_ASSERT(grpc_cq_begin_op(cq, nullptr));
134  nullptr, &completion);
135 
136  grpc_completion_queue_pluck(cq, nullptr, deadline, nullptr);
137  }
139  track_counters.Finish(state);
140 }
142 
144  TrackCounters track_counters;
145  // TODO(sreek): Templatize this benchmark and pass polling_type as a param
148  for (auto _ : state) {
149  grpc_completion_queue_next(cq, deadline, nullptr);
150  }
152  track_counters.Finish(state);
153 }
155 
156 // Helper for tests to shutdown correctly and tersely
160 }
161 
164 
165 // Tag completion queue iterate times
167  public:
168  explicit TagCallback(int* iter) : iter_(iter) {
170  inlineable = false;
171  }
173  static void Run(grpc_completion_queue_functor* cb, int ok) {
174  gpr_mu_lock(&mu);
175  GPR_ASSERT(static_cast<bool>(ok));
176  *static_cast<TagCallback*>(cb)->iter_ += 1;
177  gpr_cv_signal(&cv);
178  gpr_mu_unlock(&mu);
179  };
180 
181  private:
182  int* iter_;
183 };
184 
185 // Check if completion queue is shut down
187  public:
188  explicit ShutdownCallback(bool* done) : done_(done) {
190  inlineable = false;
191  }
193  static void Run(grpc_completion_queue_functor* cb, int ok) {
195  *static_cast<ShutdownCallback*>(cb)->done_ = static_cast<bool>(ok);
198  }
199 
200  private:
201  bool* done_;
202 };
203 
205  TrackCounters track_counters;
206  int iteration = 0, current_iterations = 0;
207  TagCallback tag_cb(&iteration);
208  gpr_mu_init(&mu);
209  gpr_cv_init(&cv);
212  bool got_shutdown = false;
213  ShutdownCallback shutdown_cb(&got_shutdown);
214  // This test with stack-allocated completions only works for non-polling or
215  // EM-polling callback core CQs because otherwise the callback could execute
216  // on another thread after the stack objects here go out of scope. An
217  // alternative would be to synchronize between the benchmark loop and the
218  // callback, but then it would be measuring the overhead of synchronization
219  // rather than the overhead of the completion queue.
220  // For generality, test here with non-polling.
222  attr.version = 2;
223  attr.cq_completion_type = GRPC_CQ_CALLBACK;
224  attr.cq_polling_type = GRPC_CQ_NON_POLLING;
225  attr.cq_shutdown_cb = &shutdown_cb;
228  for (auto _ : state) {
229  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
232  GPR_ASSERT(grpc_cq_begin_op(cc, &tag_cb));
234  nullptr, &completion);
235  }
237 
238  gpr_mu_lock(&mu);
239  current_iterations = static_cast<int>(state.iterations());
240  while (current_iterations != iteration) {
241  // Wait for all the callbacks to complete.
243  }
244  gpr_mu_unlock(&mu);
245 
247  while (!got_shutdown) {
248  // Wait for the shutdown callback to complete.
250  }
252 
253  GPR_ASSERT(got_shutdown);
254  GPR_ASSERT(iteration == static_cast<int>(state.iterations()));
255  track_counters.Finish(state);
256  gpr_cv_destroy(&cv);
257  gpr_mu_destroy(&mu);
260 }
262  TrackCounters track_counters;
263  int iteration = 0, current_iterations = 0;
264  TagCallback tag_cb(&iteration);
265  gpr_mu_init(&mu);
266  gpr_cv_init(&cv);
269  bool got_shutdown = false;
270  ShutdownCallback shutdown_cb(&got_shutdown);
273  for (auto _ : state) {
274  grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
277  GPR_ASSERT(grpc_cq_begin_op(cc, &tag_cb));
279  nullptr, completion);
280  }
282 
283  gpr_mu_lock(&mu);
284  current_iterations = static_cast<int>(state.iterations());
285  while (current_iterations != iteration) {
286  // Wait for all the callbacks to complete.
288  }
289  gpr_mu_unlock(&mu);
290 
292  while (!got_shutdown) {
293  // Wait for the shutdown callback to complete.
295  }
297 
298  GPR_ASSERT(got_shutdown);
299  GPR_ASSERT(iteration == static_cast<int>(state.iterations()));
300  track_counters.Finish(state);
301  gpr_cv_destroy(&cv);
302  gpr_mu_destroy(&mu);
305 }
308 
309 } // namespace testing
310 } // namespace grpc
311 
312 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
313 // and others do not. This allows us to support both modes.
314 namespace benchmark {
316 } // namespace benchmark
317 
318 int main(int argc, char** argv) {
319  grpc::testing::TestEnvironment env(&argc, argv);
320  LibraryInitializer libInit;
321  ::benchmark::Initialize(&argc, argv);
322  grpc::testing::InitTest(&argc, &argv, false);
324  return 0;
325 }
gpr_cv_signal
GPRAPI void gpr_cv_signal(gpr_cv *cv)
grpc::testing::InitTest
void InitTest(int *argc, char ***argv, bool remove_flags)
Definition: test_config_cc.cc:28
testing
Definition: aws_request_signer_test.cc:25
grpc::testing::BM_Pluck1Core
static void BM_Pluck1Core(benchmark::State &state)
Definition: bm_cq.cc:124
gpr_mu_unlock
GPRAPI void gpr_mu_unlock(gpr_mu *mu)
grpc::testing::PhonyTag
Definition: bm_cq.cc:78
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
log.h
grpc_completion_queue_create_for_callback
GRPCAPI grpc_completion_queue * grpc_completion_queue_create_for_callback(grpc_completion_queue_functor *shutdown_callback, void *reserved)
Definition: completion_queue_factory.cc:76
grpc::testing::TagCallback
Definition: bm_cq.cc:166
benchmark
Definition: bm_alarm.cc:55
generate.env
env
Definition: generate.py:37
grpc_completion_queue_create_for_pluck
GRPCAPI grpc_completion_queue * grpc_completion_queue_create_for_pluck(void *reserved)
Definition: completion_queue_factory.cc:69
grpc
Definition: grpcpp/alarm.h:33
grpc::testing::TagCallback::Run
static void Run(grpc_completion_queue_functor *cb, int ok)
Definition: bm_cq.cc:173
grpc.framework.interfaces.base.utilities.completion
def completion(terminal_metadata, code, message)
Definition: framework/interfaces/base/utilities.py:45
grpc::testing::ShutdownCallback::done_
bool * done_
Definition: bm_cq.cc:201
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
grpc::testing::ShutdownCallback::ShutdownCallback
ShutdownCallback(bool *done)
Definition: bm_cq.cc:188
completion_queue.h
grpc_cq_completion
Definition: src/core/lib/surface/completion_queue.h:43
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
grpc_core::ApplicationCallbackExecCtx
Definition: exec_ctx.h:283
grpc_cq_end_op
void grpc_cq_end_op(grpc_completion_queue *cq, void *tag, grpc_error_handle error, void(*done)(void *done_arg, grpc_cq_completion *storage), void *done_arg, grpc_cq_completion *storage, bool internal)
Definition: completion_queue.cc:894
benchmark::RunTheBenchmarksNamespaced
void RunTheBenchmarksNamespaced()
Definition: bm_alarm.cc:56
main
int main(int argc, char **argv)
Definition: bm_cq.cc:318
TrackCounters::Finish
virtual void Finish(benchmark::State &state)
Definition: helpers.cc:44
grpc_completion_queue_create
GRPCAPI grpc_completion_queue * grpc_completion_queue_create(const grpc_completion_queue_factory *factory, const grpc_completion_queue_attributes *attributes, void *reserved)
Definition: completion_queue_factory.cc:84
grpc::testing::shutdown_mu
static gpr_mu shutdown_mu
Definition: bm_cq.cc:162
grpc::testing::TagCallback::~TagCallback
~TagCallback()
Definition: bm_cq.cc:172
GRPC_CQ_NON_POLLING
@ GRPC_CQ_NON_POLLING
Definition: grpc_types.h:754
grpc::testing::BM_Callback_CQ_Pass1Core
static void BM_Callback_CQ_Pass1Core(benchmark::State &state)
Definition: bm_cq.cc:204
grpc::testing::PhonyTag::FinalizeResult
bool FinalizeResult(void **, bool *) override
Definition: bm_cq.cc:80
grpc::testing::shutdown_cv
static gpr_cv shutdown_cv
Definition: bm_cq.cc:163
gpr_mu_destroy
GPRAPI void gpr_mu_destroy(gpr_mu *mu)
TrackCounters
Definition: helpers.h:51
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark/src/benchmark.cc:437
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_completion_queue_functor::functor_run
void(* functor_run)(struct grpc_completion_queue_functor *, int)
Definition: grpc_types.h:778
gpr_cv_destroy
GPRAPI void gpr_cv_destroy(gpr_cv *cv)
grpc_completion_queue_factory_lookup
const GRPCAPI grpc_completion_queue_factory * grpc_completion_queue_factory_lookup(const grpc_completion_queue_attributes *attributes)
Definition: completion_queue_factory.cc:48
gpr_mu_init
GPRAPI void gpr_mu_init(gpr_mu *mu)
grpc_completion_queue
Definition: completion_queue.cc:347
grpc.h
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
grpc::testing::shutdown_and_destroy
static void shutdown_and_destroy(grpc_completion_queue *cc)
Definition: bm_cq.cc:157
completion_queue.h
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
grpc_cq_completion
struct grpc_cq_completion grpc_cq_completion
grpc::testing::BM_Pass1Cpp
static void BM_Pass1Cpp(benchmark::State &state)
Definition: bm_cq.cc:85
gpr_cv_wait
GPRAPI int gpr_cv_wait(gpr_cv *cv, gpr_mu *mu, gpr_timespec abs_deadline)
grpc::testing::BM_CreateDestroyCore
static void BM_CreateDestroyCore(benchmark::State &state)
Definition: bm_cq.cc:58
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
grpc::testing::ShutdownCallback::~ShutdownCallback
~ShutdownCallback()
Definition: bm_cq.cc:192
gpr_mu_lock
GPRAPI void gpr_mu_lock(gpr_mu *mu)
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark/src/benchmark.cc:602
grpc::testing::ShutdownCallback::Run
static void Run(grpc_completion_queue_functor *cb, int ok)
Definition: bm_cq.cc:193
helpers.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_completion_queue_attributes
Definition: grpc_types.h:791
grpc::testing::BM_CreateDestroyCpp
static void BM_CreateDestroyCpp(benchmark::State &state)
Definition: bm_cq.cc:37
LibraryInitializer
Definition: helpers.h:33
grpc::testing::tag
static void * tag(intptr_t t)
Definition: h2_ssl_cert_test.cc:263
test_config.h
gpr_inf_past
GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:63
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: benchmark-tcp-write-batch.c:80
grpc::testing::ShutdownCallback
Definition: bm_cq.cc:186
grpc_completion_queue_pluck
GRPCAPI grpc_event grpc_completion_queue_pluck(grpc_completion_queue *cq, void *tag, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1328
grpc_cq_begin_op
bool grpc_cq_begin_op(grpc_completion_queue *cq, void *tag)
Definition: completion_queue.cc:672
attr
OPENSSL_EXPORT X509_ATTRIBUTE * attr
Definition: x509.h:1666
grpc::testing::cv
static gpr_cv cv
Definition: bm_cq.cc:163
grpc_library.h
grpc::testing::BENCHMARK
static const int BENCHMARK
Definition: inproc_sync_unary_ping_pong_test.cc:35
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
grpc::testing::BM_Pass1Core
static void BM_Pass1Core(benchmark::State &state)
Definition: bm_cq.cc:105
grpc::testing::TagCallback::iter_
int * iter_
Definition: bm_cq.cc:179
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
ok
bool ok
Definition: async_end2end_test.cc:197
grpc_completion_queue_functor
Definition: grpc_types.h:773
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
test_config.h
grpc::testing::DoneWithCompletionOnStack
static void DoneWithCompletionOnStack(void *, grpc_cq_completion *)
Definition: bm_cq.cc:70
iter
Definition: test_winkernel.cpp:47
grpc_completion_queue_create_for_next
GRPCAPI grpc_completion_queue * grpc_completion_queue_create_for_next(void *reserved)
Definition: completion_queue_factory.cc:62
GRPC_CQ_CALLBACK
@ GRPC_CQ_CALLBACK
Definition: grpc_types.h:766
grpc::CompletionQueue
Definition: include/grpcpp/impl/codegen/completion_queue.h:104
gpr_timespec
Definition: gpr_types.h:50
grpc::internal::CompletionQueueTag
An interface allowing implementors to process and filter event tags.
Definition: grpcpp/impl/codegen/completion_queue_tag.h:28
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc::testing::TagCallback::TagCallback
TagCallback(int *iter)
Definition: bm_cq.cc:168
grpc::testing::BM_Callback_CQ_Pass1CoreHeapCompletion
static void BM_Callback_CQ_Pass1CoreHeapCompletion(benchmark::State &state)
Definition: bm_cq.cc:261
grpc::testing::BM_EmptyCore
static void BM_EmptyCore(benchmark::State &state)
Definition: bm_cq.cc:143
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
grpc::testing::DoneWithCompletionOnHeap
static void DoneWithCompletionOnHeap(void *, grpc_cq_completion *completion)
Definition: bm_cq.cc:73
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
gpr_cv_init
GPRAPI void gpr_cv_init(gpr_cv *cv)
grpc::testing::mu
static gpr_mu mu
Definition: bm_cq.cc:162
grpc_completion_queue_functor::inlineable
int inlineable
Definition: grpc_types.h:782
grpc::testing::BM_CreateDestroyCpp2
static void BM_CreateDestroyCpp2(benchmark::State &state)
Definition: bm_cq.cc:47


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