bm_call_create.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. *
  3. * Copyright 2017, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. /* This benchmark exists to ensure that the benchmark integration is
  34. * working */
  35. #include <string.h>
  36. #include <sstream>
  37. #include <grpc++/channel.h>
  38. #include <grpc++/support/channel_arguments.h>
  39. #include <grpc/grpc.h>
  40. #include <grpc/support/alloc.h>
  41. #include <grpc/support/string_util.h>
  42. extern "C" {
  43. #include "src/core/ext/client_channel/client_channel.h"
  44. #include "src/core/ext/load_reporting/load_reporting_filter.h"
  45. #include "src/core/lib/channel/channel_stack.h"
  46. #include "src/core/lib/channel/compress_filter.h"
  47. #include "src/core/lib/channel/connected_channel.h"
  48. #include "src/core/lib/channel/deadline_filter.h"
  49. #include "src/core/lib/channel/http_client_filter.h"
  50. #include "src/core/lib/channel/http_server_filter.h"
  51. #include "src/core/lib/channel/message_size_filter.h"
  52. #include "src/core/lib/transport/transport_impl.h"
  53. }
  54. #include "src/cpp/client/create_channel_internal.h"
  55. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  56. #include "test/cpp/microbenchmarks/helpers.h"
  57. #include "third_party/benchmark/include/benchmark/benchmark.h"
  58. auto &force_library_initialization = Library::get();
  59. void BM_Zalloc(benchmark::State &state) {
  60. // speed of light for call creation is zalloc, so benchmark a few interesting
  61. // sizes
  62. TrackCounters track_counters;
  63. size_t sz = state.range(0);
  64. while (state.KeepRunning()) {
  65. gpr_free(gpr_zalloc(sz));
  66. }
  67. track_counters.Finish(state);
  68. }
  69. BENCHMARK(BM_Zalloc)
  70. ->Arg(64)
  71. ->Arg(128)
  72. ->Arg(256)
  73. ->Arg(512)
  74. ->Arg(1024)
  75. ->Arg(1536)
  76. ->Arg(2048)
  77. ->Arg(3072)
  78. ->Arg(4096)
  79. ->Arg(5120)
  80. ->Arg(6144)
  81. ->Arg(7168);
  82. class BaseChannelFixture {
  83. public:
  84. BaseChannelFixture(grpc_channel *channel) : channel_(channel) {}
  85. ~BaseChannelFixture() { grpc_channel_destroy(channel_); }
  86. grpc_channel *channel() const { return channel_; }
  87. private:
  88. grpc_channel *const channel_;
  89. };
  90. class InsecureChannel : public BaseChannelFixture {
  91. public:
  92. InsecureChannel()
  93. : BaseChannelFixture(
  94. grpc_insecure_channel_create("localhost:1234", NULL, NULL)) {}
  95. };
  96. class LameChannel : public BaseChannelFixture {
  97. public:
  98. LameChannel()
  99. : BaseChannelFixture(grpc_lame_client_channel_create(
  100. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah")) {}
  101. };
  102. template <class Fixture>
  103. static void BM_CallCreateDestroy(benchmark::State &state) {
  104. TrackCounters track_counters;
  105. Fixture fixture;
  106. grpc_completion_queue *cq = grpc_completion_queue_create(NULL);
  107. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  108. void *method_hdl =
  109. grpc_channel_register_call(fixture.channel(), "/foo/bar", NULL, NULL);
  110. while (state.KeepRunning()) {
  111. grpc_call_destroy(grpc_channel_create_registered_call(
  112. fixture.channel(), NULL, GRPC_PROPAGATE_DEFAULTS, cq, method_hdl,
  113. deadline, NULL));
  114. }
  115. grpc_completion_queue_destroy(cq);
  116. track_counters.Finish(state);
  117. }
  118. BENCHMARK_TEMPLATE(BM_CallCreateDestroy, InsecureChannel);
  119. BENCHMARK_TEMPLATE(BM_CallCreateDestroy, LameChannel);
  120. static void *tag(int i) {
  121. return reinterpret_cast<void *>(static_cast<intptr_t>(i));
  122. }
  123. static void BM_LameChannelCallCreateCpp(benchmark::State &state) {
  124. TrackCounters track_counters;
  125. auto stub =
  126. grpc::testing::EchoTestService::NewStub(grpc::CreateChannelInternal(
  127. "", grpc_lame_client_channel_create(
  128. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah")));
  129. grpc::CompletionQueue cq;
  130. grpc::testing::EchoRequest send_request;
  131. grpc::testing::EchoResponse recv_response;
  132. grpc::Status recv_status;
  133. while (state.KeepRunning()) {
  134. grpc::ClientContext cli_ctx;
  135. auto reader = stub->AsyncEcho(&cli_ctx, send_request, &cq);
  136. reader->Finish(&recv_response, &recv_status, tag(0));
  137. void *t;
  138. bool ok;
  139. GPR_ASSERT(cq.Next(&t, &ok));
  140. GPR_ASSERT(ok);
  141. }
  142. track_counters.Finish(state);
  143. }
  144. BENCHMARK(BM_LameChannelCallCreateCpp);
  145. static void FilterDestroy(grpc_exec_ctx *exec_ctx, void *arg,
  146. grpc_error *error) {
  147. gpr_free(arg);
  148. }
  149. static void DoNothing(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {}
  150. class FakeClientChannelFactory : public grpc_client_channel_factory {
  151. public:
  152. FakeClientChannelFactory() { vtable = &vtable_; }
  153. private:
  154. static void NoRef(grpc_client_channel_factory *factory) {}
  155. static void NoUnref(grpc_exec_ctx *exec_ctx,
  156. grpc_client_channel_factory *factory) {}
  157. static grpc_subchannel *CreateSubchannel(grpc_exec_ctx *exec_ctx,
  158. grpc_client_channel_factory *factory,
  159. const grpc_subchannel_args *args) {
  160. return nullptr;
  161. }
  162. static grpc_channel *CreateClientChannel(grpc_exec_ctx *exec_ctx,
  163. grpc_client_channel_factory *factory,
  164. const char *target,
  165. grpc_client_channel_type type,
  166. const grpc_channel_args *args) {
  167. return nullptr;
  168. }
  169. static const grpc_client_channel_factory_vtable vtable_;
  170. };
  171. const grpc_client_channel_factory_vtable FakeClientChannelFactory::vtable_ = {
  172. NoRef, NoUnref, CreateSubchannel, CreateClientChannel};
  173. static grpc_arg StringArg(const char *key, const char *value) {
  174. grpc_arg a;
  175. a.type = GRPC_ARG_STRING;
  176. a.key = const_cast<char *>(key);
  177. a.value.string = const_cast<char *>(value);
  178. return a;
  179. }
  180. enum FixtureFlags : uint32_t {
  181. CHECKS_NOT_LAST = 1,
  182. REQUIRES_TRANSPORT = 2,
  183. };
  184. template <const grpc_channel_filter *kFilter, uint32_t kFlags>
  185. struct Fixture {
  186. const grpc_channel_filter *filter = kFilter;
  187. const uint32_t flags = kFlags;
  188. };
  189. namespace dummy_filter {
  190. static void StartTransportStreamOp(grpc_exec_ctx *exec_ctx,
  191. grpc_call_element *elem,
  192. grpc_transport_stream_op *op) {}
  193. static void StartTransportOp(grpc_exec_ctx *exec_ctx,
  194. grpc_channel_element *elem,
  195. grpc_transport_op *op) {}
  196. static grpc_error *InitCallElem(grpc_exec_ctx *exec_ctx,
  197. grpc_call_element *elem,
  198. const grpc_call_element_args *args) {
  199. return GRPC_ERROR_NONE;
  200. }
  201. static void SetPollsetOrPollsetSet(grpc_exec_ctx *exec_ctx,
  202. grpc_call_element *elem,
  203. grpc_polling_entity *pollent) {}
  204. static void DestroyCallElem(grpc_exec_ctx *exec_ctx, grpc_call_element *elem,
  205. const grpc_call_final_info *final_info,
  206. void *and_free_memory) {}
  207. grpc_error *InitChannelElem(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
  208. grpc_channel_element_args *args) {
  209. return GRPC_ERROR_NONE;
  210. }
  211. void DestroyChannelElem(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem) {}
  212. char *GetPeer(grpc_exec_ctx *exec_ctx, grpc_call_element *elem) {
  213. return gpr_strdup("peer");
  214. }
  215. void GetChannelInfo(grpc_exec_ctx *exec_ctx, grpc_channel_element *elem,
  216. const grpc_channel_info *channel_info) {}
  217. static const grpc_channel_filter dummy_filter = {StartTransportStreamOp,
  218. StartTransportOp,
  219. 0,
  220. InitCallElem,
  221. SetPollsetOrPollsetSet,
  222. DestroyCallElem,
  223. 0,
  224. InitChannelElem,
  225. DestroyChannelElem,
  226. GetPeer,
  227. GetChannelInfo,
  228. "dummy_filter"};
  229. } // namespace dummy_filter
  230. namespace dummy_transport {
  231. /* Memory required for a single stream element - this is allocated by upper
  232. layers and initialized by the transport */
  233. size_t sizeof_stream; /* = sizeof(transport stream) */
  234. /* name of this transport implementation */
  235. const char *name;
  236. /* implementation of grpc_transport_init_stream */
  237. int InitStream(grpc_exec_ctx *exec_ctx, grpc_transport *self,
  238. grpc_stream *stream, grpc_stream_refcount *refcount,
  239. const void *server_data) {
  240. return 0;
  241. }
  242. /* implementation of grpc_transport_set_pollset */
  243. void SetPollset(grpc_exec_ctx *exec_ctx, grpc_transport *self,
  244. grpc_stream *stream, grpc_pollset *pollset) {}
  245. /* implementation of grpc_transport_set_pollset */
  246. void SetPollsetSet(grpc_exec_ctx *exec_ctx, grpc_transport *self,
  247. grpc_stream *stream, grpc_pollset_set *pollset_set) {}
  248. /* implementation of grpc_transport_perform_stream_op */
  249. void PerformStreamOp(grpc_exec_ctx *exec_ctx, grpc_transport *self,
  250. grpc_stream *stream, grpc_transport_stream_op *op) {
  251. grpc_closure_sched(exec_ctx, op->on_complete, GRPC_ERROR_NONE);
  252. }
  253. /* implementation of grpc_transport_perform_op */
  254. void PerformOp(grpc_exec_ctx *exec_ctx, grpc_transport *self,
  255. grpc_transport_op *op) {}
  256. /* implementation of grpc_transport_destroy_stream */
  257. void DestroyStream(grpc_exec_ctx *exec_ctx, grpc_transport *self,
  258. grpc_stream *stream, void *and_free_memory) {}
  259. /* implementation of grpc_transport_destroy */
  260. void Destroy(grpc_exec_ctx *exec_ctx, grpc_transport *self) {}
  261. /* implementation of grpc_transport_get_peer */
  262. char *GetPeer(grpc_exec_ctx *exec_ctx, grpc_transport *self) {
  263. return gpr_strdup("transport_peer");
  264. }
  265. /* implementation of grpc_transport_get_endpoint */
  266. grpc_endpoint *GetEndpoint(grpc_exec_ctx *exec_ctx, grpc_transport *self) {
  267. return nullptr;
  268. }
  269. static const grpc_transport_vtable dummy_transport_vtable = {
  270. 0, "dummy_http2", InitStream,
  271. SetPollset, SetPollsetSet, PerformStreamOp,
  272. PerformOp, DestroyStream, Destroy,
  273. GetPeer, GetEndpoint};
  274. static grpc_transport dummy_transport = {&dummy_transport_vtable};
  275. } // namespace dummy_transport
  276. class NoOp {
  277. public:
  278. class Op {
  279. public:
  280. Op(grpc_exec_ctx *exec_ctx, NoOp *p, grpc_call_stack *s) {}
  281. void Finish(grpc_exec_ctx *exec_ctx) {}
  282. };
  283. };
  284. class SendEmptyMetadata {
  285. public:
  286. SendEmptyMetadata() {
  287. memset(&op_, 0, sizeof(op_));
  288. op_.on_complete = grpc_closure_init(&closure_, DoNothing, nullptr,
  289. grpc_schedule_on_exec_ctx);
  290. }
  291. class Op {
  292. public:
  293. Op(grpc_exec_ctx *exec_ctx, SendEmptyMetadata *p, grpc_call_stack *s) {
  294. grpc_metadata_batch_init(&batch_);
  295. p->op_.send_initial_metadata = &batch_;
  296. }
  297. void Finish(grpc_exec_ctx *exec_ctx) {
  298. grpc_metadata_batch_destroy(exec_ctx, &batch_);
  299. }
  300. private:
  301. grpc_metadata_batch batch_;
  302. };
  303. private:
  304. const gpr_timespec deadline_ = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  305. const gpr_timespec start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
  306. const grpc_slice method_ = grpc_slice_from_static_string("/foo/bar");
  307. grpc_transport_stream_op op_;
  308. grpc_closure closure_;
  309. };
  310. // Test a filter in isolation. Fixture specifies the filter under test (use the
  311. // Fixture<> template to specify this), and TestOp defines some unit of work to
  312. // perform on said filter.
  313. template <class Fixture, class TestOp>
  314. static void BM_IsolatedFilter(benchmark::State &state) {
  315. TrackCounters track_counters;
  316. Fixture fixture;
  317. std::ostringstream label;
  318. std::vector<grpc_arg> args;
  319. FakeClientChannelFactory fake_client_channel_factory;
  320. args.push_back(grpc_client_channel_factory_create_channel_arg(
  321. &fake_client_channel_factory));
  322. args.push_back(StringArg(GRPC_ARG_SERVER_URI, "localhost"));
  323. grpc_channel_args channel_args = {args.size(), &args[0]};
  324. std::vector<const grpc_channel_filter *> filters;
  325. if (fixture.filter != nullptr) {
  326. filters.push_back(fixture.filter);
  327. }
  328. if (fixture.flags & CHECKS_NOT_LAST) {
  329. filters.push_back(&dummy_filter::dummy_filter);
  330. label << " #has_dummy_filter";
  331. }
  332. grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
  333. size_t channel_size = grpc_channel_stack_size(&filters[0], filters.size());
  334. grpc_channel_stack *channel_stack =
  335. static_cast<grpc_channel_stack *>(gpr_zalloc(channel_size));
  336. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  337. "call_stack_init",
  338. grpc_channel_stack_init(&exec_ctx, 1, FilterDestroy, channel_stack,
  339. &filters[0], filters.size(), &channel_args,
  340. fixture.flags & REQUIRES_TRANSPORT
  341. ? &dummy_transport::dummy_transport
  342. : nullptr,
  343. "CHANNEL", channel_stack)));
  344. grpc_exec_ctx_flush(&exec_ctx);
  345. grpc_call_stack *call_stack = static_cast<grpc_call_stack *>(
  346. gpr_zalloc(channel_stack->call_stack_size));
  347. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  348. gpr_timespec start_time = gpr_now(GPR_CLOCK_MONOTONIC);
  349. grpc_slice method = grpc_slice_from_static_string("/foo/bar");
  350. grpc_call_final_info final_info;
  351. TestOp test_op_data;
  352. while (state.KeepRunning()) {
  353. GRPC_ERROR_UNREF(grpc_call_stack_init(&exec_ctx, channel_stack, 1,
  354. DoNothing, NULL, NULL, NULL, method,
  355. start_time, deadline, call_stack));
  356. typename TestOp::Op op(&exec_ctx, &test_op_data, call_stack);
  357. grpc_call_stack_destroy(&exec_ctx, call_stack, &final_info, NULL);
  358. op.Finish(&exec_ctx);
  359. grpc_exec_ctx_flush(&exec_ctx);
  360. }
  361. grpc_channel_stack_destroy(&exec_ctx, channel_stack);
  362. grpc_exec_ctx_finish(&exec_ctx);
  363. gpr_free(channel_stack);
  364. gpr_free(call_stack);
  365. state.SetLabel(label.str());
  366. track_counters.Finish(state);
  367. }
  368. typedef Fixture<nullptr, 0> NoFilter;
  369. BENCHMARK_TEMPLATE(BM_IsolatedFilter, NoFilter, NoOp);
  370. typedef Fixture<&dummy_filter::dummy_filter, 0> DummyFilter;
  371. BENCHMARK_TEMPLATE(BM_IsolatedFilter, DummyFilter, NoOp);
  372. BENCHMARK_TEMPLATE(BM_IsolatedFilter, DummyFilter, SendEmptyMetadata);
  373. typedef Fixture<&grpc_client_channel_filter, 0> ClientChannelFilter;
  374. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientChannelFilter, NoOp);
  375. typedef Fixture<&grpc_compress_filter, CHECKS_NOT_LAST> CompressFilter;
  376. BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, NoOp);
  377. BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, SendEmptyMetadata);
  378. typedef Fixture<&grpc_client_deadline_filter, CHECKS_NOT_LAST>
  379. ClientDeadlineFilter;
  380. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, NoOp);
  381. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, SendEmptyMetadata);
  382. typedef Fixture<&grpc_server_deadline_filter, CHECKS_NOT_LAST>
  383. ServerDeadlineFilter;
  384. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, NoOp);
  385. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, SendEmptyMetadata);
  386. typedef Fixture<&grpc_http_client_filter, CHECKS_NOT_LAST | REQUIRES_TRANSPORT>
  387. HttpClientFilter;
  388. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, NoOp);
  389. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, SendEmptyMetadata);
  390. typedef Fixture<&grpc_http_server_filter, CHECKS_NOT_LAST> HttpServerFilter;
  391. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, NoOp);
  392. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, SendEmptyMetadata);
  393. typedef Fixture<&grpc_message_size_filter, CHECKS_NOT_LAST> MessageSizeFilter;
  394. BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, NoOp);
  395. BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, SendEmptyMetadata);
  396. typedef Fixture<&grpc_load_reporting_filter, CHECKS_NOT_LAST>
  397. LoadReportingFilter;
  398. BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter, NoOp);
  399. BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter, SendEmptyMetadata);
  400. BENCHMARK_MAIN();