• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2019 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 // Benchmark gRPC end2end in various configurations
20 
21 #ifndef GRPC_TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
22 #define GRPC_TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
23 
24 #include <benchmark/benchmark.h>
25 
26 #include <sstream>
27 
28 #include "absl/log/check.h"
29 #include "src/proto/grpc/testing/echo.grpc.pb.h"
30 #include "test/cpp/microbenchmarks/callback_test_service.h"
31 #include "test/cpp/microbenchmarks/fullstack_context_mutators.h"
32 #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
33 
34 namespace grpc {
35 namespace testing {
36 
37 //******************************************************************************
38 // BENCHMARKING KERNELS
39 //
40 
SendCallbackUnaryPingPong(benchmark::State * state,ClientContext * cli_ctx,EchoRequest * request,EchoResponse * response,EchoTestService::Stub * stub_,bool * done,std::mutex * mu,std::condition_variable * cv)41 inline void SendCallbackUnaryPingPong(
42     benchmark::State* state, ClientContext* cli_ctx, EchoRequest* request,
43     EchoResponse* response, EchoTestService::Stub* stub_, bool* done,
44     std::mutex* mu, std::condition_variable* cv) {
45   int response_msgs_size = state->range(1);
46   cli_ctx->AddMetadata(kServerMessageSize, std::to_string(response_msgs_size));
47   stub_->async()->Echo(
48       cli_ctx, request, response,
49       [state, cli_ctx, request, response, stub_, done, mu, cv](Status s) {
50         CHECK(s.ok());
51         if (state->KeepRunning()) {
52           cli_ctx->~ClientContext();
53           new (cli_ctx) ClientContext();
54           SendCallbackUnaryPingPong(state, cli_ctx, request, response, stub_,
55                                     done, mu, cv);
56         } else {
57           std::lock_guard<std::mutex> l(*mu);
58           *done = true;
59           cv->notify_one();
60         }
61       });
62 };
63 
64 template <class Fixture, class ClientContextMutator, class ServerContextMutator>
BM_CallbackUnaryPingPong(benchmark::State & state)65 static void BM_CallbackUnaryPingPong(benchmark::State& state) {
66   int request_msgs_size = state.range(0);
67   int response_msgs_size = state.range(1);
68   CallbackStreamingTestService service;
69   std::unique_ptr<Fixture> fixture(new Fixture(&service));
70   std::unique_ptr<EchoTestService::Stub> stub_(
71       EchoTestService::NewStub(fixture->channel()));
72   EchoRequest request;
73   EchoResponse response;
74   ClientContext cli_ctx;
75 
76   if (request_msgs_size > 0) {
77     request.set_message(std::string(request_msgs_size, 'a'));
78   } else {
79     request.set_message("");
80   }
81 
82   std::mutex mu;
83   std::condition_variable cv;
84   bool done = false;
85   if (state.KeepRunning()) {
86     SendCallbackUnaryPingPong(&state, &cli_ctx, &request, &response,
87                               stub_.get(), &done, &mu, &cv);
88   }
89   std::unique_lock<std::mutex> l(mu);
90   while (!done) {
91     cv.wait(l);
92   }
93   fixture.reset();
94   state.SetBytesProcessed((request_msgs_size * state.iterations()) +
95                           (response_msgs_size * state.iterations()));
96 }
97 
98 }  // namespace testing
99 }  // namespace grpc
100 
101 #endif  // GRPC_TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
102