Debian Codegraph @

test/cpp/performance/writes_per_rpc_test.cc

raw
1 /*
2 *
3 * Copyright 2017 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 #include <gtest/gtest.h>
20
21 #include <grpc/support/log.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/create_channel.h>
24 #include <grpcpp/impl/grpc_library.h>
25 #include <grpcpp/security/credentials.h>
26 #include <grpcpp/security/server_credentials.h>
27 #include <grpcpp/server.h>
28 #include <grpcpp/server_builder.h>
29
30 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
31 #include "src/core/lib/channel/channel_args.h"
32 #include "src/core/lib/config/core_configuration.h"
33 #include "src/core/lib/iomgr/endpoint.h"
34 #include "src/core/lib/iomgr/endpoint_pair.h"
35 #include "src/core/lib/iomgr/exec_ctx.h"
36 #include "src/core/lib/iomgr/tcp_posix.h"
37 #include "src/core/lib/surface/channel.h"
38 #include "src/core/lib/surface/completion_queue.h"
39 #include "src/core/lib/surface/server.h"
40 #include "src/cpp/client/create_channel_internal.h"
41 #include "src/proto/grpc/testing/echo.grpc.pb.h"
42 #include "test/core/util/passthru_endpoint.h"
43 #include "test/core/util/port.h"
44 #include "test/core/util/test_config.h"
45
46 namespace grpc {
47 namespace testing {
48
49 static void* tag(intptr_t x) { return reinterpret_cast<void*>(x); }
50
51 static void ApplyCommonServerBuilderConfig(ServerBuilder* b) {
52 b->SetMaxReceiveMessageSize(INT_MAX);
53 b->SetMaxSendMessageSize(INT_MAX);
54 }
55
56 static void ApplyCommonChannelArguments(grpc_core::ChannelArgs* c) {
57 *c = c->Set(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, INT_MAX)
58 .Set(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
59 }
60
61 class EndpointPairFixture {
62 public:
63 EndpointPairFixture(Service* service, grpc_endpoint_pair endpoints) {
64 ServerBuilder b;
65 cq_ = b.AddCompletionQueue(true);
66 b.RegisterService(service);
67 ApplyCommonServerBuilderConfig(&b);
68 server_ = b.BuildAndStart();
69
70 grpc_core::ExecCtx exec_ctx;
71
72 /* add server endpoint to server_ */
73 {
74 grpc_core::Server* core_server =
75 grpc_core::Server::FromC(server_->c_server());
76 grpc_transport* transport = grpc_create_chttp2_transport(
77 core_server->channel_args(), endpoints.server, false /* is_client */);
78 for (grpc_pollset* pollset : core_server->pollsets()) {
79 grpc_endpoint_add_to_pollset(endpoints.server, pollset);
80 }
81
82 GPR_ASSERT(GRPC_LOG_IF_ERROR(
83 "SetupTransport",
84 core_server->SetupTransport(transport, nullptr,
85 core_server->channel_args(), nullptr)));
86 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
87 }
88
89 /* create channel */
90 {
91 grpc_core::ChannelArgs args =
92 grpc_core::CoreConfiguration::Get()
93 .channel_args_preconditioning()
94 .PreconditionChannelArgs(nullptr)
95 .Set(GRPC_ARG_DEFAULT_AUTHORITY, "test.authority");
96 ApplyCommonChannelArguments(&args);
97
98 grpc_transport* transport =
99 grpc_create_chttp2_transport(args, endpoints.client, true);
100 GPR_ASSERT(transport);
101 grpc_channel* channel =
102 grpc_core::Channel::Create("target", args, GRPC_CLIENT_DIRECT_CHANNEL,
103 transport)
104 ->release()
105 ->c_ptr();
106 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
107
108 channel_ = grpc::CreateChannelInternal(
109 "", channel,
110 std::vector<std::unique_ptr<
111 experimental::ClientInterceptorFactoryInterface>>());
112 }
113 }
114
115 virtual ~EndpointPairFixture() {
116 server_->Shutdown();
117 cq_->Shutdown();
118 void* tag;
119 bool ok;
120 while (cq_->Next(&tag, &ok)) {
121 }
122 }
123
124 ServerCompletionQueue* cq() { return cq_.get(); }
125 std::shared_ptr<Channel> channel() { return channel_; }
126
127 private:
128 std::unique_ptr<Server> server_;
129 std::unique_ptr<ServerCompletionQueue> cq_;
130 std::shared_ptr<Channel> channel_;
131 };
132
133 class InProcessCHTTP2 : public EndpointPairFixture {
134 public:
135 InProcessCHTTP2(Service* service, grpc_passthru_endpoint_stats* stats)
136 : EndpointPairFixture(service, MakeEndpoints(stats)), stats_(stats) {}
137
138 ~InProcessCHTTP2() override {
139 if (stats_ != nullptr) {
140 grpc_passthru_endpoint_stats_destroy(stats_);
141 }
142 }
143
144 int writes_performed() const { return stats_->num_writes; }
145
146 private:
147 grpc_passthru_endpoint_stats* stats_;
148
149 static grpc_endpoint_pair MakeEndpoints(grpc_passthru_endpoint_stats* stats) {
150 grpc_endpoint_pair p;
151 grpc_passthru_endpoint_create(&p.client, &p.server, stats);
152 return p;
153 }
154 };
155
156 static double UnaryPingPong(int request_size, int response_size) {
157 const int kIterations = 10000;
158
159 EchoTestService::AsyncService service;
160 std::unique_ptr<InProcessCHTTP2> fixture(
161 new InProcessCHTTP2(&service, grpc_passthru_endpoint_stats_create()));
162 EchoRequest send_request;
163 EchoResponse send_response;
164 EchoResponse recv_response;
165 if (request_size > 0) {
166 send_request.set_message(std::string(request_size, 'a'));
167 }
168 if (response_size > 0) {
169 send_response.set_message(std::string(response_size, 'a'));
170 }
171 Status recv_status;
172 struct ServerEnv {
173 ServerContext ctx;
174 EchoRequest recv_request;
175 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer;
176 ServerEnv() : response_writer(&ctx) {}
177 };
178 uint8_t server_env_buffer[2 * sizeof(ServerEnv)];
179 ServerEnv* server_env[2] = {
180 reinterpret_cast<ServerEnv*>(server_env_buffer),
181 reinterpret_cast<ServerEnv*>(server_env_buffer + sizeof(ServerEnv))};
182 new (server_env[0]) ServerEnv;
183 new (server_env[1]) ServerEnv;
184 service.RequestEcho(&server_env[0]->ctx, &server_env[0]->recv_request,
185 &server_env[0]->response_writer, fixture->cq(),
186 fixture->cq(), tag(0));
187 service.RequestEcho(&server_env[1]->ctx, &server_env[1]->recv_request,
188 &server_env[1]->response_writer, fixture->cq(),
189 fixture->cq(), tag(1));
190 std::unique_ptr<EchoTestService::Stub> stub(
191 EchoTestService::NewStub(fixture->channel()));
192 for (int iteration = 0; iteration < kIterations; iteration++) {
193 recv_response.Clear();
194 ClientContext cli_ctx;
195 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
196 stub->AsyncEcho(&cli_ctx, send_request, fixture->cq()));
197 void* t;
198 bool ok;
199 response_reader->Finish(&recv_response, &recv_status, tag(4));
200 GPR_ASSERT(fixture->cq()->Next(&t, &ok));
201 GPR_ASSERT(ok);
202 GPR_ASSERT(t == tag(0) || t == tag(1));
203 intptr_t slot = reinterpret_cast<intptr_t>(t);
204 ServerEnv* senv = server_env[slot];
205 senv->response_writer.Finish(send_response, Status::OK, tag(3));
206 for (int i = (1 << 3) | (1 << 4); i != 0;) {
207 GPR_ASSERT(fixture->cq()->Next(&t, &ok));
208 GPR_ASSERT(ok);
209 int tagnum = static_cast<int>(reinterpret_cast<intptr_t>(t));
210 GPR_ASSERT(i & (1 << tagnum));
211 i -= 1 << tagnum;
212 }
213 GPR_ASSERT(recv_status.ok());
214
215 senv->~ServerEnv();
216 senv = new (senv) ServerEnv();
217 service.RequestEcho(&senv->ctx, &senv->recv_request, &senv->response_writer,
218 fixture->cq(), fixture->cq(), tag(slot));
219 }
220
221 double writes_per_iteration =
222 static_cast<double>(fixture->writes_performed()) /
223 static_cast<double>(kIterations);
224
225 fixture.reset();
226 server_env[0]->~ServerEnv();
227 server_env[1]->~ServerEnv();
228
229 return writes_per_iteration;
230 }
231
232 TEST(WritesPerRpcTest, UnaryPingPong) {
233 EXPECT_LT(UnaryPingPong(0, 0), 2.05);
234 EXPECT_LT(UnaryPingPong(1, 0), 2.05);
235 EXPECT_LT(UnaryPingPong(0, 1), 2.05);
236 EXPECT_LT(UnaryPingPong(4096, 0), 2.5);
237 EXPECT_LT(UnaryPingPong(0, 4096), 2.5);
238 }
239
240 } // namespace testing
241 } // namespace grpc
242
243 int main(int argc, char** argv) {
244 ::testing::InitGoogleTest(&argc, argv);
245 grpc::testing::TestEnvironment env(&argc, argv);
246 grpc_init();
247 int ret = RUN_ALL_TESTS();
248 grpc_shutdown();
249 return ret;
250 }