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 GRPC_TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H
22 #define GRPC_TEST_CPP_MICROBENCHMARKS_FULLSTACK_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/fullstack_context_mutators.h"
31 #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
32
33 namespace grpc {
34 namespace testing {
35
36 //******************************************************************************
37 // BENCHMARKING KERNELS
38 //
39
tag(intptr_t x)40 static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
41
42 template <class Fixture, class ClientContextMutator, class ServerContextMutator>
BM_UnaryPingPong(benchmark::State & state)43 static void BM_UnaryPingPong(benchmark::State& state) {
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 {
57 ServerContext ctx;
58 EchoRequest recv_request;
59 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer;
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 recv_response.Clear();
78 ClientContext cli_ctx;
79 ClientContextMutator cli_ctx_mut(&cli_ctx);
80 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
81 stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
82 response_reader->Finish(&recv_response, &recv_status, tag(4));
83 void* t;
84 bool ok;
85 CHECK(fixture->cq()->Next(&t, &ok));
86 CHECK(ok);
87 CHECK(t == tag(0) || t == tag(1));
88 intptr_t slot = reinterpret_cast<intptr_t>(t);
89 ServerEnv* senv = server_env[slot];
90 ServerContextMutator svr_ctx_mut(&senv->ctx);
91 senv->response_writer.Finish(send_response, Status::OK, tag(3));
92 for (int i = (1 << 3) | (1 << 4); i != 0;) {
93 CHECK(fixture->cq()->Next(&t, &ok));
94 CHECK(ok);
95 int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
96 CHECK(i & (1 << tagnum));
97 i -= 1 << tagnum;
98 }
99 CHECK(recv_status.ok());
100
101 senv->~ServerEnv();
102 senv = new (senv) ServerEnv();
103 service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
104 fixture->cq(), fixture->cq(), tag(slot));
105 }
106 stub.reset();
107 fixture.reset();
108 server_env[0]->~ServerEnv();
109 server_env[1]->~ServerEnv();
110 state.SetBytesProcessed((state.range(0) * state.iterations()) +
111 (state.range(1) * state.iterations()));
112 }
113 } // namespace testing
114 } // namespace grpc
115
116 #endif // GRPC_TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H
117