fullstack_unary_ping_pong.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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_FULLSTACK_UNARY_PING_PONG_H
22 #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_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"
32 
33 namespace grpc {
34 namespace testing {
35 
36 /*******************************************************************************
37  * BENCHMARKING KERNELS
38  */
39 
40 static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
41 
42 template <class Fixture, class ClientContextMutator, class ServerContextMutator>
44  EchoTestService::AsyncService service;
45  std::unique_ptr<Fixture> fixture(new Fixture(&service));
46  EchoRequest send_request;
47  EchoResponse send_response;
48  EchoResponse recv_response;
49  if (state.range(0) > 0) {
50  send_request.set_message(std::string(state.range(0), 'a'));
51  }
52  if (state.range(1) > 0) {
53  send_response.set_message(std::string(state.range(1), 'a'));
54  }
55  Status recv_status;
56  struct ServerEnv {
58  EchoRequest recv_request;
60  ServerEnv() : response_writer(&ctx) {}
61  };
62  uint8_t server_env_buffer[2 * sizeof(ServerEnv)];
63  ServerEnv* server_env[2] = {
64  reinterpret_cast<ServerEnv*>(server_env_buffer),
65  reinterpret_cast<ServerEnv*>(server_env_buffer + sizeof(ServerEnv))};
66  new (server_env[0]) ServerEnv;
67  new (server_env[1]) ServerEnv;
68  service.RequestEcho(&server_env[0]->ctx, &server_env[0]->recv_request,
69  &server_env[0]->response_writer, fixture->cq(),
70  fixture->cq(), tag(0));
71  service.RequestEcho(&server_env[1]->ctx, &server_env[1]->recv_request,
72  &server_env[1]->response_writer, fixture->cq(),
73  fixture->cq(), tag(1));
74  std::unique_ptr<EchoTestService::Stub> stub(
75  EchoTestService::NewStub(fixture->channel()));
76  for (auto _ : state) {
77  GPR_TIMER_SCOPE("BenchmarkCycle", 0);
78  recv_response.Clear();
79  ClientContext cli_ctx;
80  ClientContextMutator cli_ctx_mut(&cli_ctx);
81  std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
82  stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
83  response_reader->Finish(&recv_response, &recv_status, tag(4));
84  void* t;
85  bool ok;
86  GPR_ASSERT(fixture->cq()->Next(&t, &ok));
87  GPR_ASSERT(ok);
88  GPR_ASSERT(t == tag(0) || t == tag(1));
89  intptr_t slot = reinterpret_cast<intptr_t>(t);
90  ServerEnv* senv = server_env[slot];
91  ServerContextMutator svr_ctx_mut(&senv->ctx);
92  senv->response_writer.Finish(send_response, Status::OK, tag(3));
93  for (int i = (1 << 3) | (1 << 4); i != 0;) {
94  GPR_ASSERT(fixture->cq()->Next(&t, &ok));
95  GPR_ASSERT(ok);
96  int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
97  GPR_ASSERT(i & (1 << tagnum));
98  i -= 1 << tagnum;
99  }
100  GPR_ASSERT(recv_status.ok());
101 
102  senv->~ServerEnv();
103  senv = new (senv) ServerEnv();
104  service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
105  fixture->cq(), fixture->cq(), tag(slot));
106  }
107  fixture->Finish(state);
108  fixture.reset();
109  server_env[0]->~ServerEnv();
110  server_env[1]->~ServerEnv();
111  state.SetBytesProcessed(state.range(0) * state.iterations() +
112  state.range(1) * state.iterations());
113 }
114 } // namespace testing
115 } // namespace grpc
116 
117 #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::ServerContext
Definition: grpcpp/impl/codegen/server_context.h:566
ctx
Definition: benchmark-async.c:30
timers.h
grpc
Definition: grpcpp/alarm.h:33
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: include/grpcpp/impl/codegen/status.h:126
GPR_TIMER_SCOPE
#define GPR_TIMER_SCOPE(tag, important)
Definition: src/core/lib/profiling/timers.h:43
send_request
Definition: ares_private.h:147
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
fullstack_context_mutators.h
async_greeter_client.stub
stub
Definition: hellostreamingworld/async_greeter_client.py:26
fullstack_fixtures.h
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
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
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc::testing::tag
static void * tag(intptr_t t)
Definition: h2_ssl_cert_test.cc:263
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
grpc::testing::BM_UnaryPingPong
static void BM_UnaryPingPong(benchmark::State &state)
Definition: fullstack_unary_ping_pong.h:43
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
ok
bool ok
Definition: async_end2end_test.cc:197
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
grpc::ServerAsyncResponseWriter
Definition: grpcpp/impl/codegen/async_unary_call.h:295
service
__attribute__((deprecated("Please use GRPCProtoMethod."))) @interface ProtoMethod NSString * service
Definition: ProtoMethod.h:25
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22