callback_unary_ping_pong.h
Go to the documentation of this file.
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 TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
22 #define TEST_CPP_MICROBENCHMARKS_CALLBACK_UNARY_PING_PONG_H
23 
24 #include <sstream>
25 
26 #include <benchmark/benchmark.h>
27 
29 #include "src/proto/grpc/testing/echo.grpc.pb.h"
33 
34 namespace grpc {
35 namespace testing {
36 
37 /*******************************************************************************
38  * BENCHMARKING KERNELS
39  */
40 
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  GPR_ASSERT(s.ok());
51  if (state->KeepRunning()) {
52  cli_ctx->~ClientContext();
53  new (cli_ctx) ClientContext();
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>
66  int request_msgs_size = state.range(0);
67  int response_msgs_size = state.range(1);
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  GPR_TIMER_SCOPE("BenchmarkCycle", 0);
88  stub_.get(), &done, &mu, &cv);
89  }
90  std::unique_lock<std::mutex> l(mu);
91  while (!done) {
92  cv.wait(l);
93  }
94  fixture->Finish(state);
95  fixture.reset();
96  state.SetBytesProcessed(request_msgs_size * state.iterations() +
97  response_msgs_size * state.iterations());
98 }
99 
100 } // namespace testing
101 } // namespace grpc
102 
103 #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H
Fixture
Definition: bm_call_create.cc:359
testing
Definition: aws_request_signer_test.cc:25
grpc::testing::CallbackStreamingTestService
Definition: callback_test_service.h:37
stub_
std::unique_ptr< grpc::testing::EchoTestService::Stub > stub_
Definition: client_channel_stress_test.cc:331
timers.h
grpc
Definition: grpcpp/alarm.h:33
mutex
static uv_mutex_t mutex
Definition: threadpool.c:34
GPR_TIMER_SCOPE
#define GPR_TIMER_SCOPE(tag, important)
Definition: src/core/lib/profiling/timers.h:43
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
fullstack_context_mutators.h
fullstack_fixtures.h
grpc::testing::BM_CallbackUnaryPingPong
static void BM_CallbackUnaryPingPong(benchmark::State &state)
Definition: callback_unary_ping_pong.h:65
grpc::ClientContext::AddMetadata
void AddMetadata(const std::string &meta_key, const std::string &meta_value)
Definition: client_context.cc:121
grpc::ClientContext::~ClientContext
~ClientContext()
Definition: client_context.cc:79
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
fixture
static const char fixture[]
Definition: test-fs-copyfile.c:36
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
grpc::testing::kServerMessageSize
const char *const kServerMessageSize
Definition: callback_test_service.h:35
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
grpc::testing::cv
static gpr_cv cv
Definition: bm_cq.cc:163
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
grpc::testing::SendCallbackUnaryPingPong
void SendCallbackUnaryPingPong(benchmark::State *state, ClientContext *cli_ctx, EchoRequest *request, EchoResponse *response, EchoTestService::Stub *stub_, bool *done, std::mutex *mu, std::condition_variable *cv)
Definition: callback_unary_ping_pong.h:41
callback_test_service.h
to_string
static bool to_string(zval *from)
Definition: protobuf/php/ext/google/protobuf/convert.c:333
grpc::testing::mu
static gpr_mu mu
Definition: bm_cq.cc:162


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:51