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 <benchmark/benchmark.h>
25 #include <sstream>
26 #include "src/core/lib/profiling/timers.h"
27 #include "src/proto/grpc/testing/echo.grpc.pb.h"
28 #include "test/cpp/microbenchmarks/fullstack_context_mutators.h"
29 #include "test/cpp/microbenchmarks/fullstack_fixtures.h"
30
31 namespace grpc {
32 namespace testing {
33
34 /*******************************************************************************
35 * BENCHMARKING KERNELS
36 */
37
tag(intptr_t x)38 static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
39
40 template <class Fixture, class ClientContextMutator, class ServerContextMutator>
BM_UnaryPingPong(benchmark::State & state)41 static void BM_UnaryPingPong(benchmark::State& state) {
42 EchoTestService::AsyncService service;
43 std::unique_ptr<Fixture> fixture(new Fixture(&service));
44 EchoRequest send_request;
45 EchoResponse send_response;
46 EchoResponse recv_response;
47 if (state.range(0) > 0) {
48 send_request.set_message(std::string(state.range(0), 'a'));
49 }
50 if (state.range(1) > 0) {
51 send_response.set_message(std::string(state.range(1), 'a'));
52 }
53 Status recv_status;
54 struct ServerEnv {
55 ServerContext ctx;
56 EchoRequest recv_request;
57 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer;
58 ServerEnv() : response_writer(&ctx) {}
59 };
60 uint8_t server_env_buffer[2 * sizeof(ServerEnv)];
61 ServerEnv* server_env[2] = {
62 reinterpret_cast<ServerEnv*>(server_env_buffer),
63 reinterpret_cast<ServerEnv*>(server_env_buffer + sizeof(ServerEnv))};
64 new (server_env[0]) ServerEnv;
65 new (server_env[1]) ServerEnv;
66 service.RequestEcho(&server_env[0]->ctx, &server_env[0]->recv_request,
67 &server_env[0]->response_writer, fixture->cq(),
68 fixture->cq(), tag(0));
69 service.RequestEcho(&server_env[1]->ctx, &server_env[1]->recv_request,
70 &server_env[1]->response_writer, fixture->cq(),
71 fixture->cq(), tag(1));
72 std::unique_ptr<EchoTestService::Stub> stub(
73 EchoTestService::NewStub(fixture->channel()));
74 while (state.KeepRunning()) {
75 GPR_TIMER_SCOPE("BenchmarkCycle", 0);
76 recv_response.Clear();
77 ClientContext cli_ctx;
78 ClientContextMutator cli_ctx_mut(&cli_ctx);
79 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
80 stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
81 response_reader->Finish(&recv_response, &recv_status, tag(4));
82 void* t;
83 bool ok;
84 GPR_ASSERT(fixture->cq()->Next(&t, &ok));
85 GPR_ASSERT(ok);
86 GPR_ASSERT(t == tag(0) || t == tag(1));
87 intptr_t slot = reinterpret_cast<intptr_t>(t);
88 ServerEnv* senv = server_env[slot];
89 ServerContextMutator svr_ctx_mut(&senv->ctx);
90 senv->response_writer.Finish(send_response, Status::OK, tag(3));
91 for (int i = (1 << 3) | (1 << 4); i != 0;) {
92 GPR_ASSERT(fixture->cq()->Next(&t, &ok));
93 GPR_ASSERT(ok);
94 int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
95 GPR_ASSERT(i & (1 << tagnum));
96 i -= 1 << tagnum;
97 }
98 GPR_ASSERT(recv_status.ok());
99
100 senv->~ServerEnv();
101 senv = new (senv) ServerEnv();
102 service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
103 fixture->cq(), fixture->cq(), tag(slot));
104 }
105 fixture->Finish(state);
106 fixture.reset();
107 server_env[0]->~ServerEnv();
108 server_env[1]->~ServerEnv();
109 state.SetBytesProcessed(state.range(0) * state.iterations() +
110 state.range(1) * state.iterations());
111 }
112 } // namespace testing
113 } // namespace grpc
114
115 #endif // TEST_CPP_MICROBENCHMARKS_FULLSTACK_UNARY_PING_PONG_H
116