blob: c019c1b3f53e0dac61dfb8317c888db1b3c70416 [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"
55
56#include <grpc/grpc.h>
57#include <grpc/support/log.h>
58
59DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
60DEFINE_int32(port, 0, "Server port.");
61DEFINE_int32(server_threads, 4, "Number of server threads.");
62
63using grpc::CompletionQueue;
Craig Tiller61691f92015-03-02 08:51:52 -080064using grpc::InsecureServerCredentials;
vjpaidea740f2015-02-26 16:35:35 -080065using grpc::Server;
66using grpc::ServerBuilder;
67using grpc::ServerContext;
68using grpc::ThreadPool;
69using grpc::testing::Payload;
70using grpc::testing::PayloadType;
71using grpc::testing::ServerStats;
72using grpc::testing::SimpleRequest;
73using grpc::testing::SimpleResponse;
74using grpc::testing::StatsRequest;
75using grpc::testing::TestService;
76using grpc::Status;
77
78// In some distros, gflags is in the namespace google, and in some others,
79// in gflags. This hack is enabling us to find both.
80namespace google {}
81namespace gflags {}
82using namespace google;
83using namespace gflags;
84
85static bool got_sigint = false;
86
87static void sigint_handler(int x) { got_sigint = 1; }
88
89static double time_double(struct timeval *tv) {
90 return tv->tv_sec + 1e-6 * tv->tv_usec;
91}
92
93static bool SetPayload(PayloadType type, int size, Payload *payload) {
94 PayloadType response_type = type;
95 // TODO(yangg): Support UNCOMPRESSABLE payload.
96 if (type != PayloadType::COMPRESSABLE) {
97 return false;
98 }
99 payload->set_type(response_type);
100 std::unique_ptr<char[]> body(new char[size]());
101 payload->set_body(body.get(), size);
102 return true;
103}
104
105namespace {
106
107class AsyncQpsServerTest {
108 public:
109 AsyncQpsServerTest() : srv_cq_(), async_service_(&srv_cq_), server_(nullptr) {
110 char *server_address = NULL;
111 gpr_join_host_port(&server_address, "::", FLAGS_port);
112
113 ServerBuilder builder;
Craig Tiller61691f92015-03-02 08:51:52 -0800114 builder.AddPort(server_address, InsecureServerCredentials());
vjpaidea740f2015-02-26 16:35:35 -0800115
116 builder.RegisterAsyncService(&async_service_);
117
118 server_ = builder.BuildAndStart();
119 gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
120 gpr_free(server_address);
121
122 using namespace std::placeholders;
123 request_unary_ = std::bind(&TestService::AsyncService::RequestUnaryCall,
124 &async_service_, _1, _2, _3, &srv_cq_, _4);
125 request_stats_ =
126 std::bind(&TestService::AsyncService::RequestCollectServerStats,
127 &async_service_, _1, _2, _3, &srv_cq_, _4);
128 for (int i = 0; i < 100; i++) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800129 contexts_.push_front(
130 new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>(
131 request_unary_, UnaryCall));
132 contexts_.push_front(
133 new ServerRpcContextUnaryImpl<StatsRequest, ServerStats>(
134 request_stats_, CollectServerStats));
vjpaidea740f2015-02-26 16:35:35 -0800135 }
136 }
137 ~AsyncQpsServerTest() {
138 server_->Shutdown();
139 void *ignored_tag;
140 bool ignored_ok;
141 srv_cq_.Shutdown();
142 while (srv_cq_.Next(&ignored_tag, &ignored_ok)) {
143 }
144 while (!contexts_.empty()) {
145 delete contexts_.front();
146 contexts_.pop_front();
147 }
148 }
149 void ServeRpcs(int num_threads) {
150 std::vector<std::thread> threads;
151 for (int i = 0; i < num_threads; i++) {
152 threads.push_back(std::thread([=]() {
153 // Wait until work is available or we are shutting down
154 bool ok;
155 void *got_tag;
156 while (srv_cq_.Next(&got_tag, &ok)) {
157 EXPECT_EQ(ok, true);
Vijay Pai64ac47f2015-02-26 17:59:51 -0800158 ServerRpcContext *ctx = detag(got_tag);
vjpaidea740f2015-02-26 16:35:35 -0800159 // The tag is a pointer to an RPC context to invoke
vjpai6e2e64a2015-02-27 09:28:28 -0800160 if (ctx->RunNextState() == false) {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800161 // this RPC context is done, so refresh it
vjpai5b39f9a2015-02-27 09:33:00 -0800162 ctx->Reset();
Vijay Pai64ac47f2015-02-26 17:59:51 -0800163 }
vjpaidea740f2015-02-26 16:35:35 -0800164 }
165 return;
166 }));
167 }
168 while (!got_sigint) {
169 std::this_thread::sleep_for(std::chrono::seconds(5));
170 }
171 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800172
vjpaidea740f2015-02-26 16:35:35 -0800173 private:
174 class ServerRpcContext {
Vijay Pai64ac47f2015-02-26 17:59:51 -0800175 public:
vjpaidea740f2015-02-26 16:35:35 -0800176 ServerRpcContext() {}
Vijay Pai64ac47f2015-02-26 17:59:51 -0800177 virtual ~ServerRpcContext(){};
vjpai6e2e64a2015-02-27 09:28:28 -0800178 virtual bool RunNextState() = 0;// do next state, return false if all done
vjpai5b39f9a2015-02-27 09:33:00 -0800179 virtual void Reset() = 0; // start this back at a clean state
vjpaidea740f2015-02-26 16:35:35 -0800180 };
181 static void *tag(ServerRpcContext *func) {
182 return reinterpret_cast<void *>(func);
183 }
184 static ServerRpcContext *detag(void *tag) {
185 return reinterpret_cast<ServerRpcContext *>(tag);
186 }
187
188 template <class RequestType, class ResponseType>
189 class ServerRpcContextUnaryImpl : public ServerRpcContext {
190 public:
191 ServerRpcContextUnaryImpl(
vjpai4e1e1bc2015-02-27 23:47:12 -0800192 std::function<void(ServerContext *, RequestType *,
193 grpc::ServerAsyncResponseWriter<ResponseType> *,
194 void *)> request_method,
195 std::function<grpc::Status(const RequestType *, ResponseType *)>
196 invoke_method)
vjpaidea740f2015-02-26 16:35:35 -0800197 : next_state_(&ServerRpcContextUnaryImpl::invoker),
198 request_method_(request_method),
199 invoke_method_(invoke_method),
Vijay Pai64ac47f2015-02-26 17:59:51 -0800200 response_writer_(&srv_ctx_) {
vjpaidea740f2015-02-26 16:35:35 -0800201 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800202 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800203 }
vjpai3c110662015-02-27 07:17:16 -0800204 ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {}
vjpai6e2e64a2015-02-27 09:28:28 -0800205 bool RunNextState() GRPC_OVERRIDE { return (this->*next_state_)(); }
vjpai5b39f9a2015-02-27 09:33:00 -0800206 void Reset() GRPC_OVERRIDE {
vjpaidea740f2015-02-26 16:35:35 -0800207 srv_ctx_ = ServerContext();
208 req_ = RequestType();
209 response_writer_ =
Vijay Pai64ac47f2015-02-26 17:59:51 -0800210 grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_);
vjpaidea740f2015-02-26 16:35:35 -0800211
212 // Then request the method
213 next_state_ = &ServerRpcContextUnaryImpl::invoker;
214 request_method_(&srv_ctx_, &req_, &response_writer_,
Vijay Pai64ac47f2015-02-26 17:59:51 -0800215 AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800216 }
Vijay Pai64ac47f2015-02-26 17:59:51 -0800217
vjpaidea740f2015-02-26 16:35:35 -0800218 private:
Vijay Pai64ac47f2015-02-26 17:59:51 -0800219 bool finisher() { return false; }
vjpaidea740f2015-02-26 16:35:35 -0800220 bool invoker() {
221 ResponseType response;
222
223 // Call the RPC processing function
224 grpc::Status status = invoke_method_(&req_, &response);
225
226 // Have the response writer work and invoke on_finish when done
227 next_state_ = &ServerRpcContextUnaryImpl::finisher;
Vijay Pai64ac47f2015-02-26 17:59:51 -0800228 response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this));
vjpaidea740f2015-02-26 16:35:35 -0800229 return true;
230 }
231 ServerContext srv_ctx_;
232 RequestType req_;
233 bool (ServerRpcContextUnaryImpl::*next_state_)();
vjpai4e1e1bc2015-02-27 23:47:12 -0800234 std::function<void(ServerContext *, RequestType *,
235 grpc::ServerAsyncResponseWriter<ResponseType> *, void *)>
236 request_method_;
237 std::function<grpc::Status(const RequestType *, ResponseType *)>
238 invoke_method_;
vjpaidea740f2015-02-26 16:35:35 -0800239 grpc::ServerAsyncResponseWriter<ResponseType> response_writer_;
240 };
241
242 static Status CollectServerStats(const StatsRequest *,
243 ServerStats *response) {
244 struct rusage usage;
245 struct timeval tv;
246 gettimeofday(&tv, NULL);
247 getrusage(RUSAGE_SELF, &usage);
248 response->set_time_now(time_double(&tv));
249 response->set_time_user(time_double(&usage.ru_utime));
250 response->set_time_system(time_double(&usage.ru_stime));
251 return Status::OK;
252 }
253 static Status UnaryCall(const SimpleRequest *request,
254 SimpleResponse *response) {
255 if (request->has_response_size() && request->response_size() > 0) {
256 if (!SetPayload(request->response_type(), request->response_size(),
257 response->mutable_payload())) {
258 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
259 }
260 }
261 return Status::OK;
262 }
263 CompletionQueue srv_cq_;
264 TestService::AsyncService async_service_;
265 std::unique_ptr<Server> server_;
vjpai4e1e1bc2015-02-27 23:47:12 -0800266 std::function<void(ServerContext *, SimpleRequest *,
267 grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)>
268 request_unary_;
269 std::function<void(ServerContext *, StatsRequest *,
270 grpc::ServerAsyncResponseWriter<ServerStats> *, void *)>
271 request_stats_;
vjpaidea740f2015-02-26 16:35:35 -0800272 std::forward_list<ServerRpcContext *> contexts_;
273};
274
275} // namespace
276
277static void RunServer() {
278 AsyncQpsServerTest server;
279
280 grpc_profiler_start("qps_server_async.prof");
281
282 server.ServeRpcs(FLAGS_server_threads);
283
284 grpc_profiler_stop();
285}
286
287int main(int argc, char **argv) {
288 grpc_init();
289 ParseCommandLineFlags(&argc, &argv, true);
290 GPR_ASSERT(FLAGS_port != 0);
291 GPR_ASSERT(!FLAGS_enable_ssl);
292
293 signal(SIGINT, sigint_handler);
294
295 RunServer();
296
297 grpc_shutdown();
298 google::protobuf::ShutdownProtobufLibrary();
299
300 return 0;
301}