Debian Codegraph @

test/cpp/microbenchmarks/bm_call_create.cc

raw
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
19 /* This benchmark exists to ensure that the benchmark integration is
20 * working */
21
22 #include <string.h>
23
24 #include <sstream>
25
26 #include <benchmark/benchmark.h>
27
28 #include <grpc/grpc.h>
29 #include <grpc/grpc_security.h>
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/string_util.h>
32 #include <grpcpp/channel.h>
33 #include <grpcpp/support/channel_arguments.h>
34
35 #include "src/core/ext/filters/client_channel/client_channel.h"
36 #include "src/core/ext/filters/deadline/deadline_filter.h"
37 #include "src/core/ext/filters/http/client/http_client_filter.h"
38 #include "src/core/ext/filters/http/message_compress/message_compress_filter.h"
39 #include "src/core/ext/filters/http/server/http_server_filter.h"
40 #include "src/core/ext/filters/message_size/message_size_filter.h"
41 #include "src/core/lib/channel/channel_args.h"
42 #include "src/core/lib/channel/channel_stack.h"
43 #include "src/core/lib/channel/channel_stack_builder_impl.h"
44 #include "src/core/lib/channel/connected_channel.h"
45 #include "src/core/lib/config/core_configuration.h"
46 #include "src/core/lib/iomgr/call_combiner.h"
47 #include "src/core/lib/resource_quota/resource_quota.h"
48 #include "src/core/lib/surface/channel.h"
49 #include "src/core/lib/transport/transport_impl.h"
50 #include "src/cpp/client/create_channel_internal.h"
51 #include "src/proto/grpc/testing/echo.grpc.pb.h"
52 #include "test/core/util/test_config.h"
53 #include "test/cpp/microbenchmarks/helpers.h"
54 #include "test/cpp/util/test_config.h"
55
56 static auto* g_memory_allocator = new grpc_core::MemoryAllocator(
57 grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
58 "test"));
59
60 void BM_Zalloc(benchmark::State& state) {
61 // speed of light for call creation is zalloc, so benchmark a few interesting
62 // sizes
63 size_t sz = state.range(0);
64 for (auto _ : state) {
65 gpr_free(gpr_zalloc(sz));
66 }
67 }
68 BENCHMARK(BM_Zalloc)
69 ->Arg(64)
70 ->Arg(128)
71 ->Arg(256)
72 ->Arg(512)
73 ->Arg(1024)
74 ->Arg(1536)
75 ->Arg(2048)
76 ->Arg(3072)
77 ->Arg(4096)
78 ->Arg(5120)
79 ->Arg(6144)
80 ->Arg(7168);
81
82 ////////////////////////////////////////////////////////////////////////////////
83 // Benchmarks creating full stacks
84
85 class BaseChannelFixture {
86 public:
87 explicit BaseChannelFixture(grpc_channel* channel) : channel_(channel) {}
88 ~BaseChannelFixture() { grpc_channel_destroy(channel_); }
89
90 grpc_channel* channel() const { return channel_; }
91
92 private:
93 grpc_channel* const channel_;
94 };
95
96 static grpc_channel* CreateChannel() {
97 grpc_channel_credentials* creds = grpc_insecure_credentials_create();
98 grpc_channel* channel = grpc_channel_create("localhost:1234", creds, nullptr);
99 grpc_channel_credentials_release(creds);
100 return channel;
101 }
102
103 class InsecureChannel : public BaseChannelFixture {
104 public:
105 InsecureChannel() : BaseChannelFixture(CreateChannel()) {}
106 };
107
108 class LameChannel : public BaseChannelFixture {
109 public:
110 LameChannel()
111 : BaseChannelFixture(grpc_lame_client_channel_create(
112 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah")) {}
113 };
114
115 template <class Fixture>
116 static void BM_CallCreateDestroy(benchmark::State& state) {
117 Fixture fixture;
118 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
119 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
120 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
121 nullptr, nullptr);
122 for (auto _ : state) {
123 grpc_call_unref(grpc_channel_create_registered_call(
124 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, cq, method_hdl,
125 deadline, nullptr));
126 }
127 grpc_completion_queue_destroy(cq);
128 }
129
130 BENCHMARK_TEMPLATE(BM_CallCreateDestroy, InsecureChannel);
131 BENCHMARK_TEMPLATE(BM_CallCreateDestroy, LameChannel);
132
133 ////////////////////////////////////////////////////////////////////////////////
134 // Benchmarks isolating individual filters
135
136 static void* tag(int i) {
137 return reinterpret_cast<void*>(static_cast<intptr_t>(i));
138 }
139
140 static void BM_LameChannelCallCreateCpp(benchmark::State& state) {
141 auto stub =
142 grpc::testing::EchoTestService::NewStub(grpc::CreateChannelInternal(
143 "",
144 grpc_lame_client_channel_create("localhost:1234",
145 GRPC_STATUS_UNAUTHENTICATED, "blah"),
146 std::vector<std::unique_ptr<
147 grpc::experimental::ClientInterceptorFactoryInterface>>()));
148 grpc::CompletionQueue cq;
149 grpc::testing::EchoRequest send_request;
150 grpc::testing::EchoResponse recv_response;
151 grpc::Status recv_status;
152 for (auto _ : state) {
153 grpc::ClientContext cli_ctx;
154 auto reader = stub->AsyncEcho(&cli_ctx, send_request, &cq);
155 reader->Finish(&recv_response, &recv_status, tag(0));
156 void* t;
157 bool ok;
158 GPR_ASSERT(cq.Next(&t, &ok));
159 GPR_ASSERT(ok);
160 }
161 }
162 BENCHMARK(BM_LameChannelCallCreateCpp);
163
164 static void do_nothing(void* /*ignored*/) {}
165
166 static void BM_LameChannelCallCreateCore(benchmark::State& state) {
167 grpc_channel* channel;
168 grpc_completion_queue* cq;
169 grpc_metadata_array initial_metadata_recv;
170 grpc_metadata_array trailing_metadata_recv;
171 grpc_byte_buffer* response_payload_recv = nullptr;
172 grpc_status_code status;
173 grpc_slice details;
174 grpc::testing::EchoRequest send_request;
175 grpc_slice send_request_slice =
176 grpc_slice_new(&send_request, sizeof(send_request), do_nothing);
177
178 channel = grpc_lame_client_channel_create(
179 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
180 cq = grpc_completion_queue_create_for_next(nullptr);
181 void* rc = grpc_channel_register_call(
182 channel, "/grpc.testing.EchoTestService/Echo", nullptr, nullptr);
183 for (auto _ : state) {
184 grpc_call* call = grpc_channel_create_registered_call(
185 channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, rc,
186 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
187 grpc_metadata_array_init(&initial_metadata_recv);
188 grpc_metadata_array_init(&trailing_metadata_recv);
189 grpc_byte_buffer* request_payload_send =
190 grpc_raw_byte_buffer_create(&send_request_slice, 1);
191
192 // Fill in call ops
193 grpc_op ops[6];
194 memset(ops, 0, sizeof(ops));
195 grpc_op* op = ops;
196 op->op = GRPC_OP_SEND_INITIAL_METADATA;
197 op->data.send_initial_metadata.count = 0;
198 op++;
199 op->op = GRPC_OP_SEND_MESSAGE;
200 op->data.send_message.send_message = request_payload_send;
201 op++;
202 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
203 op++;
204 op->op = GRPC_OP_RECV_INITIAL_METADATA;
205 op->data.recv_initial_metadata.recv_initial_metadata =
206 &initial_metadata_recv;
207 op++;
208 op->op = GRPC_OP_RECV_MESSAGE;
209 op->data.recv_message.recv_message = &response_payload_recv;
210 op++;
211 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
212 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
213 op->data.recv_status_on_client.status = &status;
214 op->data.recv_status_on_client.status_details = &details;
215 op++;
216
217 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
218 (size_t)(op - ops),
219 (void*)1, nullptr));
220 grpc_event ev = grpc_completion_queue_next(
221 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
222 GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
223 GPR_ASSERT(ev.success != 0);
224 grpc_call_unref(call);
225 grpc_byte_buffer_destroy(request_payload_send);
226 grpc_byte_buffer_destroy(response_payload_recv);
227 grpc_metadata_array_destroy(&initial_metadata_recv);
228 grpc_metadata_array_destroy(&trailing_metadata_recv);
229 }
230 grpc_channel_destroy(channel);
231 grpc_completion_queue_destroy(cq);
232 grpc_slice_unref(send_request_slice);
233 }
234 BENCHMARK(BM_LameChannelCallCreateCore);
235
236 static void BM_LameChannelCallCreateCoreSeparateBatch(benchmark::State& state) {
237 grpc_channel* channel;
238 grpc_completion_queue* cq;
239 grpc_metadata_array initial_metadata_recv;
240 grpc_metadata_array trailing_metadata_recv;
241 grpc_byte_buffer* response_payload_recv = nullptr;
242 grpc_status_code status;
243 grpc_slice details;
244 grpc::testing::EchoRequest send_request;
245 grpc_slice send_request_slice =
246 grpc_slice_new(&send_request, sizeof(send_request), do_nothing);
247
248 channel = grpc_lame_client_channel_create(
249 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
250 cq = grpc_completion_queue_create_for_next(nullptr);
251 void* rc = grpc_channel_register_call(
252 channel, "/grpc.testing.EchoTestService/Echo", nullptr, nullptr);
253 for (auto _ : state) {
254 grpc_call* call = grpc_channel_create_registered_call(
255 channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, rc,
256 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
257 grpc_metadata_array_init(&initial_metadata_recv);
258 grpc_metadata_array_init(&trailing_metadata_recv);
259 grpc_byte_buffer* request_payload_send =
260 grpc_raw_byte_buffer_create(&send_request_slice, 1);
261
262 // Fill in call ops
263 grpc_op ops[3];
264 memset(ops, 0, sizeof(ops));
265 grpc_op* op = ops;
266 op->op = GRPC_OP_SEND_INITIAL_METADATA;
267 op->data.send_initial_metadata.count = 0;
268 op++;
269 op->op = GRPC_OP_SEND_MESSAGE;
270 op->data.send_message.send_message = request_payload_send;
271 op++;
272 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
273 op++;
274 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
275 (size_t)(op - ops),
276 (void*)nullptr, nullptr));
277 memset(ops, 0, sizeof(ops));
278 op = ops;
279 op->op = GRPC_OP_RECV_INITIAL_METADATA;
280 op->data.recv_initial_metadata.recv_initial_metadata =
281 &initial_metadata_recv;
282 op++;
283 op->op = GRPC_OP_RECV_MESSAGE;
284 op->data.recv_message.recv_message = &response_payload_recv;
285 op++;
286 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
287 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
288 op->data.recv_status_on_client.status = &status;
289 op->data.recv_status_on_client.status_details = &details;
290 op++;
291
292 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
293 (size_t)(op - ops),
294 (void*)1, nullptr));
295 grpc_event ev = grpc_completion_queue_next(
296 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
297 GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
298 GPR_ASSERT(ev.success == 0);
299 ev = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
300 nullptr);
301 GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
302 GPR_ASSERT(ev.success != 0);
303 grpc_call_unref(call);
304 grpc_byte_buffer_destroy(request_payload_send);
305 grpc_byte_buffer_destroy(response_payload_recv);
306 grpc_metadata_array_destroy(&initial_metadata_recv);
307 grpc_metadata_array_destroy(&trailing_metadata_recv);
308 }
309 grpc_channel_destroy(channel);
310 grpc_completion_queue_destroy(cq);
311 grpc_slice_unref(send_request_slice);
312 }
313 BENCHMARK(BM_LameChannelCallCreateCoreSeparateBatch);
314
315 static void FilterDestroy(void* arg, grpc_error_handle /*error*/) {
316 gpr_free(arg);
317 }
318
319 static void DoNothing(void* /*arg*/, grpc_error_handle /*error*/) {}
320
321 class FakeClientChannelFactory : public grpc_core::ClientChannelFactory {
322 public:
323 grpc_core::RefCountedPtr<grpc_core::Subchannel> CreateSubchannel(
324 const grpc_resolved_address& /*address*/,
325 const grpc_core::ChannelArgs& /*args*/) override {
326 return nullptr;
327 }
328 };
329
330 enum FixtureFlags : uint32_t {
331 CHECKS_NOT_LAST = 1,
332 REQUIRES_TRANSPORT = 2,
333 };
334
335 template <const grpc_channel_filter* kFilter, uint32_t kFlags>
336 struct Fixture {
337 const grpc_channel_filter* filter = kFilter;
338 const uint32_t flags = kFlags;
339 };
340
341 namespace phony_filter {
342
343 static void StartTransportStreamOp(grpc_call_element* /*elem*/,
344 grpc_transport_stream_op_batch* /*op*/) {}
345
346 static void StartTransportOp(grpc_channel_element* /*elem*/,
347 grpc_transport_op* /*op*/) {}
348
349 static grpc_error_handle InitCallElem(grpc_call_element* /*elem*/,
350 const grpc_call_element_args* /*args*/) {
351 return absl::OkStatus();
352 }
353
354 static void SetPollsetOrPollsetSet(grpc_call_element* /*elem*/,
355 grpc_polling_entity* /*pollent*/) {}
356
357 static void DestroyCallElem(grpc_call_element* /*elem*/,
358 const grpc_call_final_info* /*final_info*/,
359 grpc_closure* /*then_sched_closure*/) {}
360
361 grpc_error_handle InitChannelElem(grpc_channel_element* /*elem*/,
362 grpc_channel_element_args* /*args*/) {
363 return absl::OkStatus();
364 }
365
366 void DestroyChannelElem(grpc_channel_element* /*elem*/) {}
367
368 void GetChannelInfo(grpc_channel_element* /*elem*/,
369 const grpc_channel_info* /*channel_info*/) {}
370
371 static const grpc_channel_filter phony_filter = {
372 StartTransportStreamOp, nullptr,
373 StartTransportOp, 0,
374 InitCallElem, SetPollsetOrPollsetSet,
375 DestroyCallElem, 0,
376 InitChannelElem, grpc_channel_stack_no_post_init,
377 DestroyChannelElem, GetChannelInfo,
378 "phony_filter"};
379
380 } // namespace phony_filter
381
382 namespace phony_transport {
383
384 /* Memory required for a single stream element - this is allocated by upper
385 layers and initialized by the transport */
386 size_t sizeof_stream; /* = sizeof(transport stream) */
387
388 /* name of this transport implementation */
389 const char* name;
390
391 /* implementation of grpc_transport_init_stream */
392 int InitStream(grpc_transport* /*self*/, grpc_stream* /*stream*/,
393 grpc_stream_refcount* /*refcount*/, const void* /*server_data*/,
394 grpc_core::Arena* /*arena*/) {
395 return 0;
396 }
397
398 /* implementation of grpc_transport_set_pollset */
399 void SetPollset(grpc_transport* /*self*/, grpc_stream* /*stream*/,
400 grpc_pollset* /*pollset*/) {}
401
402 /* implementation of grpc_transport_set_pollset */
403 void SetPollsetSet(grpc_transport* /*self*/, grpc_stream* /*stream*/,
404 grpc_pollset_set* /*pollset_set*/) {}
405
406 /* implementation of grpc_transport_perform_stream_op */
407 void PerformStreamOp(grpc_transport* /*self*/, grpc_stream* /*stream*/,
408 grpc_transport_stream_op_batch* op) {
409 grpc_core::ExecCtx::Run(DEBUG_LOCATION, op->on_complete, absl::OkStatus());
410 }
411
412 /* implementation of grpc_transport_perform_op */
413 void PerformOp(grpc_transport* /*self*/, grpc_transport_op* /*op*/) {}
414
415 /* implementation of grpc_transport_destroy_stream */
416 void DestroyStream(grpc_transport* /*self*/, grpc_stream* /*stream*/,
417 grpc_closure* /*then_sched_closure*/) {}
418
419 /* implementation of grpc_transport_destroy */
420 void Destroy(grpc_transport* /*self*/) {}
421
422 /* implementation of grpc_transport_get_endpoint */
423 grpc_endpoint* GetEndpoint(grpc_transport* /*self*/) { return nullptr; }
424
425 static const grpc_transport_vtable phony_transport_vtable = {0,
426 "phony_http2",
427 InitStream,
428 nullptr,
429 SetPollset,
430 SetPollsetSet,
431 PerformStreamOp,
432 PerformOp,
433 DestroyStream,
434 Destroy,
435 GetEndpoint};
436
437 static grpc_transport phony_transport = {&phony_transport_vtable};
438
439 grpc_arg Arg() {
440 static const grpc_arg_pointer_vtable vtable = {
441 // copy
442 [](void* p) { return p; },
443 // destroy
444 [](void*) {},
445 // cmp
446 [](void* a, void* b) { return grpc_core::QsortCompare(a, b); },
447 };
448 return grpc_channel_arg_pointer_create(const_cast<char*>(GRPC_ARG_TRANSPORT),
449 &phony_transport, &vtable);
450 }
451
452 } // namespace phony_transport
453
454 class NoOp {
455 public:
456 class Op {
457 public:
458 Op(NoOp* /*p*/, grpc_call_stack* /*s*/, grpc_core::Arena*) {}
459 void Finish() {}
460 };
461 };
462
463 class SendEmptyMetadata {
464 public:
465 SendEmptyMetadata() : op_payload_(nullptr) {
466 op_ = {};
467 op_.on_complete = GRPC_CLOSURE_INIT(&closure_, DoNothing, nullptr,
468 grpc_schedule_on_exec_ctx);
469 op_.send_initial_metadata = true;
470 op_.payload = &op_payload_;
471 }
472
473 class Op {
474 public:
475 Op(SendEmptyMetadata* p, grpc_call_stack* /*s*/, grpc_core::Arena* arena)
476 : batch_(arena) {
477 p->op_payload_.send_initial_metadata.send_initial_metadata = &batch_;
478 }
479 void Finish() {}
480
481 private:
482 grpc_metadata_batch batch_;
483 };
484
485 private:
486 const gpr_timespec deadline_ = gpr_inf_future(GPR_CLOCK_MONOTONIC);
487 const gpr_timespec start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
488 const grpc_slice method_ = grpc_slice_from_static_string("/foo/bar");
489 grpc_transport_stream_op_batch op_;
490 grpc_transport_stream_op_batch_payload op_payload_;
491 grpc_closure closure_;
492 };
493
494 // Test a filter in isolation. Fixture specifies the filter under test (use the
495 // Fixture<> template to specify this), and TestOp defines some unit of work to
496 // perform on said filter.
497 template <class Fixture, class TestOp>
498 static void BM_IsolatedFilter(benchmark::State& state) {
499 Fixture fixture;
500 std::ostringstream label;
501 FakeClientChannelFactory fake_client_channel_factory;
502
503 grpc_core::ChannelArgs channel_args =
504 grpc_core::CoreConfiguration::Get()
505 .channel_args_preconditioning()
506 .PreconditionChannelArgs(nullptr)
507 .SetObject(&fake_client_channel_factory)
508 .Set(GRPC_ARG_SERVER_URI, "localhost");
509 if (fixture.flags & REQUIRES_TRANSPORT) {
510 channel_args = channel_args.Set(phony_transport::Arg());
511 }
512
513 std::vector<const grpc_channel_filter*> filters;
514 if (fixture.filter != nullptr) {
515 filters.push_back(fixture.filter);
516 }
517 if (fixture.flags & CHECKS_NOT_LAST) {
518 filters.push_back(&phony_filter::phony_filter);
519 label << " #has_phony_filter";
520 }
521
522 grpc_core::ExecCtx exec_ctx;
523 size_t channel_size = grpc_channel_stack_size(
524 filters.empty() ? nullptr : &filters[0], filters.size());
525 grpc_channel_stack* channel_stack =
526 static_cast<grpc_channel_stack*>(gpr_zalloc(channel_size));
527 GPR_ASSERT(GRPC_LOG_IF_ERROR(
528 "channel_stack_init",
529 grpc_channel_stack_init(1, FilterDestroy, channel_stack,
530 filters.empty() ? nullptr : &filters[0],
531 filters.size(), channel_args, "CHANNEL",
532 channel_stack)));
533 grpc_core::ExecCtx::Get()->Flush();
534 grpc_call_stack* call_stack =
535 static_cast<grpc_call_stack*>(gpr_zalloc(channel_stack->call_stack_size));
536 grpc_core::Timestamp deadline = grpc_core::Timestamp::InfFuture();
537 gpr_cycle_counter start_time = gpr_get_cycle_counter();
538 grpc_slice method = grpc_slice_from_static_string("/foo/bar");
539 grpc_call_final_info final_info;
540 TestOp test_op_data;
541 const int kArenaSize = 32 * 1024 * 1024;
542 grpc_call_context_element context[GRPC_CONTEXT_COUNT] = {};
543 grpc_call_element_args call_args{
544 call_stack,
545 nullptr,
546 context,
547 method,
548 start_time,
549 deadline,
550 grpc_core::Arena::Create(kArenaSize, g_memory_allocator),
551 nullptr};
552 while (state.KeepRunning()) {
553 (void)grpc_call_stack_init(channel_stack, 1, DoNothing, nullptr,
554 &call_args);
555 typename TestOp::Op op(&test_op_data, call_stack, call_args.arena);
556 grpc_call_stack_destroy(call_stack, &final_info, nullptr);
557 op.Finish();
558 grpc_core::ExecCtx::Get()->Flush();
559 // recreate arena every 64k iterations to avoid oom
560 if (0 == (state.iterations() & 0xffff)) {
561 call_args.arena->Destroy();
562 call_args.arena =
563 grpc_core::Arena::Create(kArenaSize, g_memory_allocator);
564 }
565 }
566 call_args.arena->Destroy();
567 grpc_channel_stack_destroy(channel_stack);
568 grpc_core::ExecCtx::Get()->Flush();
569
570 gpr_free(channel_stack);
571 gpr_free(call_stack);
572
573 state.SetLabel(label.str());
574 }
575
576 typedef Fixture<nullptr, 0> NoFilter;
577 BENCHMARK_TEMPLATE(BM_IsolatedFilter, NoFilter, NoOp);
578 typedef Fixture<&phony_filter::phony_filter, 0> PhonyFilter;
579 BENCHMARK_TEMPLATE(BM_IsolatedFilter, PhonyFilter, NoOp);
580 BENCHMARK_TEMPLATE(BM_IsolatedFilter, PhonyFilter, SendEmptyMetadata);
581 typedef Fixture<&grpc_core::ClientChannel::kFilterVtable, 0>
582 ClientChannelFilter;
583 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientChannelFilter, NoOp);
584 typedef Fixture<&grpc_message_compress_filter, CHECKS_NOT_LAST> CompressFilter;
585 BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, NoOp);
586 BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, SendEmptyMetadata);
587 typedef Fixture<&grpc_client_deadline_filter, CHECKS_NOT_LAST>
588 ClientDeadlineFilter;
589 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, NoOp);
590 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, SendEmptyMetadata);
591 typedef Fixture<&grpc_server_deadline_filter, CHECKS_NOT_LAST>
592 ServerDeadlineFilter;
593 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, NoOp);
594 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, SendEmptyMetadata);
595 typedef Fixture<&grpc_core::HttpClientFilter::kFilter,
596 CHECKS_NOT_LAST | REQUIRES_TRANSPORT>
597 HttpClientFilter;
598 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, NoOp);
599 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, SendEmptyMetadata);
600 typedef Fixture<&grpc_core::HttpServerFilter::kFilter, CHECKS_NOT_LAST>
601 HttpServerFilter;
602 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, NoOp);
603 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, SendEmptyMetadata);
604 typedef Fixture<&grpc_message_size_filter, CHECKS_NOT_LAST> MessageSizeFilter;
605 BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, NoOp);
606 BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, SendEmptyMetadata);
607 // This cmake target is disabled for now because it depends on OpenCensus, which
608 // is Bazel-only.
609 // typedef Fixture<&grpc_server_load_reporting_filter, CHECKS_NOT_LAST>
610 // LoadReportingFilter;
611 // BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter, NoOp);
612 // BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter,
613 // SendEmptyMetadata);
614
615 ////////////////////////////////////////////////////////////////////////////////
616 // Benchmarks isolating grpc_call
617
618 namespace isolated_call_filter {
619
620 typedef struct {
621 grpc_core::CallCombiner* call_combiner;
622 } call_data;
623
624 static void StartTransportStreamOp(grpc_call_element* elem,
625 grpc_transport_stream_op_batch* op) {
626 call_data* calld = static_cast<call_data*>(elem->call_data);
627 // Construct list of closures to return.
628 grpc_core::CallCombinerClosureList closures;
629 if (op->recv_initial_metadata) {
630 closures.Add(op->payload->recv_initial_metadata.recv_initial_metadata_ready,
631 absl::OkStatus(), "recv_initial_metadata");
632 }
633 if (op->recv_message) {
634 closures.Add(op->payload->recv_message.recv_message_ready, absl::OkStatus(),
635 "recv_message");
636 }
637 if (op->recv_trailing_metadata) {
638 closures.Add(
639 op->payload->recv_trailing_metadata.recv_trailing_metadata_ready,
640 absl::OkStatus(), "recv_trailing_metadata");
641 }
642 if (op->on_complete != nullptr) {
643 closures.Add(op->on_complete, absl::OkStatus(), "on_complete");
644 }
645 // Execute closures.
646 closures.RunClosures(calld->call_combiner);
647 }
648
649 static void StartTransportOp(grpc_channel_element* /*elem*/,
650 grpc_transport_op* op) {
651 if (!op->disconnect_with_error.ok()) {
652 }
653 grpc_core::ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, absl::OkStatus());
654 }
655
656 static grpc_error_handle InitCallElem(grpc_call_element* elem,
657 const grpc_call_element_args* args) {
658 call_data* calld = static_cast<call_data*>(elem->call_data);
659 calld->call_combiner = args->call_combiner;
660 return absl::OkStatus();
661 }
662
663 static void SetPollsetOrPollsetSet(grpc_call_element* /*elem*/,
664 grpc_polling_entity* /*pollent*/) {}
665
666 static void DestroyCallElem(grpc_call_element* /*elem*/,
667 const grpc_call_final_info* /*final_info*/,
668 grpc_closure* then_sched_closure) {
669 grpc_core::ExecCtx::Run(DEBUG_LOCATION, then_sched_closure, absl::OkStatus());
670 }
671
672 grpc_error_handle InitChannelElem(grpc_channel_element* /*elem*/,
673 grpc_channel_element_args* /*args*/) {
674 return absl::OkStatus();
675 }
676
677 void DestroyChannelElem(grpc_channel_element* /*elem*/) {}
678
679 void GetChannelInfo(grpc_channel_element* /*elem*/,
680 const grpc_channel_info* /*channel_info*/) {}
681
682 static const grpc_channel_filter isolated_call_filter = {
683 StartTransportStreamOp, nullptr,
684 StartTransportOp, sizeof(call_data),
685 InitCallElem, SetPollsetOrPollsetSet,
686 DestroyCallElem, 0,
687 InitChannelElem, grpc_channel_stack_no_post_init,
688 DestroyChannelElem, GetChannelInfo,
689 "isolated_call_filter"};
690 } // namespace isolated_call_filter
691
692 class IsolatedCallFixture {
693 public:
694 IsolatedCallFixture() {
695 // We are calling grpc_channel_stack_builder_create() instead of
696 // grpc_channel_create() here, which means we're not getting the
697 // grpc_init() called by grpc_channel_create(), but we are getting
698 // the grpc_shutdown() run by grpc_channel_destroy(). So we need to
699 // call grpc_init() manually here to balance things out.
700 grpc_init();
701 grpc_core::ChannelStackBuilderImpl builder(
702 "phony", GRPC_CLIENT_CHANNEL,
703 grpc_core::CoreConfiguration::Get()
704 .channel_args_preconditioning()
705 .PreconditionChannelArgs(nullptr));
706 builder.SetTarget("phony_target");
707 builder.AppendFilter(&isolated_call_filter::isolated_call_filter);
708 {
709 grpc_core::ExecCtx exec_ctx;
710 channel_ =
711 grpc_core::Channel::CreateWithBuilder(&builder)->release()->c_ptr();
712 }
713 cq_ = grpc_completion_queue_create_for_next(nullptr);
714 }
715
716 void Finish(benchmark::State&) {
717 grpc_completion_queue_destroy(cq_);
718 grpc_channel_destroy(channel_);
719 }
720
721 grpc_channel* channel() const { return channel_; }
722 grpc_completion_queue* cq() const { return cq_; }
723
724 private:
725 grpc_completion_queue* cq_;
726 grpc_channel* channel_;
727 };
728
729 static void BM_IsolatedCall_NoOp(benchmark::State& state) {
730 IsolatedCallFixture fixture;
731 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
732 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
733 nullptr, nullptr);
734 for (auto _ : state) {
735 grpc_call_unref(grpc_channel_create_registered_call(
736 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
737 method_hdl, deadline, nullptr));
738 }
739 fixture.Finish(state);
740 }
741 BENCHMARK(BM_IsolatedCall_NoOp);
742
743 static void BM_IsolatedCall_Unary(benchmark::State& state) {
744 IsolatedCallFixture fixture;
745 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
746 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
747 nullptr, nullptr);
748 grpc_slice slice = grpc_slice_from_static_string("hello world");
749 grpc_byte_buffer* send_message = grpc_raw_byte_buffer_create(&slice, 1);
750 grpc_byte_buffer* recv_message = nullptr;
751 grpc_status_code status_code;
752 grpc_slice status_details = grpc_empty_slice();
753 grpc_metadata_array recv_initial_metadata;
754 grpc_metadata_array_init(&recv_initial_metadata);
755 grpc_metadata_array recv_trailing_metadata;
756 grpc_metadata_array_init(&recv_trailing_metadata);
757 grpc_op ops[6];
758 memset(ops, 0, sizeof(ops));
759 ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
760 ops[1].op = GRPC_OP_SEND_MESSAGE;
761 ops[1].data.send_message.send_message = send_message;
762 ops[2].op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
763 ops[3].op = GRPC_OP_RECV_INITIAL_METADATA;
764 ops[3].data.recv_initial_metadata.recv_initial_metadata =
765 &recv_initial_metadata;
766 ops[4].op = GRPC_OP_RECV_MESSAGE;
767 ops[4].data.recv_message.recv_message = &recv_message;
768 ops[5].op = GRPC_OP_RECV_STATUS_ON_CLIENT;
769 ops[5].data.recv_status_on_client.status = &status_code;
770 ops[5].data.recv_status_on_client.status_details = &status_details;
771 ops[5].data.recv_status_on_client.trailing_metadata = &recv_trailing_metadata;
772 for (auto _ : state) {
773 grpc_call* call = grpc_channel_create_registered_call(
774 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
775 method_hdl, deadline, nullptr);
776 grpc_call_start_batch(call, ops, 6, tag(1), nullptr);
777 grpc_completion_queue_next(fixture.cq(),
778 gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
779 grpc_call_unref(call);
780 }
781 fixture.Finish(state);
782 grpc_metadata_array_destroy(&recv_initial_metadata);
783 grpc_metadata_array_destroy(&recv_trailing_metadata);
784 grpc_byte_buffer_destroy(send_message);
785 }
786 BENCHMARK(BM_IsolatedCall_Unary);
787
788 static void BM_IsolatedCall_StreamingSend(benchmark::State& state) {
789 IsolatedCallFixture fixture;
790 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
791 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
792 nullptr, nullptr);
793 grpc_slice slice = grpc_slice_from_static_string("hello world");
794 grpc_byte_buffer* send_message = grpc_raw_byte_buffer_create(&slice, 1);
795 grpc_metadata_array recv_initial_metadata;
796 grpc_metadata_array_init(&recv_initial_metadata);
797 grpc_metadata_array recv_trailing_metadata;
798 grpc_metadata_array_init(&recv_trailing_metadata);
799 grpc_op ops[2];
800 memset(ops, 0, sizeof(ops));
801 ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
802 ops[1].op = GRPC_OP_RECV_INITIAL_METADATA;
803 ops[1].data.recv_initial_metadata.recv_initial_metadata =
804 &recv_initial_metadata;
805 grpc_call* call = grpc_channel_create_registered_call(
806 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
807 method_hdl, deadline, nullptr);
808 grpc_call_start_batch(call, ops, 2, tag(1), nullptr);
809 grpc_completion_queue_next(fixture.cq(), gpr_inf_future(GPR_CLOCK_MONOTONIC),
810 nullptr);
811 memset(ops, 0, sizeof(ops));
812 ops[0].op = GRPC_OP_SEND_MESSAGE;
813 ops[0].data.send_message.send_message = send_message;
814 for (auto _ : state) {
815 grpc_call_start_batch(call, ops, 1, tag(2), nullptr);
816 grpc_completion_queue_next(fixture.cq(),
817 gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
818 }
819 grpc_call_unref(call);
820 fixture.Finish(state);
821 grpc_metadata_array_destroy(&recv_initial_metadata);
822 grpc_metadata_array_destroy(&recv_trailing_metadata);
823 grpc_byte_buffer_destroy(send_message);
824 }
825 BENCHMARK(BM_IsolatedCall_StreamingSend);
826
827 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
828 // and others do not. This allows us to support both modes.
829 namespace benchmark {
830 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
831 } // namespace benchmark
832
833 int main(int argc, char** argv) {
834 grpc::testing::TestEnvironment env(&argc, argv);
835 LibraryInitializer libInit;
836 ::benchmark::Initialize(&argc, argv);
837 grpc::testing::InitTest(&argc, &argv, false);
838 benchmark::RunTheBenchmarksNamespaced();
839 return 0;
840 }