blob: 4312f597b29cbeedd02bd7ce710c15221e36a8b0 [file] [log] [blame]
vjpaidea740f2015-02-26 16:35:35 -08001/*
2 *
3 * Copyright 2015, 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#include <forward_list>
35#include <functional>
36#include <sys/time.h>
37#include <sys/resource.h>
38#include <sys/signal.h>
39#include <thread>
40
41#include <gflags/gflags.h>
42#include <grpc/support/alloc.h>
43#include <grpc/support/host_port.h>
44#include <grpc++/async_unary_call.h>
45#include <grpc++/config.h>
46#include <grpc++/server.h>
47#include <grpc++/server_builder.h>
48#include <grpc++/server_context.h>
Craig Tiller61691f92015-03-02 08:51:52 -080049#include <grpc++/server_credentials.h>
vjpaidea740f2015-02-26 16:35:35 -080050#include <grpc++/status.h>
vjpai46f65232015-03-23 10:10:27 -070051#include <grpc++/stream.h>
vjpaidea740f2015-02-26 16:35:35 -080052#include <gtest/gtest.h>
53#include "src/cpp/server/thread_pool.h"
54#include "test/core/util/grpc_profiler.h"
55#include "test/cpp/qps/qpstest.pb.h"
Craig Tillerd6479d62015-03-04 12:50:11 -080056#include "test/cpp/qps/server.h"
vjpaidea740f2015-02-26 16:35:35 -080057
58#include <grpc/grpc.h>
59#include <grpc/support/log.h>
60
Craig Tillerd6479d62015-03-04 12:50:11 -080061namespace grpc {
Craig Tillera182bf12015-03-04 13:54:39 -080062namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080063
Craig Tillerd6479d62015-03-04 12:50:11 -080064class AsyncQpsServerTest : public Server {
vjpaidea740f2015-02-26 16:35:35 -080065 public:
Craig Tillera182bf12015-03-04 13:54:39 -080066 AsyncQpsServerTest(const ServerConfig &config, int port)
67 : srv_cq_(), async_service_(&srv_cq_), server_(nullptr) {
vjpaidea740f2015-02-26 16:35:35 -080068 char *server_address = NULL;
Craig Tillerd6479d62015-03-04 12:50:11 -080069 gpr_join_host_port(&server_address, "::", port);
vjpaidea740f2015-02-26 16:35:35 -080070
71 ServerBuilder builder;
Craig Tiller61691f92015-03-02 08:51:52 -080072 builder.AddPort(server_address, InsecureServerCredentials());
Craig Tillerd6479d62015-03-04 12:50:11 -080073 gpr_free(server_address);
vjpaidea740f2015-02-26 16:35:35 -080074
75 builder.RegisterAsyncService(&async_service_);
76
77 server_ = builder.BuildAndStart();
vjpaidea740f2015-02-26 16:35:35 -080078
79 using namespace std::placeholders;
80 request_unary_ = std::bind(&TestService::AsyncService::RequestUnaryCall,
81 &async_service_, _1, _2, _3, &srv_cq_, _4);
vjpai46f65232015-03-23 10:10:27 -070082 request_streaming_ =
83 std::bind(&TestService::AsyncService::RequestStreamingCall,
84 &async_service_, _1, _2, &srv_cq_, _3);
vjpaidea740f2015-02-26 16:35:35 -080085 for (int i = 0; i < 100; i++) {
Vijay Pai64ac47f2015-02-26 17:59:51 -080086 contexts_.push_front(
87 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
vjpai46f65232015-03-23 10:10:27 -070088 request_unary_, ProcessRPC));
89 contexts_.push_front(
90 new ServerRpcContextStreamingImpl<SimpleRequest, SimpleResponse>(
91 request_streaming_, ProcessRPC));
vjpaidea740f2015-02-26 16:35:35 -080092 }
Craig Tillerd6479d62015-03-04 12:50:11 -080093 for (int i = 0; i < config.threads(); i++) {
Vijay Paiacf6f312015-03-02 15:13:39 -080094 threads_.push_back(std::thread([=]() {
vjpaidea740f2015-02-26 16:35:35 -080095 // Wait until work is available or we are shutting down
96 bool ok;
97 void *got_tag;
98 while (srv_cq_.Next(&got_tag, &ok)) {
vjpai46f65232015-03-23 10:10:27 -070099 ServerRpcContext *ctx = detag(got_tag);
100 // The tag is a pointer to an RPC context to invoke
101 if (ctx->RunNextState(ok) == false) {
102 // this RPC context is done, so refresh it
103 ctx->Reset();
104 }
vjpaidea740f2015-02-26 16:35:35 -0800105 }
106 return;
107 }));
108 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800109 }
110 ~AsyncQpsServerTest() {
111 server_->Shutdown();
112 srv_cq_.Shutdown();
Craig Tillera182bf12015-03-04 13:54:39 -0800113 for (auto &thr : threads_) {
Craig Tillerd6479d62015-03-04 12:50:11 -0800114 thr.join();
115 }
116 while (!contexts_.empty()) {
117 delete contexts_.front();
118 contexts_.pop_front();
vjpaidea740f2015-02-26 16:35:35 -0800119 }
120 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800121
vjpaidea740f2015-02-26 16:35:35 -0800122 private:
123 class ServerRpcContext {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800124 public:
vjpaidea740f2015-02-26 16:35:35 -0800125 ServerRpcContext() {}
Vijay Pai64ac47f2015-02-26 17:59:51 -0800126 virtual ~ServerRpcContext(){};
vjpai46f65232015-03-23 10:10:27 -0700127 virtual bool RunNextState(bool) = 0; // next state, return false if done
Craig Tillera182bf12015-03-04 13:54:39 -0800128 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800129 };
130 static void *tag(ServerRpcContext *func) {
131 return reinterpret_cast<void *>(func);
132 }
133 static ServerRpcContext *detag(void *tag) {
134 return reinterpret_cast<ServerRpcContext *>(tag);
135 }
136
137 template <class RequestType, class ResponseType>
vjpai46f65232015-03-23 10:10:27 -0700138 class ServerRpcContextUnaryImpl GRPC_FINAL : public ServerRpcContext {
vjpaidea740f2015-02-26 16:35:35 -0800139 public:
140 ServerRpcContextUnaryImpl(
vjpai4e1e1bc2015-02-27 23:47:12 -0800141 std::function<void(ServerContext *, RequestType *,
142 grpc::ServerAsyncResponseWriter<ResponseType> *,
143 void *)> request_method,
144 std::function<grpc::Status(const RequestType *, ResponseType *)>
145 invoke_method)
vjpaidea740f2015-02-26 16:35:35 -0800146 : next_state_(&ServerRpcContextUnaryImpl::invoker),
147 request_method_(request_method),
148 invoke_method_(invoke_method),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800149 response_writer_(&srv_ctx_) {
vjpaidea740f2015-02-26 16:35:35 -0800150 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800151 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800152 }
vjpai3c110662015-02-27 07:17:16 -0800153 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
vjpai46f65232015-03-23 10:10:27 -0700154 bool RunNextState(bool ok) GRPC_OVERRIDE {return (this->*next_state_)(ok);}
vjpai5b39f9a2015-02-27 09:33:00 -0800155 void Reset() GRPC_OVERRIDE {
vjpaidea740f2015-02-26 16:35:35 -0800156 srv_ctx_ = ServerContext();
157 req_ = RequestType();
158 response_writer_ =
Vijay Pai64ac47f2015-02-26 17:59:51 -0800159 grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_);
vjpaidea740f2015-02-26 16:35:35 -0800160
161 // Then request the method
162 next_state_ = &ServerRpcContextUnaryImpl::invoker;
163 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800164 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800165 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800166
vjpaidea740f2015-02-26 16:35:35 -0800167 private:
vjpai46f65232015-03-23 10:10:27 -0700168 bool finisher(bool) { return false; }
169 bool invoker(bool ok) {
170 if (!ok)
171 return false;
172
vjpaidea740f2015-02-26 16:35:35 -0800173 ResponseType response;
174
175 // Call the RPC processing function
176 grpc::Status status = invoke_method_(&req_, &response);
177
178 // Have the response writer work and invoke on_finish when done
179 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800180 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800181 return true;
182 }
183 ServerContext srv_ctx_;
184 RequestType req_;
vjpai46f65232015-03-23 10:10:27 -0700185 bool (ServerRpcContextUnaryImpl::*next_state_)(bool);
vjpai4e1e1bc2015-02-27 23:47:12 -0800186 std::function<void(ServerContext *, RequestType *,
187 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
188 request_method_;
189 std::function<grpc::Status(const RequestType *, ResponseType *)>
190 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800191 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
192 };
193
vjpai46f65232015-03-23 10:10:27 -0700194 template <class RequestType, class ResponseType>
195 class ServerRpcContextStreamingImpl GRPC_FINAL : public ServerRpcContext {
196 public:
197 ServerRpcContextStreamingImpl(
198 std::function<void(ServerContext *,
199 grpc::ServerAsyncReaderWriter<ResponseType,
200 RequestType> *, void *)> request_method,
201 std::function<grpc::Status(const RequestType *, ResponseType *)>
202 invoke_method)
203 : next_state_(&ServerRpcContextStreamingImpl::request_done),
204 request_method_(request_method),
205 invoke_method_(invoke_method),
206 stream_(&srv_ctx_) {
207 request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
208 }
209 ~ServerRpcContextStreamingImpl() GRPC_OVERRIDE {
210 }
211 bool RunNextState(bool ok) GRPC_OVERRIDE {return (this->*next_state_)(ok);}
212 void Reset() GRPC_OVERRIDE {
213 srv_ctx_ = ServerContext();
214 req_ = RequestType();
215 stream_ = grpc::ServerAsyncReaderWriter<ResponseType,
216 RequestType>(&srv_ctx_);
217
218 // Then request the method
219 next_state_ = &ServerRpcContextStreamingImpl::request_done;
220 request_method_(&srv_ctx_, &stream_, AsyncQpsServerTest::tag(this));
221 }
222
223 private:
224 bool request_done(bool ok) {
225 if (!ok)
226 return false;
227 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
228 next_state_ = &ServerRpcContextStreamingImpl::read_done;
229 return true;
230 }
231
232 bool read_done(bool ok) {
233 if (ok) {
234 // invoke the method
235 ResponseType response;
236 // Call the RPC processing function
237 grpc::Status status = invoke_method_(&req_, &response);
238 // initiate the write
239 stream_.Write(response, AsyncQpsServerTest::tag(this));
240 next_state_ = &ServerRpcContextStreamingImpl::write_done;
241 } else { // client has sent writes done
242 // finish the stream
243 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
244 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
245 }
246 return true;
247 }
248 bool write_done(bool ok) {
249 // now go back and get another streaming read!
250 if (ok) {
251 stream_.Read(&req_, AsyncQpsServerTest::tag(this));
252 next_state_ = &ServerRpcContextStreamingImpl::read_done;
253 }
254 else {
255 stream_.Finish(Status::OK, AsyncQpsServerTest::tag(this));
256 next_state_ = &ServerRpcContextStreamingImpl::finish_done;
257 }
258 return true;
259 }
260 bool finish_done(bool ok) {return false; /* reset the context */ }
261
262 ServerContext srv_ctx_;
263 RequestType req_;
264 bool (ServerRpcContextStreamingImpl::*next_state_)(bool);
265 std::function<void(ServerContext *,
266 grpc::ServerAsyncReaderWriter<ResponseType,
267 RequestType> *, void *)> request_method_;
268 std::function<grpc::Status(const RequestType *, ResponseType *)>
269 invoke_method_;
270 grpc::ServerAsyncReaderWriter<ResponseType,RequestType> stream_;
271 };
272
273 static Status ProcessRPC(const SimpleRequest *request,
274 SimpleResponse *response) {
275 if (request->response_size() > 0) {
vjpaidea740f2015-02-26 16:35:35 -0800276 if (!SetPayload(request->response_type(), request->response_size(),
277 response->mutable_payload())) {
278 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
279 }
280 }
281 return Status::OK;
282 }
283 CompletionQueue srv_cq_;
284 TestService::AsyncService async_service_;
Vijay Paiacf6f312015-03-02 15:13:39 -0800285 std::vector<std::thread> threads_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800286 std::unique_ptr<grpc::Server> server_;
vjpai4e1e1bc2015-02-27 23:47:12 -0800287 std::function<void(ServerContext *, SimpleRequest *,
288 grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)>
289 request_unary_;
vjpai46f65232015-03-23 10:10:27 -0700290 std::function<void(ServerContext *, grpc::ServerAsyncReaderWriter<
291 SimpleResponse,SimpleRequest> *, void *)>
292 request_streaming_;
vjpaidea740f2015-02-26 16:35:35 -0800293 std::forward_list<ServerRpcContext *> contexts_;
294};
295
Craig Tillera182bf12015-03-04 13:54:39 -0800296std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config,
297 int port) {
298 return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port));
vjpaidea740f2015-02-26 16:35:35 -0800299}
300
Craig Tillera182bf12015-03-04 13:54:39 -0800301} // namespace testing
302} // namespace grpc