3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 /* This benchmark exists to ensure that the benchmark integration is
26 #include <benchmark/benchmark.h>
28 #include <grpc/grpc.h>
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/string_util.h>
31 #include <grpcpp/channel.h>
32 #include <grpcpp/support/channel_arguments.h>
34 #include "src/core/ext/filters/client_channel/client_channel.h"
35 #include "src/core/ext/filters/deadline/deadline_filter.h"
36 #include "src/core/ext/filters/http/client/http_client_filter.h"
37 #include "src/core/ext/filters/http/message_compress/message_compress_filter.h"
38 #include "src/core/ext/filters/http/server/http_server_filter.h"
39 #include "src/core/ext/filters/message_size/message_size_filter.h"
40 #include "src/core/lib/channel/channel_stack.h"
41 #include "src/core/lib/channel/connected_channel.h"
42 #include "src/core/lib/iomgr/call_combiner.h"
43 #include "src/core/lib/profiling/timers.h"
44 #include "src/core/lib/surface/channel.h"
45 #include "src/core/lib/transport/transport_impl.h"
46 #include "src/cpp/client/create_channel_internal.h"
47 #include "src/proto/grpc/testing/echo.grpc.pb.h"
48 #include "test/core/util/resource_user_util.h"
49 #include "test/core/util/test_config.h"
50 #include "test/cpp/microbenchmarks/helpers.h"
51 #include "test/cpp/util/test_config.h"
53 void BM_Zalloc(benchmark::State& state) {
54 // speed of light for call creation is zalloc, so benchmark a few interesting
56 TrackCounters track_counters;
57 size_t sz = state.range(0);
58 for (auto _ : state) {
59 gpr_free(gpr_zalloc(sz));
61 track_counters.Finish(state);
77 ////////////////////////////////////////////////////////////////////////////////
78 // Benchmarks creating full stacks
80 class BaseChannelFixture {
82 explicit BaseChannelFixture(grpc_channel* channel) : channel_(channel) {}
83 ~BaseChannelFixture() { grpc_channel_destroy(channel_); }
85 grpc_channel* channel() const { return channel_; }
88 grpc_channel* const channel_;
91 class InsecureChannel : public BaseChannelFixture {
95 grpc_insecure_channel_create("localhost:1234", nullptr, nullptr)) {}
98 class LameChannel : public BaseChannelFixture {
101 : BaseChannelFixture(grpc_lame_client_channel_create(
102 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah")) {}
105 template <class Fixture>
106 static void BM_CallCreateDestroy(benchmark::State& state) {
107 TrackCounters track_counters;
109 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
110 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
111 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
113 for (auto _ : state) {
114 grpc_call_unref(grpc_channel_create_registered_call(
115 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, cq, method_hdl,
118 grpc_completion_queue_destroy(cq);
119 track_counters.Finish(state);
122 BENCHMARK_TEMPLATE(BM_CallCreateDestroy, InsecureChannel);
123 BENCHMARK_TEMPLATE(BM_CallCreateDestroy, LameChannel);
125 ////////////////////////////////////////////////////////////////////////////////
126 // Benchmarks isolating individual filters
128 static void* tag(int i) {
129 return reinterpret_cast<void*>(static_cast<intptr_t>(i));
132 static void BM_LameChannelCallCreateCpp(benchmark::State& state) {
133 TrackCounters track_counters;
135 grpc::testing::EchoTestService::NewStub(grpc::CreateChannelInternal(
137 grpc_lame_client_channel_create("localhost:1234",
138 GRPC_STATUS_UNAUTHENTICATED, "blah"),
139 std::vector<std::unique_ptr<
140 grpc::experimental::ClientInterceptorFactoryInterface>>()));
141 grpc::CompletionQueue cq;
142 grpc::testing::EchoRequest send_request;
143 grpc::testing::EchoResponse recv_response;
144 grpc::Status recv_status;
145 for (auto _ : state) {
146 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
147 grpc::ClientContext cli_ctx;
148 auto reader = stub->AsyncEcho(&cli_ctx, send_request, &cq);
149 reader->Finish(&recv_response, &recv_status, tag(0));
152 GPR_ASSERT(cq.Next(&t, &ok));
155 track_counters.Finish(state);
157 BENCHMARK(BM_LameChannelCallCreateCpp);
159 static void do_nothing(void* /*ignored*/) {}
161 static void BM_LameChannelCallCreateCore(benchmark::State& state) {
162 TrackCounters track_counters;
164 grpc_channel* channel;
165 grpc_completion_queue* cq;
166 grpc_metadata_array initial_metadata_recv;
167 grpc_metadata_array trailing_metadata_recv;
168 grpc_byte_buffer* response_payload_recv = nullptr;
169 grpc_status_code status;
171 grpc::testing::EchoRequest send_request;
172 grpc_slice send_request_slice =
173 grpc_slice_new(&send_request, sizeof(send_request), do_nothing);
175 channel = grpc_lame_client_channel_create(
176 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
177 cq = grpc_completion_queue_create_for_next(nullptr);
178 void* rc = grpc_channel_register_call(
179 channel, "/grpc.testing.EchoTestService/Echo", nullptr, nullptr);
180 for (auto _ : state) {
181 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
182 grpc_call* call = grpc_channel_create_registered_call(
183 channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, rc,
184 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
185 grpc_metadata_array_init(&initial_metadata_recv);
186 grpc_metadata_array_init(&trailing_metadata_recv);
187 grpc_byte_buffer* request_payload_send =
188 grpc_raw_byte_buffer_create(&send_request_slice, 1);
192 memset(ops, 0, sizeof(ops));
194 op->op = GRPC_OP_SEND_INITIAL_METADATA;
195 op->data.send_initial_metadata.count = 0;
197 op->op = GRPC_OP_SEND_MESSAGE;
198 op->data.send_message.send_message = request_payload_send;
200 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
202 op->op = GRPC_OP_RECV_INITIAL_METADATA;
203 op->data.recv_initial_metadata.recv_initial_metadata =
204 &initial_metadata_recv;
206 op->op = GRPC_OP_RECV_MESSAGE;
207 op->data.recv_message.recv_message = &response_payload_recv;
209 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
210 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
211 op->data.recv_status_on_client.status = &status;
212 op->data.recv_status_on_client.status_details = &details;
215 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
218 grpc_event ev = grpc_completion_queue_next(
219 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
220 GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
221 GPR_ASSERT(ev.success != 0);
222 grpc_call_unref(call);
223 grpc_byte_buffer_destroy(request_payload_send);
224 grpc_byte_buffer_destroy(response_payload_recv);
225 grpc_metadata_array_destroy(&initial_metadata_recv);
226 grpc_metadata_array_destroy(&trailing_metadata_recv);
228 grpc_channel_destroy(channel);
229 grpc_completion_queue_destroy(cq);
230 grpc_slice_unref(send_request_slice);
231 track_counters.Finish(state);
233 BENCHMARK(BM_LameChannelCallCreateCore);
235 static void BM_LameChannelCallCreateCoreSeparateBatch(benchmark::State& state) {
236 TrackCounters track_counters;
238 grpc_channel* channel;
239 grpc_completion_queue* cq;
240 grpc_metadata_array initial_metadata_recv;
241 grpc_metadata_array trailing_metadata_recv;
242 grpc_byte_buffer* response_payload_recv = nullptr;
243 grpc_status_code status;
245 grpc::testing::EchoRequest send_request;
246 grpc_slice send_request_slice =
247 grpc_slice_new(&send_request, sizeof(send_request), do_nothing);
249 channel = grpc_lame_client_channel_create(
250 "localhost:1234", GRPC_STATUS_UNAUTHENTICATED, "blah");
251 cq = grpc_completion_queue_create_for_next(nullptr);
252 void* rc = grpc_channel_register_call(
253 channel, "/grpc.testing.EchoTestService/Echo", nullptr, nullptr);
254 for (auto _ : state) {
255 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
256 grpc_call* call = grpc_channel_create_registered_call(
257 channel, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, rc,
258 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
259 grpc_metadata_array_init(&initial_metadata_recv);
260 grpc_metadata_array_init(&trailing_metadata_recv);
261 grpc_byte_buffer* request_payload_send =
262 grpc_raw_byte_buffer_create(&send_request_slice, 1);
266 memset(ops, 0, sizeof(ops));
268 op->op = GRPC_OP_SEND_INITIAL_METADATA;
269 op->data.send_initial_metadata.count = 0;
271 op->op = GRPC_OP_SEND_MESSAGE;
272 op->data.send_message.send_message = request_payload_send;
274 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
276 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
278 (void*)nullptr, nullptr));
279 memset(ops, 0, sizeof(ops));
281 op->op = GRPC_OP_RECV_INITIAL_METADATA;
282 op->data.recv_initial_metadata.recv_initial_metadata =
283 &initial_metadata_recv;
285 op->op = GRPC_OP_RECV_MESSAGE;
286 op->data.recv_message.recv_message = &response_payload_recv;
288 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
289 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
290 op->data.recv_status_on_client.status = &status;
291 op->data.recv_status_on_client.status_details = &details;
294 GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops,
297 grpc_event ev = grpc_completion_queue_next(
298 cq, gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
299 GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
300 GPR_ASSERT(ev.success == 0);
301 ev = grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME),
303 GPR_ASSERT(ev.type != GRPC_QUEUE_SHUTDOWN);
304 GPR_ASSERT(ev.success != 0);
305 grpc_call_unref(call);
306 grpc_byte_buffer_destroy(request_payload_send);
307 grpc_byte_buffer_destroy(response_payload_recv);
308 grpc_metadata_array_destroy(&initial_metadata_recv);
309 grpc_metadata_array_destroy(&trailing_metadata_recv);
311 grpc_channel_destroy(channel);
312 grpc_completion_queue_destroy(cq);
313 grpc_slice_unref(send_request_slice);
314 track_counters.Finish(state);
316 BENCHMARK(BM_LameChannelCallCreateCoreSeparateBatch);
318 static void FilterDestroy(void* arg, grpc_error_handle /*error*/) {
322 static void DoNothing(void* /*arg*/, grpc_error_handle /*error*/) {}
324 class FakeClientChannelFactory : public grpc_core::ClientChannelFactory {
326 grpc_core::RefCountedPtr<grpc_core::Subchannel> CreateSubchannel(
327 const grpc_channel_args* /*args*/) override {
332 static grpc_arg StringArg(const char* key, const char* value) {
334 a.type = GRPC_ARG_STRING;
335 a.key = const_cast<char*>(key);
336 a.value.string = const_cast<char*>(value);
340 enum FixtureFlags : uint32_t {
342 REQUIRES_TRANSPORT = 2,
345 template <const grpc_channel_filter* kFilter, uint32_t kFlags>
347 const grpc_channel_filter* filter = kFilter;
348 const uint32_t flags = kFlags;
351 namespace phony_filter {
353 static void StartTransportStreamOp(grpc_call_element* /*elem*/,
354 grpc_transport_stream_op_batch* /*op*/) {}
356 static void StartTransportOp(grpc_channel_element* /*elem*/,
357 grpc_transport_op* /*op*/) {}
359 static grpc_error_handle InitCallElem(grpc_call_element* /*elem*/,
360 const grpc_call_element_args* /*args*/) {
361 return GRPC_ERROR_NONE;
364 static void SetPollsetOrPollsetSet(grpc_call_element* /*elem*/,
365 grpc_polling_entity* /*pollent*/) {}
367 static void DestroyCallElem(grpc_call_element* /*elem*/,
368 const grpc_call_final_info* /*final_info*/,
369 grpc_closure* /*then_sched_closure*/) {}
371 grpc_error_handle InitChannelElem(grpc_channel_element* /*elem*/,
372 grpc_channel_element_args* /*args*/) {
373 return GRPC_ERROR_NONE;
376 void DestroyChannelElem(grpc_channel_element* /*elem*/) {}
378 void GetChannelInfo(grpc_channel_element* /*elem*/,
379 const grpc_channel_info* /*channel_info*/) {}
381 static const grpc_channel_filter phony_filter = {StartTransportStreamOp,
385 SetPollsetOrPollsetSet,
393 } // namespace phony_filter
395 namespace phony_transport {
397 /* Memory required for a single stream element - this is allocated by upper
398 layers and initialized by the transport */
399 size_t sizeof_stream; /* = sizeof(transport stream) */
401 /* name of this transport implementation */
404 /* implementation of grpc_transport_init_stream */
405 int InitStream(grpc_transport* /*self*/, grpc_stream* /*stream*/,
406 grpc_stream_refcount* /*refcount*/, const void* /*server_data*/,
407 grpc_core::Arena* /*arena*/) {
411 /* implementation of grpc_transport_set_pollset */
412 void SetPollset(grpc_transport* /*self*/, grpc_stream* /*stream*/,
413 grpc_pollset* /*pollset*/) {}
415 /* implementation of grpc_transport_set_pollset */
416 void SetPollsetSet(grpc_transport* /*self*/, grpc_stream* /*stream*/,
417 grpc_pollset_set* /*pollset_set*/) {}
419 /* implementation of grpc_transport_perform_stream_op */
420 void PerformStreamOp(grpc_transport* /*self*/, grpc_stream* /*stream*/,
421 grpc_transport_stream_op_batch* op) {
422 grpc_core::ExecCtx::Run(DEBUG_LOCATION, op->on_complete, GRPC_ERROR_NONE);
425 /* implementation of grpc_transport_perform_op */
426 void PerformOp(grpc_transport* /*self*/, grpc_transport_op* /*op*/) {}
428 /* implementation of grpc_transport_destroy_stream */
429 void DestroyStream(grpc_transport* /*self*/, grpc_stream* /*stream*/,
430 grpc_closure* /*then_sched_closure*/) {}
432 /* implementation of grpc_transport_destroy */
433 void Destroy(grpc_transport* /*self*/) {}
435 /* implementation of grpc_transport_get_endpoint */
436 grpc_endpoint* GetEndpoint(grpc_transport* /*self*/) { return nullptr; }
438 static const grpc_transport_vtable phony_transport_vtable = {
439 0, "phony_http2", InitStream,
440 SetPollset, SetPollsetSet, PerformStreamOp,
441 PerformOp, DestroyStream, Destroy,
444 static grpc_transport phony_transport = {&phony_transport_vtable};
446 } // namespace phony_transport
452 Op(NoOp* /*p*/, grpc_call_stack* /*s*/) {}
457 class SendEmptyMetadata {
459 SendEmptyMetadata() : op_payload_(nullptr) {
461 op_.on_complete = GRPC_CLOSURE_INIT(&closure_, DoNothing, nullptr,
462 grpc_schedule_on_exec_ctx);
463 op_.send_initial_metadata = true;
464 op_.payload = &op_payload_;
469 Op(SendEmptyMetadata* p, grpc_call_stack* /*s*/) {
470 grpc_metadata_batch_init(&batch_);
471 p->op_payload_.send_initial_metadata.send_initial_metadata = &batch_;
473 void Finish() { grpc_metadata_batch_destroy(&batch_); }
476 grpc_metadata_batch batch_;
480 const gpr_timespec deadline_ = gpr_inf_future(GPR_CLOCK_MONOTONIC);
481 const gpr_timespec start_time_ = gpr_now(GPR_CLOCK_MONOTONIC);
482 const grpc_slice method_ = grpc_slice_from_static_string("/foo/bar");
483 grpc_transport_stream_op_batch op_;
484 grpc_transport_stream_op_batch_payload op_payload_;
485 grpc_closure closure_;
488 // Test a filter in isolation. Fixture specifies the filter under test (use the
489 // Fixture<> template to specify this), and TestOp defines some unit of work to
490 // perform on said filter.
491 template <class Fixture, class TestOp>
492 static void BM_IsolatedFilter(benchmark::State& state) {
493 TrackCounters track_counters;
495 std::ostringstream label;
496 FakeClientChannelFactory fake_client_channel_factory;
498 std::vector<grpc_arg> args = {
499 grpc_core::ClientChannelFactory::CreateChannelArg(
500 &fake_client_channel_factory),
501 StringArg(GRPC_ARG_SERVER_URI, "localhost"),
503 grpc_channel_args channel_args = {args.size(), &args[0]};
505 std::vector<const grpc_channel_filter*> filters;
506 if (fixture.filter != nullptr) {
507 filters.push_back(fixture.filter);
509 if (fixture.flags & CHECKS_NOT_LAST) {
510 filters.push_back(&phony_filter::phony_filter);
511 label << " #has_phony_filter";
514 grpc_core::ExecCtx exec_ctx;
515 size_t channel_size = grpc_channel_stack_size(
516 filters.empty() ? nullptr : &filters[0], filters.size());
517 grpc_channel_stack* channel_stack =
518 static_cast<grpc_channel_stack*>(gpr_zalloc(channel_size));
519 GPR_ASSERT(GRPC_LOG_IF_ERROR(
520 "channel_stack_init",
521 grpc_channel_stack_init(1, FilterDestroy, channel_stack,
522 filters.empty() ? nullptr : &filters[0],
523 filters.size(), &channel_args,
524 fixture.flags & REQUIRES_TRANSPORT
525 ? &phony_transport::phony_transport
527 "CHANNEL", channel_stack)));
528 grpc_core::ExecCtx::Get()->Flush();
529 grpc_call_stack* call_stack =
530 static_cast<grpc_call_stack*>(gpr_zalloc(channel_stack->call_stack_size));
531 grpc_millis deadline = GRPC_MILLIS_INF_FUTURE;
532 gpr_cycle_counter start_time = gpr_get_cycle_counter();
533 grpc_slice method = grpc_slice_from_static_string("/foo/bar");
534 grpc_call_final_info final_info;
536 const int kArenaSize = 4096;
537 grpc_call_context_element context[GRPC_CONTEXT_COUNT] = {};
538 grpc_call_element_args call_args{call_stack,
544 grpc_core::Arena::Create(kArenaSize),
546 while (state.KeepRunning()) {
547 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
549 grpc_call_stack_init(channel_stack, 1, DoNothing, nullptr, &call_args));
550 typename TestOp::Op op(&test_op_data, call_stack);
551 grpc_call_stack_destroy(call_stack, &final_info, nullptr);
553 grpc_core::ExecCtx::Get()->Flush();
554 // recreate arena every 64k iterations to avoid oom
555 if (0 == (state.iterations() & 0xffff)) {
556 call_args.arena->Destroy();
557 call_args.arena = grpc_core::Arena::Create(kArenaSize);
560 call_args.arena->Destroy();
561 grpc_channel_stack_destroy(channel_stack);
562 grpc_core::ExecCtx::Get()->Flush();
564 gpr_free(channel_stack);
565 gpr_free(call_stack);
567 state.SetLabel(label.str());
568 track_counters.Finish(state);
571 typedef Fixture<nullptr, 0> NoFilter;
572 BENCHMARK_TEMPLATE(BM_IsolatedFilter, NoFilter, NoOp);
573 typedef Fixture<&phony_filter::phony_filter, 0> PhonyFilter;
574 BENCHMARK_TEMPLATE(BM_IsolatedFilter, PhonyFilter, NoOp);
575 BENCHMARK_TEMPLATE(BM_IsolatedFilter, PhonyFilter, SendEmptyMetadata);
576 typedef Fixture<&grpc_core::ClientChannel::kFilterVtable, 0>
578 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientChannelFilter, NoOp);
579 typedef Fixture<&grpc_message_compress_filter, CHECKS_NOT_LAST> CompressFilter;
580 BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, NoOp);
581 BENCHMARK_TEMPLATE(BM_IsolatedFilter, CompressFilter, SendEmptyMetadata);
582 typedef Fixture<&grpc_client_deadline_filter, CHECKS_NOT_LAST>
583 ClientDeadlineFilter;
584 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, NoOp);
585 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ClientDeadlineFilter, SendEmptyMetadata);
586 typedef Fixture<&grpc_server_deadline_filter, CHECKS_NOT_LAST>
587 ServerDeadlineFilter;
588 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, NoOp);
589 BENCHMARK_TEMPLATE(BM_IsolatedFilter, ServerDeadlineFilter, SendEmptyMetadata);
590 typedef Fixture<&grpc_http_client_filter, CHECKS_NOT_LAST | REQUIRES_TRANSPORT>
592 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, NoOp);
593 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpClientFilter, SendEmptyMetadata);
594 typedef Fixture<&grpc_http_server_filter, CHECKS_NOT_LAST> HttpServerFilter;
595 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, NoOp);
596 BENCHMARK_TEMPLATE(BM_IsolatedFilter, HttpServerFilter, SendEmptyMetadata);
597 typedef Fixture<&grpc_message_size_filter, CHECKS_NOT_LAST> MessageSizeFilter;
598 BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, NoOp);
599 BENCHMARK_TEMPLATE(BM_IsolatedFilter, MessageSizeFilter, SendEmptyMetadata);
600 // This cmake target is disabled for now because it depends on OpenCensus, which
602 // typedef Fixture<&grpc_server_load_reporting_filter, CHECKS_NOT_LAST>
603 // LoadReportingFilter;
604 // BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter, NoOp);
605 // BENCHMARK_TEMPLATE(BM_IsolatedFilter, LoadReportingFilter,
606 // SendEmptyMetadata);
608 ////////////////////////////////////////////////////////////////////////////////
609 // Benchmarks isolating grpc_call
611 namespace isolated_call_filter {
614 grpc_core::CallCombiner* call_combiner;
617 static void StartTransportStreamOp(grpc_call_element* elem,
618 grpc_transport_stream_op_batch* op) {
619 call_data* calld = static_cast<call_data*>(elem->call_data);
620 // Construct list of closures to return.
621 grpc_core::CallCombinerClosureList closures;
622 if (op->recv_initial_metadata) {
623 closures.Add(op->payload->recv_initial_metadata.recv_initial_metadata_ready,
624 GRPC_ERROR_NONE, "recv_initial_metadata");
626 if (op->recv_message) {
627 closures.Add(op->payload->recv_message.recv_message_ready, GRPC_ERROR_NONE,
630 if (op->recv_trailing_metadata) {
632 op->payload->recv_trailing_metadata.recv_trailing_metadata_ready,
633 GRPC_ERROR_NONE, "recv_trailing_metadata");
635 if (op->on_complete != nullptr) {
636 closures.Add(op->on_complete, GRPC_ERROR_NONE, "on_complete");
639 closures.RunClosures(calld->call_combiner);
642 static void StartTransportOp(grpc_channel_element* /*elem*/,
643 grpc_transport_op* op) {
644 if (op->disconnect_with_error != GRPC_ERROR_NONE) {
645 GRPC_ERROR_UNREF(op->disconnect_with_error);
647 grpc_core::ExecCtx::Run(DEBUG_LOCATION, op->on_consumed, GRPC_ERROR_NONE);
650 static grpc_error_handle InitCallElem(grpc_call_element* elem,
651 const grpc_call_element_args* args) {
652 call_data* calld = static_cast<call_data*>(elem->call_data);
653 calld->call_combiner = args->call_combiner;
654 return GRPC_ERROR_NONE;
657 static void SetPollsetOrPollsetSet(grpc_call_element* /*elem*/,
658 grpc_polling_entity* /*pollent*/) {}
660 static void DestroyCallElem(grpc_call_element* /*elem*/,
661 const grpc_call_final_info* /*final_info*/,
662 grpc_closure* then_sched_closure) {
663 grpc_core::ExecCtx::Run(DEBUG_LOCATION, then_sched_closure, GRPC_ERROR_NONE);
666 grpc_error_handle InitChannelElem(grpc_channel_element* /*elem*/,
667 grpc_channel_element_args* /*args*/) {
668 return GRPC_ERROR_NONE;
671 void DestroyChannelElem(grpc_channel_element* /*elem*/) {}
673 void GetChannelInfo(grpc_channel_element* /*elem*/,
674 const grpc_channel_info* /*channel_info*/) {}
676 static const grpc_channel_filter isolated_call_filter = {
677 StartTransportStreamOp,
681 SetPollsetOrPollsetSet,
687 "isolated_call_filter"};
688 } // namespace isolated_call_filter
690 class IsolatedCallFixture : public TrackCounters {
692 IsolatedCallFixture() {
693 // We are calling grpc_channel_stack_builder_create() instead of
694 // grpc_channel_create() here, which means we're not getting the
695 // grpc_init() called by grpc_channel_create(), but we are getting
696 // the grpc_shutdown() run by grpc_channel_destroy(). So we need to
697 // call grpc_init() manually here to balance things out.
699 grpc_channel_stack_builder* builder = grpc_channel_stack_builder_create();
700 grpc_channel_stack_builder_set_name(builder, "phony");
701 grpc_channel_stack_builder_set_target(builder, "phony_target");
702 GPR_ASSERT(grpc_channel_stack_builder_append_filter(
703 builder, &isolated_call_filter::isolated_call_filter, nullptr,
706 grpc_core::ExecCtx exec_ctx;
707 channel_ = grpc_channel_create_with_builder(
708 builder, GRPC_CLIENT_CHANNEL, grpc_resource_user_create_unlimited(),
711 cq_ = grpc_completion_queue_create_for_next(nullptr);
714 void Finish(benchmark::State& state) override {
715 grpc_completion_queue_destroy(cq_);
716 grpc_channel_destroy(channel_);
717 TrackCounters::Finish(state);
720 grpc_channel* channel() const { return channel_; }
721 grpc_completion_queue* cq() const { return cq_; }
724 grpc_completion_queue* cq_;
725 grpc_channel* channel_;
728 static void BM_IsolatedCall_NoOp(benchmark::State& state) {
729 IsolatedCallFixture fixture;
730 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
731 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
733 for (auto _ : state) {
734 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
735 grpc_call_unref(grpc_channel_create_registered_call(
736 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
737 method_hdl, deadline, nullptr));
739 fixture.Finish(state);
741 BENCHMARK(BM_IsolatedCall_NoOp);
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",
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);
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 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
774 grpc_call* call = grpc_channel_create_registered_call(
775 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
776 method_hdl, deadline, nullptr);
777 grpc_call_start_batch(call, ops, 6, tag(1), nullptr);
778 grpc_completion_queue_next(fixture.cq(),
779 gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
780 grpc_call_unref(call);
782 fixture.Finish(state);
783 grpc_metadata_array_destroy(&recv_initial_metadata);
784 grpc_metadata_array_destroy(&recv_trailing_metadata);
785 grpc_byte_buffer_destroy(send_message);
787 BENCHMARK(BM_IsolatedCall_Unary);
789 static void BM_IsolatedCall_StreamingSend(benchmark::State& state) {
790 IsolatedCallFixture fixture;
791 gpr_timespec deadline = gpr_inf_future(GPR_CLOCK_MONOTONIC);
792 void* method_hdl = grpc_channel_register_call(fixture.channel(), "/foo/bar",
794 grpc_slice slice = grpc_slice_from_static_string("hello world");
795 grpc_byte_buffer* send_message = grpc_raw_byte_buffer_create(&slice, 1);
796 grpc_metadata_array recv_initial_metadata;
797 grpc_metadata_array_init(&recv_initial_metadata);
798 grpc_metadata_array recv_trailing_metadata;
799 grpc_metadata_array_init(&recv_trailing_metadata);
801 memset(ops, 0, sizeof(ops));
802 ops[0].op = GRPC_OP_SEND_INITIAL_METADATA;
803 ops[1].op = GRPC_OP_RECV_INITIAL_METADATA;
804 ops[1].data.recv_initial_metadata.recv_initial_metadata =
805 &recv_initial_metadata;
806 grpc_call* call = grpc_channel_create_registered_call(
807 fixture.channel(), nullptr, GRPC_PROPAGATE_DEFAULTS, fixture.cq(),
808 method_hdl, deadline, nullptr);
809 grpc_call_start_batch(call, ops, 2, tag(1), nullptr);
810 grpc_completion_queue_next(fixture.cq(), gpr_inf_future(GPR_CLOCK_MONOTONIC),
812 memset(ops, 0, sizeof(ops));
813 ops[0].op = GRPC_OP_SEND_MESSAGE;
814 ops[0].data.send_message.send_message = send_message;
815 for (auto _ : state) {
816 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
817 grpc_call_start_batch(call, ops, 1, tag(2), nullptr);
818 grpc_completion_queue_next(fixture.cq(),
819 gpr_inf_future(GPR_CLOCK_MONOTONIC), nullptr);
821 grpc_call_unref(call);
822 fixture.Finish(state);
823 grpc_metadata_array_destroy(&recv_initial_metadata);
824 grpc_metadata_array_destroy(&recv_trailing_metadata);
825 grpc_byte_buffer_destroy(send_message);
827 BENCHMARK(BM_IsolatedCall_StreamingSend);
829 // Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
830 // and others do not. This allows us to support both modes.
831 namespace benchmark {
832 void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); }
833 } // namespace benchmark
835 int main(int argc, char** argv) {
836 grpc::testing::TestEnvironment env(argc, argv);
837 LibraryInitializer libInit;
838 ::benchmark::Initialize(&argc, argv);
839 ::grpc::testing::InitTest(&argc, &argv, false);
840 benchmark::RunTheBenchmarksNamespaced();