blob: 19778e5a7cd09c6c231c17aa09d5e8c4458f59bc [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>
51#include <gtest/gtest.h>
52#include "src/cpp/server/thread_pool.h"
53#include "test/core/util/grpc_profiler.h"
54#include "test/cpp/qps/qpstest.pb.h"
Craig Tillerd6479d62015-03-04 12:50:11 -080055#include "test/cpp/qps/server.h"
vjpaidea740f2015-02-26 16:35:35 -080056
57#include <grpc/grpc.h>
58#include <grpc/support/log.h>
59
Craig Tillerd6479d62015-03-04 12:50:11 -080060namespace grpc {
Craig Tillera182bf12015-03-04 13:54:39 -080061namespace testing {
vjpaidea740f2015-02-26 16:35:35 -080062
Craig Tillerd6479d62015-03-04 12:50:11 -080063class AsyncQpsServerTest : public Server {
vjpaidea740f2015-02-26 16:35:35 -080064 public:
Craig Tillera182bf12015-03-04 13:54:39 -080065 AsyncQpsServerTest(const ServerConfig &config, int port)
66 : srv_cq_(), async_service_(&srv_cq_), server_(nullptr) {
vjpaidea740f2015-02-26 16:35:35 -080067 char *server_address = NULL;
Craig Tillerd6479d62015-03-04 12:50:11 -080068 gpr_join_host_port(&server_address, "::", port);
vjpaidea740f2015-02-26 16:35:35 -080069
70 ServerBuilder builder;
Craig Tiller61691f92015-03-02 08:51:52 -080071 builder.AddPort(server_address, InsecureServerCredentials());
Craig Tillerd6479d62015-03-04 12:50:11 -080072 gpr_free(server_address);
vjpaidea740f2015-02-26 16:35:35 -080073
74 builder.RegisterAsyncService(&async_service_);
75
76 server_ = builder.BuildAndStart();
vjpaidea740f2015-02-26 16:35:35 -080077
78 using namespace std::placeholders;
79 request_unary_ = std::bind(&TestService::AsyncService::RequestUnaryCall,
80 &async_service_, _1, _2, _3, &srv_cq_, _4);
vjpaidea740f2015-02-26 16:35:35 -080081 for (int i = 0; i < 100; i++) {
Vijay Pai64ac47f2015-02-26 17:59:51 -080082 contexts_.push_front(
83 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
84 request_unary_, UnaryCall));
vjpaidea740f2015-02-26 16:35:35 -080085 }
Craig Tillerd6479d62015-03-04 12:50:11 -080086 for (int i = 0; i < config.threads(); i++) {
Vijay Paiacf6f312015-03-02 15:13:39 -080087 threads_.push_back(std::thread([=]() {
vjpaidea740f2015-02-26 16:35:35 -080088 // Wait until work is available or we are shutting down
89 bool ok;
90 void *got_tag;
91 while (srv_cq_.Next(&got_tag, &ok)) {
Vijay Paiacf6f312015-03-02 15:13:39 -080092 if (ok) {
93 ServerRpcContext *ctx = detag(got_tag);
94 // The tag is a pointer to an RPC context to invoke
95 if (ctx->RunNextState() == false) {
96 // this RPC context is done, so refresh it
97 ctx->Reset();
98 }
Vijay Pai64ac47f2015-02-26 17:59:51 -080099 }
vjpaidea740f2015-02-26 16:35:35 -0800100 }
101 return;
102 }));
103 }
Craig Tillerd6479d62015-03-04 12:50:11 -0800104 }
105 ~AsyncQpsServerTest() {
106 server_->Shutdown();
107 srv_cq_.Shutdown();
Craig Tillera182bf12015-03-04 13:54:39 -0800108 for (auto &thr : threads_) {
Craig Tillerd6479d62015-03-04 12:50:11 -0800109 thr.join();
110 }
111 while (!contexts_.empty()) {
112 delete contexts_.front();
113 contexts_.pop_front();
vjpaidea740f2015-02-26 16:35:35 -0800114 }
115 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800116
vjpaidea740f2015-02-26 16:35:35 -0800117 private:
118 class ServerRpcContext {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800119 public:
vjpaidea740f2015-02-26 16:35:35 -0800120 ServerRpcContext() {}
Vijay Pai64ac47f2015-02-26 17:59:51 -0800121 virtual ~ServerRpcContext(){};
Craig Tillera182bf12015-03-04 13:54:39 -0800122 virtual bool RunNextState() = 0; // do next state, return false if all done
123 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800124 };
125 static void *tag(ServerRpcContext *func) {
126 return reinterpret_cast<void *>(func);
127 }
128 static ServerRpcContext *detag(void *tag) {
129 return reinterpret_cast<ServerRpcContext *>(tag);
130 }
131
132 template <class RequestType, class ResponseType>
133 class ServerRpcContextUnaryImpl : public ServerRpcContext {
134 public:
135 ServerRpcContextUnaryImpl(
vjpai4e1e1bc2015-02-27 23:47:12 -0800136 std::function<void(ServerContext *, RequestType *,
137 grpc::ServerAsyncResponseWriter<ResponseType> *,
138 void *)> request_method,
139 std::function<grpc::Status(const RequestType *, ResponseType *)>
140 invoke_method)
vjpaidea740f2015-02-26 16:35:35 -0800141 : next_state_(&ServerRpcContextUnaryImpl::invoker),
142 request_method_(request_method),
143 invoke_method_(invoke_method),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800144 response_writer_(&srv_ctx_) {
vjpaidea740f2015-02-26 16:35:35 -0800145 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800146 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800147 }
vjpai3c110662015-02-27 07:17:16 -0800148 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
vjpai6e2e64a2015-02-27 09:28:28 -0800149 bool RunNextState() GRPC_OVERRIDE { return (this->*next_state_)(); }
vjpai5b39f9a2015-02-27 09:33:00 -0800150 void Reset() GRPC_OVERRIDE {
vjpaidea740f2015-02-26 16:35:35 -0800151 srv_ctx_ = ServerContext();
152 req_ = RequestType();
153 response_writer_ =
Vijay Pai64ac47f2015-02-26 17:59:51 -0800154 grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_);
vjpaidea740f2015-02-26 16:35:35 -0800155
156 // Then request the method
157 next_state_ = &ServerRpcContextUnaryImpl::invoker;
158 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800159 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800160 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800161
vjpaidea740f2015-02-26 16:35:35 -0800162 private:
Vijay Pai64ac47f2015-02-26 17:59:51 -0800163 bool finisher() { return false; }
vjpaidea740f2015-02-26 16:35:35 -0800164 bool invoker() {
165 ResponseType response;
166
167 // Call the RPC processing function
168 grpc::Status status = invoke_method_(&req_, &response);
169
170 // Have the response writer work and invoke on_finish when done
171 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800172 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800173 return true;
174 }
175 ServerContext srv_ctx_;
176 RequestType req_;
177 bool (ServerRpcContextUnaryImpl::*next_state_)();
vjpai4e1e1bc2015-02-27 23:47:12 -0800178 std::function<void(ServerContext *, RequestType *,
179 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
180 request_method_;
181 std::function<grpc::Status(const RequestType *, ResponseType *)>
182 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800183 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
184 };
185
vjpaidea740f2015-02-26 16:35:35 -0800186 static Status UnaryCall(const SimpleRequest *request,
187 SimpleResponse *response) {
188 if (request->has_response_size() && request->response_size() > 0) {
189 if (!SetPayload(request->response_type(), request->response_size(),
190 response->mutable_payload())) {
191 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
192 }
193 }
194 return Status::OK;
195 }
196 CompletionQueue srv_cq_;
197 TestService::AsyncService async_service_;
Vijay Paiacf6f312015-03-02 15:13:39 -0800198 std::vector<std::thread> threads_;
Craig Tillerd6479d62015-03-04 12:50:11 -0800199 std::unique_ptr<grpc::Server> server_;
vjpai4e1e1bc2015-02-27 23:47:12 -0800200 std::function<void(ServerContext *, SimpleRequest *,
201 grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)>
202 request_unary_;
vjpaidea740f2015-02-26 16:35:35 -0800203 std::forward_list<ServerRpcContext *> contexts_;
204};
205
Craig Tillera182bf12015-03-04 13:54:39 -0800206std::unique_ptr<Server> CreateAsyncServer(const ServerConfig &config,
207 int port) {
208 return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port));
vjpaidea740f2015-02-26 16:35:35 -0800209}
210
Craig Tillera182bf12015-03-04 13:54:39 -0800211} // namespace testing
212} // namespace grpc