blob: dc2970105991cb8d682fb77331656333e1d818e6 [file] [log] [blame]
Craig Tiller627a5982017-03-03 09:47:21 -08001/*
2 *
3 * Copyright 2017, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#ifndef TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H
35#define TEST_CPP_MICROBENCHMARKS_FULLSTACK_FIXTURES_H
36
37#include <grpc++/channel.h>
38#include <grpc++/create_channel.h>
39#include <grpc++/security/credentials.h>
40#include <grpc++/security/server_credentials.h>
41#include <grpc++/server.h>
42#include <grpc++/server_builder.h>
43#include <grpc/support/log.h>
44
45extern "C" {
46#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
47#include "src/core/lib/channel/channel_args.h"
48#include "src/core/lib/iomgr/endpoint.h"
49#include "src/core/lib/iomgr/endpoint_pair.h"
50#include "src/core/lib/iomgr/exec_ctx.h"
51#include "src/core/lib/iomgr/tcp_posix.h"
52#include "src/core/lib/surface/channel.h"
53#include "src/core/lib/surface/completion_queue.h"
54#include "src/core/lib/surface/server.h"
55#include "test/core/util/passthru_endpoint.h"
56#include "test/core/util/port.h"
57}
58
59#include "test/cpp/microbenchmarks/helpers.h"
60
61namespace grpc {
62namespace testing {
63
64static void ApplyCommonServerBuilderConfig(ServerBuilder* b) {
65 b->SetMaxReceiveMessageSize(INT_MAX);
66 b->SetMaxSendMessageSize(INT_MAX);
67}
68
69static void ApplyCommonChannelArguments(ChannelArguments* c) {
70 c->SetInt(GRPC_ARG_MAX_RECEIVE_MESSAGE_LENGTH, INT_MAX);
71 c->SetInt(GRPC_ARG_MAX_SEND_MESSAGE_LENGTH, INT_MAX);
72}
73
74class BaseFixture : public TrackCounters {};
75
76class FullstackFixture : public BaseFixture {
77 public:
78 FullstackFixture(Service* service, const grpc::string& address) {
79 ServerBuilder b;
80 b.AddListeningPort(address, InsecureServerCredentials());
81 cq_ = b.AddCompletionQueue(true);
82 b.RegisterService(service);
83 ApplyCommonServerBuilderConfig(&b);
84 server_ = b.BuildAndStart();
85 ChannelArguments args;
86 ApplyCommonChannelArguments(&args);
87 channel_ = CreateCustomChannel(address, InsecureChannelCredentials(), args);
88 }
89
90 virtual ~FullstackFixture() {
91 server_->Shutdown();
92 cq_->Shutdown();
93 void* tag;
94 bool ok;
95 while (cq_->Next(&tag, &ok)) {
96 }
97 }
98
99 ServerCompletionQueue* cq() { return cq_.get(); }
100 std::shared_ptr<Channel> channel() { return channel_; }
101
102 private:
103 std::unique_ptr<Server> server_;
104 std::unique_ptr<ServerCompletionQueue> cq_;
105 std::shared_ptr<Channel> channel_;
106};
107
108class TCP : public FullstackFixture {
109 public:
110 TCP(Service* service) : FullstackFixture(service, MakeAddress()) {}
111
112 private:
113 static grpc::string MakeAddress() {
114 int port = grpc_pick_unused_port_or_die();
115 std::stringstream addr;
116 addr << "localhost:" << port;
117 return addr.str();
118 }
119};
120
121class UDS : public FullstackFixture {
122 public:
123 UDS(Service* service) : FullstackFixture(service, MakeAddress()) {}
124
125 private:
126 static grpc::string MakeAddress() {
127 int port = grpc_pick_unused_port_or_die(); // just for a unique id - not a
128 // real port
129 std::stringstream addr;
130 addr << "unix:/tmp/bm_fullstack." << port;
131 return addr.str();
132 }
133};
134
135class EndpointPairFixture : public BaseFixture {
136 public:
Craig Tillercc928d62017-03-03 11:06:40 -0800137 EndpointPairFixture(Service* service, grpc_endpoint_pair endpoints)
138 : endpoint_pair_(endpoints) {
Craig Tiller627a5982017-03-03 09:47:21 -0800139 ServerBuilder b;
140 cq_ = b.AddCompletionQueue(true);
141 b.RegisterService(service);
142 ApplyCommonServerBuilderConfig(&b);
143 server_ = b.BuildAndStart();
144
145 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
146
Craig Tillercc928d62017-03-03 11:06:40 -0800147 /* add server endpoint to server_
148 * */
Craig Tiller627a5982017-03-03 09:47:21 -0800149 {
150 const grpc_channel_args* server_args =
151 grpc_server_get_channel_args(server_->c_server());
Craig Tillercc928d62017-03-03 11:06:40 -0800152 server_transport_ = grpc_create_chttp2_transport(
Craig Tiller627a5982017-03-03 09:47:21 -0800153 &exec_ctx, server_args, endpoints.server, 0 /* is_client */);
154
155 grpc_pollset** pollsets;
156 size_t num_pollsets = 0;
157 grpc_server_get_pollsets(server_->c_server(), &pollsets, &num_pollsets);
158
159 for (size_t i = 0; i < num_pollsets; i++) {
160 grpc_endpoint_add_to_pollset(&exec_ctx, endpoints.server, pollsets[i]);
161 }
162
Craig Tillercc928d62017-03-03 11:06:40 -0800163 grpc_server_setup_transport(&exec_ctx, server_->c_server(),
164 server_transport_, NULL, server_args);
165 grpc_chttp2_transport_start_reading(&exec_ctx, server_transport_, NULL);
Craig Tiller627a5982017-03-03 09:47:21 -0800166 }
167
168 /* create channel */
169 {
170 ChannelArguments args;
171 args.SetString(GRPC_ARG_DEFAULT_AUTHORITY, "test.authority");
172 ApplyCommonChannelArguments(&args);
173
174 grpc_channel_args c_args = args.c_channel_args();
Craig Tillercc928d62017-03-03 11:06:40 -0800175 client_transport_ =
Craig Tiller627a5982017-03-03 09:47:21 -0800176 grpc_create_chttp2_transport(&exec_ctx, &c_args, endpoints.client, 1);
Craig Tillercc928d62017-03-03 11:06:40 -0800177 GPR_ASSERT(client_transport_);
178 grpc_channel* channel =
179 grpc_channel_create(&exec_ctx, "target", &c_args,
180 GRPC_CLIENT_DIRECT_CHANNEL, client_transport_);
181 grpc_chttp2_transport_start_reading(&exec_ctx, client_transport_, NULL);
Craig Tiller627a5982017-03-03 09:47:21 -0800182
183 channel_ = CreateChannelInternal("", channel);
184 }
185
186 grpc_exec_ctx_finish(&exec_ctx);
187 }
188
189 virtual ~EndpointPairFixture() {
190 server_->Shutdown();
191 cq_->Shutdown();
192 void* tag;
193 bool ok;
194 while (cq_->Next(&tag, &ok)) {
195 }
196 }
197
198 ServerCompletionQueue* cq() { return cq_.get(); }
199 std::shared_ptr<Channel> channel() { return channel_; }
200
Craig Tillercc928d62017-03-03 11:06:40 -0800201 protected:
202 grpc_endpoint_pair endpoint_pair_;
203 grpc_transport* client_transport_;
204 grpc_transport* server_transport_;
205
Craig Tiller627a5982017-03-03 09:47:21 -0800206 private:
207 std::unique_ptr<Server> server_;
208 std::unique_ptr<ServerCompletionQueue> cq_;
209 std::shared_ptr<Channel> channel_;
210};
211
212class SockPair : public EndpointPairFixture {
213 public:
214 SockPair(Service* service)
215 : EndpointPairFixture(service, grpc_iomgr_create_endpoint_pair(
216 "test", Library::get().rq(), 8192)) {}
217};
218
219class InProcessCHTTP2 : public EndpointPairFixture {
220 public:
221 InProcessCHTTP2(Service* service)
222 : EndpointPairFixture(service, MakeEndpoints()) {}
223
224 void AddToLabel(std::ostream& out, benchmark::State& state) {
225 EndpointPairFixture::AddToLabel(out, state);
226 out << " writes/iter:"
227 << ((double)stats_.num_writes / (double)state.iterations());
228 }
229
230 private:
231 grpc_passthru_endpoint_stats stats_;
232
233 grpc_endpoint_pair MakeEndpoints() {
234 grpc_endpoint_pair p;
235 grpc_passthru_endpoint_create(&p.client, &p.server, Library::get().rq(),
236 &stats_);
237 return p;
238 }
239};
240
241} // namespace testing
242} // namespace grpc
243
244#endif