blob: a5edb0549380635b04a5cdf0e97356bdd4fee6ea [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
vpai92fe70e2015-01-13 11:21:38 -080034#include <sys/time.h>
35#include <sys/resource.h>
Craig Tiller056ba542015-01-31 21:15:10 -080036#include <sys/signal.h>
vpai80b6d012014-12-17 11:47:32 -080037#include <thread>
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"
vpai80b6d012014-12-17 11:47:32 -080051
52#include <grpc/grpc.h>
53#include <grpc/support/log.h>
54
vpai80b6d012014-12-17 11:47:32 -080055DEFINE_int32(port, 0, "Server port.");
Craig Tiller5c004c62015-02-23 16:31:57 -080056DEFINE_int32(driver_port, 0, "Server driver port.");
vpai80b6d012014-12-17 11:47:32 -080057
58using grpc::Server;
59using grpc::ServerBuilder;
yangga4b6f5d2014-12-17 15:53:12 -080060using grpc::ServerContext;
Craig Tiller5c004c62015-02-23 16:31:57 -080061using grpc::ServerReaderWriter;
Vijay Paic3b02d92015-02-10 10:39:03 -080062using grpc::ThreadPool;
vpai80b6d012014-12-17 11:47:32 -080063using grpc::testing::Payload;
64using grpc::testing::PayloadType;
vpai92fe70e2015-01-13 11:21:38 -080065using grpc::testing::ServerStats;
vpai80b6d012014-12-17 11:47:32 -080066using grpc::testing::SimpleRequest;
67using grpc::testing::SimpleResponse;
vpai92fe70e2015-01-13 11:21:38 -080068using grpc::testing::StatsRequest;
vpai80b6d012014-12-17 11:47:32 -080069using grpc::testing::TestService;
Craig Tiller5c004c62015-02-23 16:31:57 -080070using grpc::testing::QpsServer;
71using grpc::testing::ServerArgs;
72using grpc::testing::ServerStats;
73using grpc::testing::ServerStatus;
vpai80b6d012014-12-17 11:47:32 -080074using grpc::Status;
75
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010076// In some distros, gflags is in the namespace google, and in some others,
77// in gflags. This hack is enabling us to find both.
78namespace google { }
79namespace gflags { }
80using namespace google;
81using namespace gflags;
82
Craig Tiller056ba542015-01-31 21:15:10 -080083static bool got_sigint = false;
84
85static void sigint_handler(int x) { got_sigint = 1; }
86
vpai92fe70e2015-01-13 11:21:38 -080087static double time_double(struct timeval* tv) {
88 return tv->tv_sec + 1e-6 * tv->tv_usec;
89}
90
Craig Tiller056ba542015-01-31 21:15:10 -080091static bool SetPayload(PayloadType type, int size, Payload* payload) {
vpai80b6d012014-12-17 11:47:32 -080092 PayloadType response_type = type;
93 // TODO(yangg): Support UNCOMPRESSABLE payload.
94 if (type != PayloadType::COMPRESSABLE) {
95 return false;
96 }
97 payload->set_type(response_type);
98 std::unique_ptr<char[]> body(new char[size]());
99 payload->set_body(body.get(), size);
100 return true;
101}
102
Craig Tiller056ba542015-01-31 21:15:10 -0800103namespace {
104
105class TestServiceImpl final : public TestService::Service {
vpai80b6d012014-12-17 11:47:32 -0800106 public:
vpai92fe70e2015-01-13 11:21:38 -0800107 Status CollectServerStats(ServerContext* context, const StatsRequest*,
108 ServerStats* response) {
109 struct rusage usage;
110 struct timeval tv;
111 gettimeofday(&tv, NULL);
112 getrusage(RUSAGE_SELF, &usage);
113 response->set_time_now(time_double(&tv));
114 response->set_time_user(time_double(&usage.ru_utime));
115 response->set_time_system(time_double(&usage.ru_stime));
116 return Status::OK;
117 }
yangga4b6f5d2014-12-17 15:53:12 -0800118 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
119 SimpleResponse* response) {
vpai80b6d012014-12-17 11:47:32 -0800120 if (request->has_response_size() && request->response_size() > 0) {
121 if (!SetPayload(request->response_type(), request->response_size(),
122 response->mutable_payload())) {
123 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
124 }
125 }
126 return Status::OK;
127 }
128};
129
Craig Tiller056ba542015-01-31 21:15:10 -0800130} // namespace
131
Craig Tiller5c004c62015-02-23 16:31:57 -0800132class ServerImpl : public QpsServer::Service {
133 public:
134 Status RunServer(ServerContext* ctx, ServerReaderWriter<ServerStatus, ServerArgs>* stream) {
135 ServerArgs args;
136 std::unique_ptr<ServerStats> last_stats;
137 if (!stream->Read(&args)) return Status::OK;
138
139 bool done = false;
140 while (!done) {
141 std::lock_guard<std::mutex> lock(server_mu_);
142
143 char* server_address = NULL;
144 gpr_join_host_port(&server_address, "::", FLAGS_port);
145
146 TestServiceImpl service;
147
148 ServerBuilder builder;
149 builder.AddPort(server_address);
150 builder.RegisterService(&service);
151
152 gpr_free(server_address);
153
154 std::unique_ptr<ThreadPool> pool(new ThreadPool(args.threads()));
155 builder.SetThreadPool(pool.get());
156
157 auto server = builder.BuildAndStart();
158 gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
159
160 ServerStatus last_status;
161 if (last_stats.get()) {
162 *last_status.mutable_stats() = *last_stats;
163 }
164 if (!stream->Write(last_status)) return Status(grpc::UNKNOWN);
165
166 grpc_profiler_start("qps_server.prof");
167
168 done = stream->Read(&args);
169
170 grpc_profiler_stop();
171 }
172
173 ServerStatus last_status;
174 if (last_stats.get()) {
175 *last_status.mutable_stats() = *last_stats;
176 }
177 stream->Write(last_status);
178 return Status::OK;
179 }
180
181 private:
182 std::mutex server_mu_;
183};
184
Craig Tiller056ba542015-01-31 21:15:10 -0800185static void RunServer() {
vpai80b6d012014-12-17 11:47:32 -0800186 char* server_address = NULL;
Craig Tiller5c004c62015-02-23 16:31:57 -0800187 gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
vpai80b6d012014-12-17 11:47:32 -0800188
Craig Tiller5c004c62015-02-23 16:31:57 -0800189 ServerImpl service;
vpai80b6d012014-12-17 11:47:32 -0800190
191 ServerBuilder builder;
192 builder.AddPort(server_address);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800193 builder.RegisterService(&service);
Vijay Paic3b02d92015-02-10 10:39:03 -0800194
Craig Tiller5c004c62015-02-23 16:31:57 -0800195 gpr_free(server_address);
Vijay Paic3b02d92015-02-10 10:39:03 -0800196
Craig Tiller5c004c62015-02-23 16:31:57 -0800197 auto server = builder.BuildAndStart();
Craig Tiller056ba542015-01-31 21:15:10 -0800198
199 while (!got_sigint) {
vpai80b6d012014-12-17 11:47:32 -0800200 std::this_thread::sleep_for(std::chrono::seconds(5));
201 }
vpai80b6d012014-12-17 11:47:32 -0800202}
203
204int main(int argc, char** argv) {
205 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100206 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800207
Craig Tiller056ba542015-01-31 21:15:10 -0800208 signal(SIGINT, sigint_handler);
Craig Tiller06059952015-02-18 08:34:56 -0800209
vpai80b6d012014-12-17 11:47:32 -0800210 GPR_ASSERT(FLAGS_port != 0);
vpai80b6d012014-12-17 11:47:32 -0800211 RunServer();
212
213 grpc_shutdown();
214 return 0;
215}