Debian Codegraph @

test/cpp/microbenchmarks/fullstack_fixtures.h

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 #ifndef TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H
20 #define TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H
21
22 #include <grpc/support/atm.h>
23 #include <grpc/support/log.h>
24 #include <grpcpp/channel.h>
25 #include <grpcpp/create_channel.h>
26 #include <grpcpp/security/credentials.h>
27 #include <grpcpp/security/server_credentials.h>
28 #include <grpcpp/server.h>
29 #include <grpcpp/server_builder.h>
30
31 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/config/core_configuration.h"
34 #include "src/core/lib/iomgr/endpoint.h"
35 #include "src/core/lib/iomgr/endpoint_pair.h"
36 #include "src/core/lib/iomgr/exec_ctx.h"
37 #include "src/core/lib/iomgr/tcp_posix.h"
38 #include "src/core/lib/surface/channel.h"
39 #include "src/core/lib/surface/completion_queue.h"
40 #include "src/core/lib/surface/server.h"
41 #include "src/cpp/client/create_channel_internal.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 #include "test/cpp/microbenchmarks/helpers.h"
46
47 namespace grpc {
48 namespace testing {
49
50 class FixtureConfiguration {
51 public:
52 virtual ~FixtureConfiguration() {}
53 virtual void ApplyCommonChannelArguments(ChannelArguments* c) const {
54 c->SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, INT_MAX);
55 c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
56 c->SetResourceQuota(ResourceQuota());
57 }
58
59 virtual void ApplyCommonServerBuilderConfig(ServerBuilder* b) const {
60 b->SetMaxReceiveMessageSize(INT_MAX);
61 b->SetMaxSendMessageSize(INT_MAX);
62 }
63 };
64
65 class BaseFixture {
66 public:
67 virtual ~BaseFixture() = default;
68 };
69
70 class FullstackFixture : public BaseFixture {
71 public:
72 FullstackFixture(Service* service, const FixtureConfiguration& config,
73 const std::string& address) {
74 ServerBuilder b;
75 if (address.length() > 0) {
76 b.AddListeningPort(address, InsecureServerCredentials());
77 }
78 cq_ = b.AddCompletionQueue(true);
79 b.RegisterService(service);
80 config.ApplyCommonServerBuilderConfig(&b);
81 server_ = b.BuildAndStart();
82 ChannelArguments args;
83 config.ApplyCommonChannelArguments(&args);
84 if (address.length() > 0) {
85 channel_ = grpc::CreateCustomChannel(address,
86 InsecureChannelCredentials(), args);
87 } else {
88 channel_ = server_->InProcessChannel(args);
89 }
90 }
91
92 ~FullstackFixture() override {
93 server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));
94 cq_->Shutdown();
95 void* tag;
96 bool ok;
97 while (cq_->Next(&tag, &ok)) {
98 }
99 }
100
101 ServerCompletionQueue* cq() { return cq_.get(); }
102 std::shared_ptr<Channel> channel() { return channel_; }
103
104 private:
105 std::unique_ptr<Server> server_;
106 std::unique_ptr<ServerCompletionQueue> cq_;
107 std::shared_ptr<Channel> channel_;
108 };
109
110 class TCP : public FullstackFixture {
111 public:
112 explicit TCP(Service* service,
113 const FixtureConfiguration& fixture_configuration =
114 FixtureConfiguration())
115 : FullstackFixture(service, fixture_configuration, MakeAddress(&port_)) {}
116
117 ~TCP() override { grpc_recycle_unused_port(port_); }
118
119 private:
120 int port_;
121
122 static std::string MakeAddress(int* port) {
123 *port = grpc_pick_unused_port_or_die();
124 std::stringstream addr;
125 addr << "localhost:" << *port;
126 return addr.str();
127 }
128 };
129
130 class UDS : public FullstackFixture {
131 public:
132 explicit UDS(Service* service,
133 const FixtureConfiguration& fixture_configuration =
134 FixtureConfiguration())
135 : FullstackFixture(service, fixture_configuration, MakeAddress(&port_)) {}
136
137 ~UDS() override { grpc_recycle_unused_port(port_); }
138
139 private:
140 int port_;
141
142 static std::string MakeAddress(int* port) {
143 *port = grpc_pick_unused_port_or_die(); // just for a unique id - not a
144 // real port
145 std::stringstream addr;
146 addr << "unix:/tmp/bm_fullstack." << *port;
147 return addr.str();
148 }
149 };
150
151 class InProcess : public FullstackFixture {
152 public:
153 explicit InProcess(Service* service,
154 const FixtureConfiguration& fixture_configuration =
155 FixtureConfiguration())
156 : FullstackFixture(service, fixture_configuration, "") {}
157 ~InProcess() override {}
158 };
159
160 class EndpointPairFixture : public BaseFixture {
161 public:
162 EndpointPairFixture(Service* service, grpc_endpoint_pair endpoints,
163 const FixtureConfiguration& fixture_configuration)
164 : endpoint_pair_(endpoints) {
165 ServerBuilder b;
166 cq_ = b.AddCompletionQueue(true);
167 b.RegisterService(service);
168 fixture_configuration.ApplyCommonServerBuilderConfig(&b);
169 server_ = b.BuildAndStart();
170 grpc_core::ExecCtx exec_ctx;
171 /* add server endpoint to server_
172 * */
173 {
174 grpc_core::Server* core_server =
175 grpc_core::Server::FromC(server_->c_server());
176 grpc_core::ChannelArgs server_args = core_server->channel_args();
177 server_transport_ = grpc_create_chttp2_transport(
178 server_args, endpoints.server, false /* is_client */);
179 for (grpc_pollset* pollset : core_server->pollsets()) {
180 grpc_endpoint_add_to_pollset(endpoints.server, pollset);
181 }
182
183 GPR_ASSERT(GRPC_LOG_IF_ERROR(
184 "SetupTransport",
185 core_server->SetupTransport(server_transport_, nullptr, server_args,
186 nullptr)));
187 grpc_chttp2_transport_start_reading(server_transport_, nullptr, nullptr,
188 nullptr);
189 }
190
191 /* create channel */
192 {
193 grpc_core::ChannelArgs c_args;
194 {
195 ChannelArguments args;
196 args.SetString(GRPC_ARG_DEFAULT_AUTHORITY, "test.authority");
197 fixture_configuration.ApplyCommonChannelArguments(&args);
198 // precondition
199 grpc_channel_args tmp_args;
200 args.SetChannelArgs(&tmp_args);
201 c_args = grpc_core::CoreConfiguration::Get()
202 .channel_args_preconditioning()
203 .PreconditionChannelArgs(&tmp_args);
204 }
205 client_transport_ =
206 grpc_create_chttp2_transport(c_args, endpoints.client, true);
207 GPR_ASSERT(client_transport_);
208 grpc_channel* channel =
209 grpc_core::Channel::Create(
210 "target", c_args, GRPC_CLIENT_DIRECT_CHANNEL, client_transport_)
211 ->release()
212 ->c_ptr();
213 grpc_chttp2_transport_start_reading(client_transport_, nullptr, nullptr,
214 nullptr);
215
216 channel_ = grpc::CreateChannelInternal(
217 "", channel,
218 std::vector<std::unique_ptr<
219 experimental::ClientInterceptorFactoryInterface>>());
220 }
221 }
222
223 ~EndpointPairFixture() override {
224 server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));
225 cq_->Shutdown();
226 void* tag;
227 bool ok;
228 while (cq_->Next(&tag, &ok)) {
229 }
230 }
231
232 ServerCompletionQueue* cq() { return cq_.get(); }
233 std::shared_ptr<Channel> channel() { return channel_; }
234
235 protected:
236 grpc_endpoint_pair endpoint_pair_;
237 grpc_transport* client_transport_;
238 grpc_transport* server_transport_;
239
240 private:
241 std::unique_ptr<Server> server_;
242 std::unique_ptr<ServerCompletionQueue> cq_;
243 std::shared_ptr<Channel> channel_;
244 };
245
246 class SockPair : public EndpointPairFixture {
247 public:
248 explicit SockPair(Service* service,
249 const FixtureConfiguration& fixture_configuration =
250 FixtureConfiguration())
251 : EndpointPairFixture(service,
252 grpc_iomgr_create_endpoint_pair("test", nullptr),
253 fixture_configuration) {}
254 };
255
256 /* Use InProcessCHTTP2 instead. This class (with stats as an explicit parameter)
257 is here only to be able to initialize both the base class and stats_ with the
258 same stats instance without accessing the stats_ fields before the object is
259 properly initialized. */
260 class InProcessCHTTP2WithExplicitStats : public EndpointPairFixture {
261 public:
262 InProcessCHTTP2WithExplicitStats(
263 Service* service, grpc_passthru_endpoint_stats* stats,
264 const FixtureConfiguration& fixture_configuration)
265 : EndpointPairFixture(service, MakeEndpoints(stats),
266 fixture_configuration),
267 stats_(stats) {}
268
269 ~InProcessCHTTP2WithExplicitStats() override {
270 if (stats_ != nullptr) {
271 grpc_passthru_endpoint_stats_destroy(stats_);
272 }
273 }
274
275 private:
276 grpc_passthru_endpoint_stats* stats_;
277
278 static grpc_endpoint_pair MakeEndpoints(grpc_passthru_endpoint_stats* stats) {
279 grpc_endpoint_pair p;
280 grpc_passthru_endpoint_create(&p.client, &p.server, stats);
281 return p;
282 }
283 };
284
285 class InProcessCHTTP2 : public InProcessCHTTP2WithExplicitStats {
286 public:
287 explicit InProcessCHTTP2(Service* service,
288 const FixtureConfiguration& fixture_configuration =
289 FixtureConfiguration())
290 : InProcessCHTTP2WithExplicitStats(service,
291 grpc_passthru_endpoint_stats_create(),
292 fixture_configuration) {}
293 };
294
295 ////////////////////////////////////////////////////////////////////////////////
296 // Minimal stack fixtures
297
298 class MinStackConfiguration : public FixtureConfiguration {
299 void ApplyCommonChannelArguments(ChannelArguments* a) const override {
300 a->SetInt(GRPC_ARG_MINIMAL_STACK, 1);
301 FixtureConfiguration::ApplyCommonChannelArguments(a);
302 }
303
304 void ApplyCommonServerBuilderConfig(ServerBuilder* b) const override {
305 b->AddChannelArgument(GRPC_ARG_MINIMAL_STACK, 1);
306 FixtureConfiguration::ApplyCommonServerBuilderConfig(b);
307 }
308 };
309
310 template <class Base>
311 class MinStackize : public Base {
312 public:
313 explicit MinStackize(Service* service)
314 : Base(service, MinStackConfiguration()) {}
315 };
316
317 typedef MinStackize<TCP> MinTCP;
318 typedef MinStackize<UDS> MinUDS;
319 typedef MinStackize<InProcess> MinInProcess;
320 typedef MinStackize<SockPair> MinSockPair;
321 typedef MinStackize<InProcessCHTTP2> MinInProcessCHTTP2;
322
323 } // namespace testing
324 } // namespace grpc
325
326 #endif