blob: 75425c9eb8e2dce9597b68bbc2eda18aabd678e6 [file] [log] [blame]
vpai80b6d012014-12-17 11:47:32 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
vpai80b6d012014-12-17 11:47:32 -08004 * 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
Craig Tiller056ba542015-01-31 21:15:10 -080034#include <sys/signal.h>
vpai80b6d012014-12-17 11:47:32 -080035#include <thread>
36
Craig Tillercf133f42015-02-26 14:05:56 -080037#include <unistd.h>
38
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010039#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080040#include <grpc/support/alloc.h>
41#include <grpc/support/host_port.h>
42#include <grpc++/config.h>
43#include <grpc++/server.h>
44#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080045#include <grpc++/server_context.h>
vpai80b6d012014-12-17 11:47:32 -080046#include <grpc++/status.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080047#include <grpc++/stream.h>
Vijay Paic3b02d92015-02-10 10:39:03 -080048#include "src/cpp/server/thread_pool.h"
Craig Tiller056ba542015-01-31 21:15:10 -080049#include "test/core/util/grpc_profiler.h"
vpai92fe70e2015-01-13 11:21:38 -080050#include "test/cpp/qps/qpstest.pb.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080051#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080052
53#include <grpc/grpc.h>
54#include <grpc/support/log.h>
55
vpai80b6d012014-12-17 11:47:32 -080056DEFINE_int32(port, 0, "Server port.");
Craig Tiller5c004c62015-02-23 16:31:57 -080057DEFINE_int32(driver_port, 0, "Server driver port.");
vpai80b6d012014-12-17 11:47:32 -080058
59using grpc::Server;
60using grpc::ServerBuilder;
yangga4b6f5d2014-12-17 15:53:12 -080061using grpc::ServerContext;
Craig Tiller5c004c62015-02-23 16:31:57 -080062using grpc::ServerReaderWriter;
Vijay Paic3b02d92015-02-10 10:39:03 -080063using grpc::ThreadPool;
vpai80b6d012014-12-17 11:47:32 -080064using grpc::testing::Payload;
65using grpc::testing::PayloadType;
vpai92fe70e2015-01-13 11:21:38 -080066using grpc::testing::ServerStats;
vpai80b6d012014-12-17 11:47:32 -080067using grpc::testing::SimpleRequest;
68using grpc::testing::SimpleResponse;
vpai92fe70e2015-01-13 11:21:38 -080069using grpc::testing::StatsRequest;
vpai80b6d012014-12-17 11:47:32 -080070using grpc::testing::TestService;
Craig Tiller5c004c62015-02-23 16:31:57 -080071using grpc::testing::QpsServer;
72using grpc::testing::ServerArgs;
73using grpc::testing::ServerStats;
74using grpc::testing::ServerStatus;
vpai80b6d012014-12-17 11:47:32 -080075using grpc::Status;
76
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010077// In some distros, gflags is in the namespace google, and in some others,
78// in gflags. This hack is enabling us to find both.
79namespace google { }
80namespace gflags { }
81using namespace google;
82using namespace gflags;
83
Craig Tiller056ba542015-01-31 21:15:10 -080084static bool got_sigint = false;
85
86static void sigint_handler(int x) { got_sigint = 1; }
87
Craig Tiller056ba542015-01-31 21:15:10 -080088static bool SetPayload(PayloadType type, int size, Payload* payload) {
vpai80b6d012014-12-17 11:47:32 -080089 PayloadType response_type = type;
90 // TODO(yangg): Support UNCOMPRESSABLE payload.
91 if (type != PayloadType::COMPRESSABLE) {
92 return false;
93 }
94 payload->set_type(response_type);
95 std::unique_ptr<char[]> body(new char[size]());
96 payload->set_body(body.get(), size);
97 return true;
98}
99
Craig Tiller056ba542015-01-31 21:15:10 -0800100namespace {
101
Craig Tillercf133f42015-02-26 14:05:56 -0800102class TestServiceImpl GRPC_FINAL : public TestService::Service {
vpai80b6d012014-12-17 11:47:32 -0800103 public:
yangga4b6f5d2014-12-17 15:53:12 -0800104 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800105 SimpleResponse* response) override {
vpai80b6d012014-12-17 11:47:32 -0800106 if (request->has_response_size() && request->response_size() > 0) {
107 if (!SetPayload(request->response_type(), request->response_size(),
108 response->mutable_payload())) {
109 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
110 }
111 }
112 return Status::OK;
113 }
114};
115
Craig Tiller056ba542015-01-31 21:15:10 -0800116} // namespace
117
Craig Tiller5c004c62015-02-23 16:31:57 -0800118class ServerImpl : public QpsServer::Service {
119 public:
120 Status RunServer(ServerContext* ctx, ServerReaderWriter<ServerStatus, ServerArgs>* stream) {
121 ServerArgs args;
Craig Tiller5c004c62015-02-23 16:31:57 -0800122 if (!stream->Read(&args)) return Status::OK;
123
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800124 std::lock_guard<std::mutex> lock(server_mu_);
Craig Tiller5c004c62015-02-23 16:31:57 -0800125
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800126 char* server_address = NULL;
127 gpr_join_host_port(&server_address, "::", FLAGS_port);
Craig Tiller5c004c62015-02-23 16:31:57 -0800128
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800129 TestServiceImpl service;
Craig Tiller5c004c62015-02-23 16:31:57 -0800130
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800131 ServerBuilder builder;
132 builder.AddPort(server_address);
133 builder.RegisterService(&service);
Craig Tiller5c004c62015-02-23 16:31:57 -0800134
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800135 std::unique_ptr<ThreadPool> pool(new ThreadPool(args.config().threads()));
136 builder.SetThreadPool(pool.get());
Craig Tiller5c004c62015-02-23 16:31:57 -0800137
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800138 auto server = builder.BuildAndStart();
139 gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
Craig Tiller5c004c62015-02-23 16:31:57 -0800140
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800141 gpr_free(server_address);
Craig Tiller5c004c62015-02-23 16:31:57 -0800142
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800143 ServerStatus status;
144 status.set_port(FLAGS_port);
145 if (!stream->Write(status)) return Status(grpc::UNKNOWN);
Craig Tiller5c004c62015-02-23 16:31:57 -0800146
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800147 grpc_profiler_start("qps_server.prof");
148 Timer timer;
Craig Tiller5c004c62015-02-23 16:31:57 -0800149
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800150 if (stream->Read(&args)) {
151 gpr_log(GPR_ERROR, "Got a server request, but not expecting one");
152 return Status(grpc::UNKNOWN);
Craig Tiller5c004c62015-02-23 16:31:57 -0800153 }
154
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800155 auto timer_result = timer.Mark();
156 grpc_profiler_stop();
157
158 auto* stats = status.mutable_stats();
159 stats->set_time_elapsed(timer_result.wall);
160 stats->set_time_system(timer_result.system);
161 stats->set_time_user(timer_result.user);
162 stream->Write(status);
Craig Tiller5c004c62015-02-23 16:31:57 -0800163 return Status::OK;
164 }
165
166 private:
167 std::mutex server_mu_;
168};
169
Craig Tiller056ba542015-01-31 21:15:10 -0800170static void RunServer() {
vpai80b6d012014-12-17 11:47:32 -0800171 char* server_address = NULL;
Craig Tiller5c004c62015-02-23 16:31:57 -0800172 gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
vpai80b6d012014-12-17 11:47:32 -0800173
Craig Tiller5c004c62015-02-23 16:31:57 -0800174 ServerImpl service;
vpai80b6d012014-12-17 11:47:32 -0800175
176 ServerBuilder builder;
177 builder.AddPort(server_address);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800178 builder.RegisterService(&service);
Vijay Paic3b02d92015-02-10 10:39:03 -0800179
Craig Tiller5c004c62015-02-23 16:31:57 -0800180 gpr_free(server_address);
Vijay Paic3b02d92015-02-10 10:39:03 -0800181
Craig Tiller5c004c62015-02-23 16:31:57 -0800182 auto server = builder.BuildAndStart();
Craig Tiller056ba542015-01-31 21:15:10 -0800183
184 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800185 sleep(5);
vpai80b6d012014-12-17 11:47:32 -0800186 }
vpai80b6d012014-12-17 11:47:32 -0800187}
188
189int main(int argc, char** argv) {
Craig Tiller8ecb1702015-02-24 17:08:23 -0800190 signal(SIGINT, sigint_handler);
191
vpai80b6d012014-12-17 11:47:32 -0800192 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100193 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800194
195 GPR_ASSERT(FLAGS_port != 0);
vpai80b6d012014-12-17 11:47:32 -0800196 RunServer();
197
198 grpc_shutdown();
199 return 0;
200}