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