bm_cq.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. /* This benchmark exists to ensure that the benchmark integration is
  19. * working */
  20. #include <benchmark/benchmark.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpcpp/completion_queue.h>
  24. #include <grpcpp/impl/grpc_library.h>
  25. #include "src/core/lib/surface/completion_queue.h"
  26. #include "test/core/util/test_config.h"
  27. #include "test/cpp/microbenchmarks/helpers.h"
  28. #include "test/cpp/util/test_config.h"
  29. namespace grpc {
  30. namespace testing {
  31. static void BM_CreateDestroyCpp(benchmark::State& state) {
  32. TrackCounters track_counters;
  33. for (auto _ : state) {
  34. CompletionQueue cq;
  35. }
  36. track_counters.Finish(state);
  37. }
  38. BENCHMARK(BM_CreateDestroyCpp);
  39. /* Create cq using a different constructor */
  40. static void BM_CreateDestroyCpp2(benchmark::State& state) {
  41. TrackCounters track_counters;
  42. for (auto _ : state) {
  43. grpc_completion_queue* core_cq =
  44. grpc_completion_queue_create_for_next(nullptr);
  45. CompletionQueue cq(core_cq);
  46. }
  47. track_counters.Finish(state);
  48. }
  49. BENCHMARK(BM_CreateDestroyCpp2);
  50. static void BM_CreateDestroyCore(benchmark::State& state) {
  51. TrackCounters track_counters;
  52. for (auto _ : state) {
  53. // TODO(sreek): Templatize this benchmark and pass completion type and
  54. // polling type as parameters
  55. grpc_completion_queue_destroy(
  56. grpc_completion_queue_create_for_next(nullptr));
  57. }
  58. track_counters.Finish(state);
  59. }
  60. BENCHMARK(BM_CreateDestroyCore);
  61. static void DoneWithCompletionOnStack(void* /*arg*/,
  62. grpc_cq_completion* /*completion*/) {}
  63. static void DoneWithCompletionOnHeap(void* /*arg*/,
  64. grpc_cq_completion* completion) {
  65. delete completion;
  66. }
  67. class PhonyTag final : public internal::CompletionQueueTag {
  68. public:
  69. bool FinalizeResult(void** /*tag*/, bool* /*status*/) override {
  70. return true;
  71. }
  72. };
  73. static void BM_Pass1Cpp(benchmark::State& state) {
  74. TrackCounters track_counters;
  75. CompletionQueue cq;
  76. grpc_completion_queue* c_cq = cq.cq();
  77. for (auto _ : state) {
  78. grpc_cq_completion completion;
  79. PhonyTag phony_tag;
  80. grpc_core::ExecCtx exec_ctx;
  81. GPR_ASSERT(grpc_cq_begin_op(c_cq, &phony_tag));
  82. grpc_cq_end_op(c_cq, &phony_tag, GRPC_ERROR_NONE, DoneWithCompletionOnStack,
  83. nullptr, &completion);
  84. void* tag;
  85. bool ok;
  86. cq.Next(&tag, &ok);
  87. }
  88. track_counters.Finish(state);
  89. }
  90. BENCHMARK(BM_Pass1Cpp);
  91. static void BM_Pass1Core(benchmark::State& state) {
  92. TrackCounters track_counters;
  93. // TODO(sreek): Templatize this benchmark and pass polling_type as a param
  94. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  95. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  96. for (auto _ : state) {
  97. grpc_cq_completion completion;
  98. grpc_core::ExecCtx exec_ctx;
  99. GPR_ASSERT(grpc_cq_begin_op(cq, nullptr));
  100. grpc_cq_end_op(cq, nullptr, GRPC_ERROR_NONE, DoneWithCompletionOnStack,
  101. nullptr, &completion);
  102. grpc_completion_queue_next(cq, deadline, nullptr);
  103. }
  104. grpc_completion_queue_destroy(cq);
  105. track_counters.Finish(state);
  106. }
  107. BENCHMARK(BM_Pass1Core);
  108. static void BM_Pluck1Core(benchmark::State& state) {
  109. TrackCounters track_counters;
  110. // TODO(sreek): Templatize this benchmark and pass polling_type as a param
  111. grpc_completion_queue* cq = grpc_completion_queue_create_for_pluck(nullptr);
  112. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  113. for (auto _ : state) {
  114. grpc_cq_completion completion;
  115. grpc_core::ExecCtx exec_ctx;
  116. GPR_ASSERT(grpc_cq_begin_op(cq, nullptr));
  117. grpc_cq_end_op(cq, nullptr, GRPC_ERROR_NONE, DoneWithCompletionOnStack,
  118. nullptr, &completion);
  119. grpc_completion_queue_pluck(cq, nullptr, deadline, nullptr);
  120. }
  121. grpc_completion_queue_destroy(cq);
  122. track_counters.Finish(state);
  123. }
  124. BENCHMARK(BM_Pluck1Core);
  125. static void BM_EmptyCore(benchmark::State& state) {
  126. TrackCounters track_counters;
  127. // TODO(sreek): Templatize this benchmark and pass polling_type as a param
  128. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  129. gpr_timespec deadline = gpr_inf_past(GPR_CLOCK_MONOTONIC);
  130. for (auto _ : state) {
  131. grpc_completion_queue_next(cq, deadline, nullptr);
  132. }
  133. grpc_completion_queue_destroy(cq);
  134. track_counters.Finish(state);
  135. }
  136. BENCHMARK(BM_EmptyCore);
  137. // Helper for tests to shutdown correctly and tersely
  138. static void shutdown_and_destroy(grpc_completion_queue* cc) {
  139. grpc_completion_queue_shutdown(cc);
  140. grpc_completion_queue_destroy(cc);
  141. }
  142. static gpr_mu shutdown_mu, mu;
  143. static gpr_cv shutdown_cv, cv;
  144. // Tag completion queue iterate times
  145. class TagCallback : public grpc_completion_queue_functor {
  146. public:
  147. explicit TagCallback(int* iter) : iter_(iter) {
  148. functor_run = &TagCallback::Run;
  149. inlineable = false;
  150. }
  151. ~TagCallback() {}
  152. static void Run(grpc_completion_queue_functor* cb, int ok) {
  153. gpr_mu_lock(&mu);
  154. GPR_ASSERT(static_cast<bool>(ok));
  155. *static_cast<TagCallback*>(cb)->iter_ += 1;
  156. gpr_cv_signal(&cv);
  157. gpr_mu_unlock(&mu);
  158. };
  159. private:
  160. int* iter_;
  161. };
  162. // Check if completion queue is shut down
  163. class ShutdownCallback : public grpc_completion_queue_functor {
  164. public:
  165. explicit ShutdownCallback(bool* done) : done_(done) {
  166. functor_run = &ShutdownCallback::Run;
  167. inlineable = false;
  168. }
  169. ~ShutdownCallback() {}
  170. static void Run(grpc_completion_queue_functor* cb, int ok) {
  171. gpr_mu_lock(&shutdown_mu);
  172. *static_cast<ShutdownCallback*>(cb)->done_ = static_cast<bool>(ok);
  173. gpr_cv_signal(&shutdown_cv);
  174. gpr_mu_unlock(&shutdown_mu);
  175. }
  176. private:
  177. bool* done_;
  178. };
  179. static void BM_Callback_CQ_Pass1Core(benchmark::State& state) {
  180. TrackCounters track_counters;
  181. int iteration = 0, current_iterations = 0;
  182. TagCallback tag_cb(&iteration);
  183. gpr_mu_init(&mu);
  184. gpr_cv_init(&cv);
  185. gpr_mu_init(&shutdown_mu);
  186. gpr_cv_init(&shutdown_cv);
  187. bool got_shutdown = false;
  188. ShutdownCallback shutdown_cb(&got_shutdown);
  189. // This test with stack-allocated completions only works for non-polling or
  190. // EM-polling callback core CQs because otherwise the callback could execute
  191. // on another thread after the stack objects here go out of scope. An
  192. // alternative would be to synchronize between the benchmark loop and the
  193. // callback, but then it would be measuring the overhead of synchronization
  194. // rather than the overhead of the completion queue.
  195. // For generality, test here with non-polling.
  196. grpc_completion_queue_attributes attr;
  197. attr.version = 2;
  198. attr.cq_completion_type = GRPC_CQ_CALLBACK;
  199. attr.cq_polling_type = GRPC_CQ_NON_POLLING;
  200. attr.cq_shutdown_cb = &shutdown_cb;
  201. grpc_completion_queue* cc = grpc_completion_queue_create(
  202. grpc_completion_queue_factory_lookup(&attr), &attr, nullptr);
  203. for (auto _ : state) {
  204. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  205. grpc_core::ExecCtx exec_ctx;
  206. grpc_cq_completion completion;
  207. GPR_ASSERT(grpc_cq_begin_op(cc, &tag_cb));
  208. grpc_cq_end_op(cc, &tag_cb, GRPC_ERROR_NONE, DoneWithCompletionOnStack,
  209. nullptr, &completion);
  210. }
  211. shutdown_and_destroy(cc);
  212. gpr_mu_lock(&mu);
  213. current_iterations = static_cast<int>(state.iterations());
  214. while (current_iterations != iteration) {
  215. // Wait for all the callbacks to complete.
  216. gpr_cv_wait(&cv, &mu, gpr_inf_future(GPR_CLOCK_REALTIME));
  217. }
  218. gpr_mu_unlock(&mu);
  219. gpr_mu_lock(&shutdown_mu);
  220. while (!got_shutdown) {
  221. // Wait for the shutdown callback to complete.
  222. gpr_cv_wait(&shutdown_cv, &shutdown_mu, gpr_inf_future(GPR_CLOCK_REALTIME));
  223. }
  224. gpr_mu_unlock(&shutdown_mu);
  225. GPR_ASSERT(got_shutdown);
  226. GPR_ASSERT(iteration == static_cast<int>(state.iterations()));
  227. track_counters.Finish(state);
  228. gpr_cv_destroy(&cv);
  229. gpr_mu_destroy(&mu);
  230. gpr_cv_destroy(&shutdown_cv);
  231. gpr_mu_destroy(&shutdown_mu);
  232. }
  233. static void BM_Callback_CQ_Pass1CoreHeapCompletion(benchmark::State& state) {
  234. TrackCounters track_counters;
  235. int iteration = 0, current_iterations = 0;
  236. TagCallback tag_cb(&iteration);
  237. gpr_mu_init(&mu);
  238. gpr_cv_init(&cv);
  239. gpr_mu_init(&shutdown_mu);
  240. gpr_cv_init(&shutdown_cv);
  241. bool got_shutdown = false;
  242. ShutdownCallback shutdown_cb(&got_shutdown);
  243. grpc_completion_queue* cc =
  244. grpc_completion_queue_create_for_callback(&shutdown_cb, nullptr);
  245. for (auto _ : state) {
  246. grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
  247. grpc_core::ExecCtx exec_ctx;
  248. grpc_cq_completion* completion = new grpc_cq_completion;
  249. GPR_ASSERT(grpc_cq_begin_op(cc, &tag_cb));
  250. grpc_cq_end_op(cc, &tag_cb, GRPC_ERROR_NONE, DoneWithCompletionOnHeap,
  251. nullptr, completion);
  252. }
  253. shutdown_and_destroy(cc);
  254. gpr_mu_lock(&mu);
  255. current_iterations = static_cast<int>(state.iterations());
  256. while (current_iterations != iteration) {
  257. // Wait for all the callbacks to complete.
  258. gpr_cv_wait(&cv, &mu, gpr_inf_future(GPR_CLOCK_REALTIME));
  259. }
  260. gpr_mu_unlock(&mu);
  261. gpr_mu_lock(&shutdown_mu);
  262. while (!got_shutdown) {
  263. // Wait for the shutdown callback to complete.
  264. gpr_cv_wait(&shutdown_cv, &shutdown_mu, gpr_inf_future(GPR_CLOCK_REALTIME));
  265. }
  266. gpr_mu_unlock(&shutdown_mu);
  267. GPR_ASSERT(got_shutdown);
  268. GPR_ASSERT(iteration == static_cast<int>(state.iterations()));
  269. track_counters.Finish(state);
  270. gpr_cv_destroy(&cv);
  271. gpr_mu_destroy(&mu);
  272. gpr_cv_destroy(&shutdown_cv);
  273. gpr_mu_destroy(&shutdown_mu);
  274. }
  275. BENCHMARK(BM_Callback_CQ_Pass1Core);
  276. BENCHMARK(BM_Callback_CQ_Pass1CoreHeapCompletion);
  277. } // namespace testing
  278. } // namespace grpc
  279. // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
  280. // and others do not. This allows us to support both modes.
  281. namespace benchmark {
  282. void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
  283. } // namespace benchmark
  284. int main(int argc, char** argv) {
  285. grpc::testing::TestEnvironment env(argc, argv);
  286. LibraryInitializer libInit;
  287. ::benchmark::Initialize(&argc, argv);
  288. grpc::testing::InitTest(&argc, &argv, false);
  289. benchmark::RunTheBenchmarksNamespaced();
  290. return 0;
  291. }