bm_call_create.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /*
  2. *
  3. * Copyright 2017 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 <string.h>
  22. #include <sstream>
  23. #include <grpc++/channel.h>
  24. #include <grpc++/support/channel_arguments.h>
  25. #include <grpc/grpc.h>
  26. #include <grpc/support/alloc.h>
  27. #include <grpc/support/string_util.h>
  28. extern "C" {
  29. #include "src/core/ext/filters/client_channel/client_channel.h"
  30. #include "src/core/ext/filters/deadline/deadline_filter.h"
  31. #include "src/core/ext/filters/http/client/http_client_filter.h"
  32. #include "src/core/ext/filters/http/message_compress/message_compress_filter.h"
  33. #include "src/core/ext/filters/http/server/http_server_filter.h"
  34. #include "src/core/ext/filters/load_reporting/server_load_reporting_filter.h"
  35. #include "src/core/ext/filters/message_size/message_size_filter.h"
  36. #include "src/core/lib/channel/channel_stack.h"
  37. #include "src/core/lib/channel/connected_channel.h"
  38. #include "src/core/lib/iomgr/call_combiner.h"
  39. #include "src/core/lib/profiling/timers.h"
  40. #include "src/core/lib/surface/channel.h"
  41. #include "src/core/lib/transport/transport_impl.h"
  42. }
  43. #include "src/cpp/client/create_channel_internal.h"
  44. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  45. #include "test/cpp/microbenchmarks/helpers.h"
  46. auto &force_library_initialization = Library::get();
  47. void BM_Zalloc(benchmark::State &state) {
  48. // speed of light for call creation is zalloc, so benchmark a few interesting
  49. // sizes
  50. TrackCounters track_counters;
  51. size_t sz = state.range(0);
  52. while (state.KeepRunning()) {
  53. gpr_free(gpr_zalloc(sz));
  54. }
  55. track_counters.Finish(state);
  56. }
  57. BENCHMARK(BM_Zalloc)
  58. ->Arg(64)
  59. ->Arg(128)
  60. ->Arg(256)
  61. ->Arg(512)
  62. ->Arg(1024)
  63. ->Arg(1536)
  64. ->Arg(2048)
  65. ->Arg(3072)
  66. ->Arg(4096)
  67. ->Arg(5120)
  68. ->Arg(6144)
  69. ->Arg(7168);
  70. ////////////////////////////////////////////////////////////////////////////////
  71. // Benchmarks creating full stacks
  72. class BaseChannelFixture {
  73. public:
  74. BaseChannelFixture(grpc_channel *channel) : channel_(channel) {}
  75. ~BaseChannelFixture() { grpc_channel_destroy(channel_); }
  76. grpc_channel *channel() const { return channel_; }
  77. private:
  78. grpc_channel *const channel_;
  79. };
  80. class InsecureChannel : public BaseChannelFixture {
  81. public:
  82. InsecureChannel()
  83. : BaseChannelFixture(
  84. grpc_insecure_channel_create("localhost:1234", NULL, NULL)) {}
  85. };
  86. class LameChannel : public BaseChannelFixture {
  87. public:
  88. LameChannel()
  89. : BaseChannelFixture(grpc_lame_client_channel_create(
  90. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah")) {}
  91. };
  92. template <class Fixture>
  93. static void BM_CallCreateDestroy(benchmark::State &state) {
  94. TrackCounters track_counters;
  95. Fixture fixture;
  96. grpc_completion_queue *cq = grpc_completion_queue_create_for_next(NULL);
  97. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  98. void *method_hdl =
  99. grpc_channel_register_call(fixture.channel(), "/foo/bar", NULL, NULL);
  100. while (state.KeepRunning()) {
  101. grpc_call_unref(grpc_channel_create_registered_call(
  102. fixture.channel(), NULL, GRPC_PROPAGATE_DEFAULTS, cq, method_hdl,
  103. deadline, NULL));
  104. }
  105. grpc_completion_queue_destroy(cq);
  106. track_counters.Finish(state);
  107. }
  108. BENCHMARK_TEMPLATE(BM_CallCreateDestroy, InsecureChannel);
  109. BENCHMARK_TEMPLATE(BM_CallCreateDestroy, LameChannel);
  110. ////////////////////////////////////////////////////////////////////////////////
  111. // Benchmarks isolating individual filters
  112. static void *tag(int i) {
  113. return reinterpret_cast<void *>(static_cast<intptr_t>(i));
  114. }
  115. static void BM_LameChannelCallCreateCpp(benchmark::State &state) {
  116. TrackCounters track_counters;
  117. auto stub =
  118. grpc::testing::EchoTestService::NewStub(grpc::CreateChannelInternal(
  119. "", grpc_lame_client_channel_create(
  120. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah")));
  121. grpc::CompletionQueue cq;
  122. grpc::testing::EchoRequest send_request;
  123. grpc::testing::EchoResponse recv_response;
  124. grpc::Status recv_status;
  125. while (state.KeepRunning()) {
  126. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  127. grpc::ClientContext cli_ctx;
  128. auto reader = stub->AsyncEcho(&cli_ctx, send_request, &cq);
  129. reader->Finish(&recv_response, &recv_status, tag(0));
  130. void *t;
  131. bool ok;
  132. GPR_ASSERT(cq.Next(&t, &ok));
  133. GPR_ASSERT(ok);
  134. }
  135. track_counters.Finish(state);
  136. }
  137. BENCHMARK(BM_LameChannelCallCreateCpp);
  138. static void do_nothing(void *ignored) {}
  139. static void BM_LameChannelCallCreateCore(benchmark::State &state) {
  140. TrackCounters track_counters;
  141. grpc_channel *channel;
  142. grpc_completion_queue *cq;
  143. grpc_metadata_array initial_metadata_recv;
  144. grpc_metadata_array trailing_metadata_recv;
  145. grpc_byte_buffer *response_payload_recv = NULL;
  146. grpc_status_code status;
  147. grpc_slice details;
  148. grpc::testing::EchoRequest send_request;
  149. grpc_slice send_request_slice =
  150. grpc_slice_new(&send_request, sizeof(send_request), do_nothing);
  151. channel = grpc_lame_client_channel_create(
  152. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
  153. cq = grpc_completion_queue_create_for_next(NULL);
  154. void *rc = grpc_channel_register_call(
  155. channel, "/grpc.testing.EchoTestService/Echo", NULL, NULL);
  156. while (state.KeepRunning()) {
  157. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  158. grpc_call *call = grpc_channel_create_registered_call(
  159. channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, rc,
  160. gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  161. grpc_metadata_array_init(&initial_metadata_recv);
  162. grpc_metadata_array_init(&trailing_metadata_recv);
  163. grpc_byte_buffer *request_payload_send =
  164. grpc_raw_byte_buffer_create(&send_request_slice, 1);
  165. // Fill in call ops
  166. grpc_op ops[6];
  167. memset(ops, 0, sizeof(ops));
  168. grpc_op *op = ops;
  169. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  170. op->data.send_initial_metadata.count = 0;
  171. op++;
  172. op->op = GRPC_OP_SEND_MESSAGE;
  173. op->data.send_message.send_message = request_payload_send;
  174. op++;
  175. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  176. op++;
  177. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  178. op->data.recv_initial_metadata.recv_initial_metadata =
  179. &initial_metadata_recv;
  180. op++;
  181. op->op = GRPC_OP_RECV_MESSAGE;
  182. op->data.recv_message.recv_message = &response_payload_recv;
  183. op++;
  184. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  185. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  186. op->data.recv_status_on_client.status = &status;
  187. op->data.recv_status_on_client.status_details = &details;
  188. op++;
  189. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
  190. (size_t)(op - ops),
  191. (void *)1, NULL));
  192. grpc_event ev = grpc_completion_queue_next(
  193. cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  194. GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
  195. GPR_ASSERT(ev.success != 0);
  196. grpc_call_unref(call);
  197. grpc_byte_buffer_destroy(request_payload_send);
  198. grpc_byte_buffer_destroy(response_payload_recv);
  199. grpc_metadata_array_destroy(&initial_metadata_recv);
  200. grpc_metadata_array_destroy(&trailing_metadata_recv);
  201. }
  202. grpc_channel_destroy(channel);
  203. grpc_completion_queue_destroy(cq);
  204. grpc_slice_unref(send_request_slice);
  205. track_counters.Finish(state);
  206. }
  207. BENCHMARK(BM_LameChannelCallCreateCore);
  208. static void BM_LameChannelCallCreateCoreSeparateBatch(benchmark::State &state) {
  209. TrackCounters track_counters;
  210. grpc_channel *channel;
  211. grpc_completion_queue *cq;
  212. grpc_metadata_array initial_metadata_recv;
  213. grpc_metadata_array trailing_metadata_recv;
  214. grpc_byte_buffer *response_payload_recv = NULL;
  215. grpc_status_code status;
  216. grpc_slice details;
  217. grpc::testing::EchoRequest send_request;
  218. grpc_slice send_request_slice =
  219. grpc_slice_new(&send_request, sizeof(send_request), do_nothing);
  220. channel = grpc_lame_client_channel_create(
  221. "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
  222. cq = grpc_completion_queue_create_for_next(NULL);
  223. void *rc = grpc_channel_register_call(
  224. channel, "/grpc.testing.EchoTestService/Echo", NULL, NULL);
  225. while (state.KeepRunning()) {
  226. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  227. grpc_call *call = grpc_channel_create_registered_call(
  228. channel, NULL, GRPC_PROPAGATE_DEFAULTS, cq, rc,
  229. gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  230. grpc_metadata_array_init(&initial_metadata_recv);
  231. grpc_metadata_array_init(&trailing_metadata_recv);
  232. grpc_byte_buffer *request_payload_send =
  233. grpc_raw_byte_buffer_create(&send_request_slice, 1);
  234. // Fill in call ops
  235. grpc_op ops[3];
  236. memset(ops, 0, sizeof(ops));
  237. grpc_op *op = ops;
  238. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  239. op->data.send_initial_metadata.count = 0;
  240. op++;
  241. op->op = GRPC_OP_SEND_MESSAGE;
  242. op->data.send_message.send_message = request_payload_send;
  243. op++;
  244. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  245. op++;
  246. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
  247. (size_t)(op - ops),
  248. (void *)0, NULL));
  249. memset(ops, 0, sizeof(ops));
  250. op = ops;
  251. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  252. op->data.recv_initial_metadata.recv_initial_metadata =
  253. &initial_metadata_recv;
  254. op++;
  255. op->op = GRPC_OP_RECV_MESSAGE;
  256. op->data.recv_message.recv_message = &response_payload_recv;
  257. op++;
  258. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  259. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  260. op->data.recv_status_on_client.status = &status;
  261. op->data.recv_status_on_client.status_details = &details;
  262. op++;
  263. GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
  264. (size_t)(op - ops),
  265. (void *)1, NULL));
  266. grpc_event ev = grpc_completion_queue_next(
  267. cq, gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
  268. GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
  269. GPR_ASSERT(ev.success == 0);
  270. ev = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
  271. NULL);
  272. GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
  273. GPR_ASSERT(ev.success != 0);
  274. grpc_call_unref(call);
  275. grpc_byte_buffer_destroy(request_payload_send);
  276. grpc_byte_buffer_destroy(response_payload_recv);
  277. grpc_metadata_array_destroy(&initial_metadata_recv);
  278. grpc_metadata_array_destroy(&trailing_metadata_recv);
  279. }
  280. grpc_channel_destroy(channel);
  281. grpc_completion_queue_destroy(cq);
  282. grpc_slice_unref(send_request_slice);
  283. track_counters.Finish(state);
  284. }
  285. BENCHMARK(BM_LameChannelCallCreateCoreSeparateBatch);
  286. static void FilterDestroy(void *arg, grpc_error *error) { gpr_free(arg); }
  287. static void DoNothing(void *arg, grpc_error *error) {}
  288. class FakeClientChannelFactory : public grpc_client_channel_factory {
  289. public:
  290. FakeClientChannelFactory() { vtable = &vtable_; }
  291. private:
  292. static void NoRef(grpc_client_channel_factory *factory) {}
  293. static void NoUnref(grpc_client_channel_factory *factory) {}
  294. static grpc_subchannel *CreateSubchannel(grpc_client_channel_factory *factory,
  295. const grpc_subchannel_args *args) {
  296. return nullptr;
  297. }
  298. static grpc_channel *CreateClientChannel(grpc_client_channel_factory *factory,
  299. const char *target,
  300. grpc_client_channel_type type,
  301. const grpc_channel_args *args) {
  302. return nullptr;
  303. }
  304. static const grpc_client_channel_factory_vtable vtable_;
  305. };
  306. const grpc_client_channel_factory_vtable FakeClientChannelFactory::vtable_ = {
  307. NoRef, NoUnref, CreateSubchannel, CreateClientChannel};
  308. static grpc_arg StringArg(const char *key, const char *value) {
  309. grpc_arg a;
  310. a.type = GRPC_ARG_STRING;
  311. a.key = const_cast<char *>(key);
  312. a.value.string = const_cast<char *>(value);
  313. return a;
  314. }
  315. enum FixtureFlags : uint32_t {
  316. CHECKS_NOT_LAST = 1,
  317. REQUIRES_TRANSPORT = 2,
  318. };
  319. template <const grpc_channel_filter *kFilter, uint32_t kFlags>
  320. struct Fixture {
  321. const grpc_channel_filter *filter = kFilter;
  322. const uint32_t flags = kFlags;
  323. };
  324. namespace dummy_filter {
  325. static void StartTransportStreamOp(grpc_call_element *elem,
  326. grpc_transport_stream_op_batch *op) {}
  327. static void StartTransportOp(grpc_channel_element *elem,
  328. grpc_transport_op *op) {}
  329. static grpc_error *InitCallElem(grpc_call_element *elem,
  330. const grpc_call_element_args *args) {
  331. return GRPC_ERROR_NONE;
  332. }
  333. static void SetPollsetOrPollsetSet(grpc_call_element *elem,
  334. grpc_polling_entity *pollent) {}
  335. static void DestroyCallElem(grpc_call_element *elem,
  336. const grpc_call_final_info *final_info,
  337. grpc_closure *then_sched_closure) {}
  338. grpc_error *InitChannelElem(grpc_channel_element *elem,
  339. grpc_channel_element_args *args) {
  340. return GRPC_ERROR_NONE;
  341. }
  342. void DestroyChannelElem(grpc_channel_element *elem) {}
  343. void GetChannelInfo(grpc_channel_element *elem,
  344. const grpc_channel_info *channel_info) {}
  345. static const grpc_channel_filter dummy_filter = {StartTransportStreamOp,
  346. StartTransportOp,
  347. 0,
  348. InitCallElem,
  349. SetPollsetOrPollsetSet,
  350. DestroyCallElem,
  351. 0,
  352. InitChannelElem,
  353. DestroyChannelElem,
  354. GetChannelInfo,
  355. "dummy_filter"};
  356. } // namespace dummy_filter
  357. namespace dummy_transport {
  358. /* Memory required for a single stream element - this is allocated by upper
  359. layers and initialized by the transport */
  360. size_t sizeof_stream; /* = sizeof(transport stream) */
  361. /* name of this transport implementation */
  362. const char *name;
  363. /* implementation of grpc_transport_init_stream */
  364. int InitStream(grpc_transport *self, grpc_stream *stream,
  365. grpc_stream_refcount *refcount, const void *server_data,
  366. gpr_arena *arena) {
  367. return 0;
  368. }
  369. /* implementation of grpc_transport_set_pollset */
  370. void SetPollset(grpc_transport *self, grpc_stream *stream,
  371. grpc_pollset *pollset) {}
  372. /* implementation of grpc_transport_set_pollset */
  373. void SetPollsetSet(grpc_transport *self, grpc_stream *stream,
  374. grpc_pollset_set *pollset_set) {}
  375. /* implementation of grpc_transport_perform_stream_op */
  376. void PerformStreamOp(grpc_transport *self, grpc_stream *stream,
  377. grpc_transport_stream_op_batch *op) {
  378. GRPC_CLOSURE_SCHED(op->on_complete, GRPC_ERROR_NONE);
  379. }
  380. /* implementation of grpc_transport_perform_op */
  381. void PerformOp(grpc_transport *self, grpc_transport_op *op) {}
  382. /* implementation of grpc_transport_destroy_stream */
  383. void DestroyStream(grpc_transport *self, grpc_stream *stream,
  384. grpc_closure *then_sched_closure) {}
  385. /* implementation of grpc_transport_destroy */
  386. void Destroy(grpc_transport *self) {}
  387. /* implementation of grpc_transport_get_endpoint */
  388. grpc_endpoint *GetEndpoint(grpc_transport *self) { return nullptr; }
  389. static const grpc_transport_vtable dummy_transport_vtable = {
  390. 0, "dummy_http2", InitStream,
  391. SetPollset, SetPollsetSet, PerformStreamOp,
  392. PerformOp, DestroyStream, Destroy,
  393. GetEndpoint};
  394. static grpc_transport dummy_transport = {&dummy_transport_vtable};
  395. } // namespace dummy_transport
  396. class NoOp {
  397. public:
  398. class Op {
  399. public:
  400. Op(NoOp *p, grpc_call_stack *s) {}
  401. void Finish() {}
  402. };
  403. };
  404. class SendEmptyMetadata {
  405. public:
  406. SendEmptyMetadata() {
  407. memset(&op_, 0, sizeof(op_));
  408. op_.on_complete = GRPC_CLOSURE_INIT(&closure_, DoNothing, nullptr,
  409. grpc_schedule_on_exec_ctx);
  410. op_.send_initial_metadata = true;
  411. op_.payload = &op_payload_;
  412. }
  413. class Op {
  414. public:
  415. Op(SendEmptyMetadata *p, grpc_call_stack *s) {
  416. grpc_metadata_batch_init(&batch_);
  417. p->op_payload_.send_initial_metadata.send_initial_metadata = &batch_;
  418. }
  419. void Finish() { grpc_metadata_batch_destroy(&batch_); }
  420. private:
  421. grpc_metadata_batch batch_;
  422. };
  423. private:
  424. const gpr_timespec deadline_ = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  425. const gpr_timespec start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
  426. const grpc_slice method_ = grpc_slice_from_static_string("/foo/bar");
  427. grpc_transport_stream_op_batch op_;
  428. grpc_transport_stream_op_batch_payload op_payload_;
  429. grpc_closure closure_;
  430. };
  431. // Test a filter in isolation. Fixture specifies the filter under test (use the
  432. // Fixture<> template to specify this), and TestOp defines some unit of work to
  433. // perform on said filter.
  434. template <class Fixture, class TestOp>
  435. static void BM_IsolatedFilter(benchmark::State &state) {
  436. TrackCounters track_counters;
  437. Fixture fixture;
  438. std::ostringstream label;
  439. std::vector<grpc_arg> args;
  440. FakeClientChannelFactory fake_client_channel_factory;
  441. args.push_back(grpc_client_channel_factory_create_channel_arg(
  442. &fake_client_channel_factory));
  443. args.push_back(StringArg(GRPC_ARG_SERVER_URI, "localhost"));
  444. grpc_channel_args channel_args = {args.size(), &args[0]};
  445. std::vector<const grpc_channel_filter *> filters;
  446. if (fixture.filter != nullptr) {
  447. filters.push_back(fixture.filter);
  448. }
  449. if (fixture.flags & CHECKS_NOT_LAST) {
  450. filters.push_back(&dummy_filter::dummy_filter);
  451. label << " #has_dummy_filter";
  452. }
  453. ExecCtx _local_exec_ctx;
  454. size_t channel_size = grpc_channel_stack_size(
  455. filters.size() == 0 ? NULL : &filters[0], filters.size());
  456. grpc_channel_stack *channel_stack =
  457. static_cast<grpc_channel_stack *>(gpr_zalloc(channel_size));
  458. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  459. "channel_stack_init",
  460. grpc_channel_stack_init(1, FilterDestroy, channel_stack, &filters[0],
  461. filters.size(), &channel_args,
  462. fixture.flags & REQUIRES_TRANSPORT
  463. ? &dummy_transport::dummy_transport
  464. : nullptr,
  465. "CHANNEL", channel_stack)));
  466. grpc_exec_ctx_flush();
  467. grpc_call_stack *call_stack = static_cast<grpc_call_stack *>(
  468. gpr_zalloc(channel_stack->call_stack_size));
  469. grpc_millis deadline = GRPC_MILLIS_INF_FUTURE;
  470. gpr_timespec start_time = gpr_now(GPR_CLOCK_MONOTONIC);
  471. grpc_slice method = grpc_slice_from_static_string("/foo/bar");
  472. grpc_call_final_info final_info;
  473. TestOp test_op_data;
  474. grpc_call_element_args call_args;
  475. call_args.call_stack = call_stack;
  476. call_args.server_transport_data = NULL;
  477. call_args.context = NULL;
  478. call_args.path = method;
  479. call_args.start_time = start_time;
  480. call_args.deadline = deadline;
  481. const int kArenaSize = 4096;
  482. call_args.arena = gpr_arena_create(kArenaSize);
  483. while (state.KeepRunning()) {
  484. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  485. GRPC_ERROR_UNREF(
  486. grpc_call_stack_init(channel_stack, 1, DoNothing, NULL, &call_args));
  487. typename TestOp::Op op(&test_op_data, call_stack);
  488. grpc_call_stack_destroy(call_stack, &final_info, NULL);
  489. op.Finish();
  490. grpc_exec_ctx_flush();
  491. // recreate arena every 64k iterations to avoid oom
  492. if (0 == (state.iterations() & 0xffff)) {
  493. gpr_arena_destroy(call_args.arena);
  494. call_args.arena = gpr_arena_create(kArenaSize);
  495. }
  496. }
  497. gpr_arena_destroy(call_args.arena);
  498. grpc_channel_stack_destroy(channel_stack);
  499. grpc_exec_ctx_finish();
  500. gpr_free(channel_stack);
  501. gpr_free(call_stack);
  502. state.SetLabel(label.str());
  503. track_counters.Finish(state);
  504. }
  505. typedef Fixture<nullptr, 0> NoFilter;
  506. BENCHMARK_TEMPLATE(BM_IsolatedFilter, NoFilter, NoOp);
  507. typedef Fixture<&dummy_filter::dummy_filter, 0> DummyFilter;
  508. BENCHMARK_TEMPLATE(BM_IsolatedFilter, DummyFilter, NoOp);
  509. BENCHMARK_TEMPLATE(BM_IsolatedFilter, DummyFilter, SendEmptyMetadata);
  510. typedef Fixture<&grpc_client_channel_filter, 0> ClientChannelFilter;
  511. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientChannelFilter, NoOp);
  512. typedef Fixture<&grpc_message_compress_filter, CHECKS_NOT_LAST> CompressFilter;
  513. BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, NoOp);
  514. BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, SendEmptyMetadata);
  515. typedef Fixture<&grpc_client_deadline_filter, CHECKS_NOT_LAST>
  516. ClientDeadlineFilter;
  517. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, NoOp);
  518. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, SendEmptyMetadata);
  519. typedef Fixture<&grpc_server_deadline_filter, CHECKS_NOT_LAST>
  520. ServerDeadlineFilter;
  521. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, NoOp);
  522. BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, SendEmptyMetadata);
  523. typedef Fixture<&grpc_http_client_filter, CHECKS_NOT_LAST | REQUIRES_TRANSPORT>
  524. HttpClientFilter;
  525. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, NoOp);
  526. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, SendEmptyMetadata);
  527. typedef Fixture<&grpc_http_server_filter, CHECKS_NOT_LAST> HttpServerFilter;
  528. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, NoOp);
  529. BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, SendEmptyMetadata);
  530. typedef Fixture<&grpc_message_size_filter, CHECKS_NOT_LAST> MessageSizeFilter;
  531. BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, NoOp);
  532. BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, SendEmptyMetadata);
  533. typedef Fixture<&grpc_server_load_reporting_filter, CHECKS_NOT_LAST>
  534. LoadReportingFilter;
  535. BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter, NoOp);
  536. BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter, SendEmptyMetadata);
  537. ////////////////////////////////////////////////////////////////////////////////
  538. // Benchmarks isolating grpc_call
  539. namespace isolated_call_filter {
  540. typedef struct { grpc_call_combiner *call_combiner; } call_data;
  541. static void StartTransportStreamOp(grpc_call_element *elem,
  542. grpc_transport_stream_op_batch *op) {
  543. call_data *calld = static_cast<call_data *>(elem->call_data);
  544. if (op->recv_initial_metadata) {
  545. GRPC_CALL_COMBINER_START(
  546. calld->call_combiner,
  547. op->payload->recv_initial_metadata.recv_initial_metadata_ready,
  548. GRPC_ERROR_NONE, "recv_initial_metadata");
  549. }
  550. if (op->recv_message) {
  551. GRPC_CALL_COMBINER_START(calld->call_combiner,
  552. op->payload->recv_message.recv_message_ready,
  553. GRPC_ERROR_NONE, "recv_message");
  554. }
  555. GRPC_CLOSURE_SCHED(op->on_complete, GRPC_ERROR_NONE);
  556. }
  557. static void StartTransportOp(grpc_channel_element *elem,
  558. grpc_transport_op *op) {
  559. if (op->disconnect_with_error != GRPC_ERROR_NONE) {
  560. GRPC_ERROR_UNREF(op->disconnect_with_error);
  561. }
  562. GRPC_CLOSURE_SCHED(op->on_consumed, GRPC_ERROR_NONE);
  563. }
  564. static grpc_error *InitCallElem(grpc_call_element *elem,
  565. const grpc_call_element_args *args) {
  566. call_data *calld = static_cast<call_data *>(elem->call_data);
  567. calld->call_combiner = args->call_combiner;
  568. return GRPC_ERROR_NONE;
  569. }
  570. static void SetPollsetOrPollsetSet(grpc_call_element *elem,
  571. grpc_polling_entity *pollent) {}
  572. static void DestroyCallElem(grpc_call_element *elem,
  573. const grpc_call_final_info *final_info,
  574. grpc_closure *then_sched_closure) {
  575. GRPC_CLOSURE_SCHED(then_sched_closure, GRPC_ERROR_NONE);
  576. }
  577. grpc_error *InitChannelElem(grpc_channel_element *elem,
  578. grpc_channel_element_args *args) {
  579. return GRPC_ERROR_NONE;
  580. }
  581. void DestroyChannelElem(grpc_channel_element *elem) {}
  582. void GetChannelInfo(grpc_channel_element *elem,
  583. const grpc_channel_info *channel_info) {}
  584. static const grpc_channel_filter isolated_call_filter = {
  585. StartTransportStreamOp,
  586. StartTransportOp,
  587. sizeof(call_data),
  588. InitCallElem,
  589. SetPollsetOrPollsetSet,
  590. DestroyCallElem,
  591. 0,
  592. InitChannelElem,
  593. DestroyChannelElem,
  594. GetChannelInfo,
  595. "isolated_call_filter"};
  596. } // namespace isolated_call_filter
  597. class IsolatedCallFixture : public TrackCounters {
  598. public:
  599. IsolatedCallFixture() {
  600. grpc_channel_stack_builder *builder = grpc_channel_stack_builder_create();
  601. grpc_channel_stack_builder_set_name(builder, "dummy");
  602. grpc_channel_stack_builder_set_target(builder, "dummy_target");
  603. GPR_ASSERT(grpc_channel_stack_builder_append_filter(
  604. builder, &isolated_call_filter::isolated_call_filter, NULL, NULL));
  605. {
  606. ExecCtx _local_exec_ctx;
  607. channel_ = grpc_channel_create_with_builder(builder, GRPC_CLIENT_CHANNEL);
  608. grpc_exec_ctx_finish();
  609. }
  610. cq_ = grpc_completion_queue_create_for_next(NULL);
  611. }
  612. void Finish(benchmark::State &state) {
  613. grpc_completion_queue_destroy(cq_);
  614. grpc_channel_destroy(channel_);
  615. TrackCounters::Finish(state);
  616. }
  617. grpc_channel *channel() const { return channel_; }
  618. grpc_completion_queue *cq() const { return cq_; }
  619. private:
  620. grpc_completion_queue *cq_;
  621. grpc_channel *channel_;
  622. };
  623. static void BM_IsolatedCall_NoOp(benchmark::State &state) {
  624. IsolatedCallFixture fixture;
  625. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  626. void *method_hdl =
  627. grpc_channel_register_call(fixture.channel(), "/foo/bar", NULL, NULL);
  628. while (state.KeepRunning()) {
  629. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  630. grpc_call_unref(grpc_channel_create_registered_call(
  631. fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
  632. method_hdl, deadline, NULL));
  633. }
  634. fixture.Finish(state);
  635. }
  636. BENCHMARK(BM_IsolatedCall_NoOp);
  637. static void BM_IsolatedCall_Unary(benchmark::State &state) {
  638. IsolatedCallFixture fixture;
  639. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  640. void *method_hdl =
  641. grpc_channel_register_call(fixture.channel(), "/foo/bar", NULL, NULL);
  642. grpc_slice slice = grpc_slice_from_static_string("hello world");
  643. grpc_byte_buffer *send_message = grpc_raw_byte_buffer_create(&slice, 1);
  644. grpc_byte_buffer *recv_message = NULL;
  645. grpc_status_code status_code;
  646. grpc_slice status_details = grpc_empty_slice();
  647. grpc_metadata_array recv_initial_metadata;
  648. grpc_metadata_array_init(&recv_initial_metadata);
  649. grpc_metadata_array recv_trailing_metadata;
  650. grpc_metadata_array_init(&recv_trailing_metadata);
  651. grpc_op ops[6];
  652. memset(ops, 0, sizeof(ops));
  653. ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
  654. ops[1].op = GRPC_OP_SEND_MESSAGE;
  655. ops[1].data.send_message.send_message = send_message;
  656. ops[2].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  657. ops[3].op = GRPC_OP_RECV_INITIAL_METADATA;
  658. ops[3].data.recv_initial_metadata.recv_initial_metadata =
  659. &recv_initial_metadata;
  660. ops[4].op = GRPC_OP_RECV_MESSAGE;
  661. ops[4].data.recv_message.recv_message = &recv_message;
  662. ops[5].op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  663. ops[5].data.recv_status_on_client.status = &status_code;
  664. ops[5].data.recv_status_on_client.status_details = &status_details;
  665. ops[5].data.recv_status_on_client.trailing_metadata = &recv_trailing_metadata;
  666. while (state.KeepRunning()) {
  667. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  668. grpc_call *call = grpc_channel_create_registered_call(
  669. fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
  670. method_hdl, deadline, NULL);
  671. grpc_call_start_batch(call, ops, 6, tag(1), NULL);
  672. grpc_completion_queue_next(fixture.cq(),
  673. gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL);
  674. grpc_call_unref(call);
  675. }
  676. fixture.Finish(state);
  677. grpc_metadata_array_destroy(&recv_initial_metadata);
  678. grpc_metadata_array_destroy(&recv_trailing_metadata);
  679. grpc_byte_buffer_destroy(send_message);
  680. }
  681. BENCHMARK(BM_IsolatedCall_Unary);
  682. static void BM_IsolatedCall_StreamingSend(benchmark::State &state) {
  683. IsolatedCallFixture fixture;
  684. gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
  685. void *method_hdl =
  686. grpc_channel_register_call(fixture.channel(), "/foo/bar", NULL, NULL);
  687. grpc_slice slice = grpc_slice_from_static_string("hello world");
  688. grpc_byte_buffer *send_message = grpc_raw_byte_buffer_create(&slice, 1);
  689. grpc_metadata_array recv_initial_metadata;
  690. grpc_metadata_array_init(&recv_initial_metadata);
  691. grpc_metadata_array recv_trailing_metadata;
  692. grpc_metadata_array_init(&recv_trailing_metadata);
  693. grpc_op ops[2];
  694. memset(ops, 0, sizeof(ops));
  695. ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
  696. ops[1].op = GRPC_OP_RECV_INITIAL_METADATA;
  697. ops[1].data.recv_initial_metadata.recv_initial_metadata =
  698. &recv_initial_metadata;
  699. grpc_call *call = grpc_channel_create_registered_call(
  700. fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
  701. method_hdl, deadline, NULL);
  702. grpc_call_start_batch(call, ops, 2, tag(1), NULL);
  703. grpc_completion_queue_next(fixture.cq(), gpr_inf_future(GPR_CLOCK_MONOTONIC),
  704. NULL);
  705. memset(ops, 0, sizeof(ops));
  706. ops[0].op = GRPC_OP_SEND_MESSAGE;
  707. ops[0].data.send_message.send_message = send_message;
  708. while (state.KeepRunning()) {
  709. GPR_TIMER_SCOPE("BenchmarkCycle", 0);
  710. grpc_call_start_batch(call, ops, 1, tag(2), NULL);
  711. grpc_completion_queue_next(fixture.cq(),
  712. gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL);
  713. }
  714. grpc_call_unref(call);
  715. fixture.Finish(state);
  716. grpc_metadata_array_destroy(&recv_initial_metadata);
  717. grpc_metadata_array_destroy(&recv_trailing_metadata);
  718. grpc_byte_buffer_destroy(send_message);
  719. }
  720. BENCHMARK(BM_IsolatedCall_StreamingSend);
  721. BENCHMARK_MAIN();