blob: 89fdfeb0cb97c36998438672bea77036e03bbcd5 [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
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010037#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080038#include <grpc/support/alloc.h>
39#include <grpc/support/host_port.h>
40#include <grpc++/config.h>
41#include <grpc++/server.h>
42#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080043#include <grpc++/server_context.h>
vpai80b6d012014-12-17 11:47:32 -080044#include <grpc++/status.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080045#include <grpc++/stream.h>
Vijay Paic3b02d92015-02-10 10:39:03 -080046#include "src/cpp/server/thread_pool.h"
Craig Tiller056ba542015-01-31 21:15:10 -080047#include "test/core/util/grpc_profiler.h"
vpai92fe70e2015-01-13 11:21:38 -080048#include "test/cpp/qps/qpstest.pb.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080049#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080050
51#include <grpc/grpc.h>
52#include <grpc/support/log.h>
53
vpai80b6d012014-12-17 11:47:32 -080054DEFINE_int32(port, 0, "Server port.");
Craig Tiller5c004c62015-02-23 16:31:57 -080055DEFINE_int32(driver_port, 0, "Server driver port.");
vpai80b6d012014-12-17 11:47:32 -080056
57using grpc::Server;
58using grpc::ServerBuilder;
yangga4b6f5d2014-12-17 15:53:12 -080059using grpc::ServerContext;
Craig Tiller5c004c62015-02-23 16:31:57 -080060using grpc::ServerReaderWriter;
Vijay Paic3b02d92015-02-10 10:39:03 -080061using grpc::ThreadPool;
vpai80b6d012014-12-17 11:47:32 -080062using grpc::testing::Payload;
63using grpc::testing::PayloadType;
vpai92fe70e2015-01-13 11:21:38 -080064using grpc::testing::ServerStats;
vpai80b6d012014-12-17 11:47:32 -080065using grpc::testing::SimpleRequest;
66using grpc::testing::SimpleResponse;
vpai92fe70e2015-01-13 11:21:38 -080067using grpc::testing::StatsRequest;
vpai80b6d012014-12-17 11:47:32 -080068using grpc::testing::TestService;
Craig Tiller5c004c62015-02-23 16:31:57 -080069using grpc::testing::QpsServer;
70using grpc::testing::ServerArgs;
71using grpc::testing::ServerStats;
72using grpc::testing::ServerStatus;
vpai80b6d012014-12-17 11:47:32 -080073using grpc::Status;
74
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010075// In some distros, gflags is in the namespace google, and in some others,
76// in gflags. This hack is enabling us to find both.
77namespace google { }
78namespace gflags { }
79using namespace google;
80using namespace gflags;
81
Craig Tiller056ba542015-01-31 21:15:10 -080082static bool got_sigint = false;
83
84static void sigint_handler(int x) { got_sigint = 1; }
85
Craig Tiller056ba542015-01-31 21:15:10 -080086static bool SetPayload(PayloadType type, int size, Payload* payload) {
vpai80b6d012014-12-17 11:47:32 -080087 PayloadType response_type = type;
88 // TODO(yangg): Support UNCOMPRESSABLE payload.
89 if (type != PayloadType::COMPRESSABLE) {
90 return false;
91 }
92 payload->set_type(response_type);
93 std::unique_ptr<char[]> body(new char[size]());
94 payload->set_body(body.get(), size);
95 return true;
96}
97
Craig Tiller056ba542015-01-31 21:15:10 -080098namespace {
99
100class TestServiceImpl final : public TestService::Service {
vpai80b6d012014-12-17 11:47:32 -0800101 public:
yangga4b6f5d2014-12-17 15:53:12 -0800102 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800103 SimpleResponse* response) override {
vpai80b6d012014-12-17 11:47:32 -0800104 if (request->has_response_size() && request->response_size() > 0) {
105 if (!SetPayload(request->response_type(), request->response_size(),
106 response->mutable_payload())) {
107 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
108 }
109 }
110 return Status::OK;
111 }
112};
113
Craig Tiller056ba542015-01-31 21:15:10 -0800114} // namespace
115
Craig Tiller5c004c62015-02-23 16:31:57 -0800116class ServerImpl : public QpsServer::Service {
117 public:
118 Status RunServer(ServerContext* ctx, ServerReaderWriter<ServerStatus, ServerArgs>* stream) {
119 ServerArgs args;
Craig Tiller5c004c62015-02-23 16:31:57 -0800120 if (!stream->Read(&args)) return Status::OK;
121
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800122 std::lock_guard<std::mutex> lock(server_mu_);
Craig Tiller5c004c62015-02-23 16:31:57 -0800123
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800124 char* server_address = NULL;
125 gpr_join_host_port(&server_address, "::", FLAGS_port);
Craig Tiller5c004c62015-02-23 16:31:57 -0800126
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800127 TestServiceImpl service;
Craig Tiller5c004c62015-02-23 16:31:57 -0800128
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800129 ServerBuilder builder;
130 builder.AddPort(server_address);
131 builder.RegisterService(&service);
Craig Tiller5c004c62015-02-23 16:31:57 -0800132
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800133 std::unique_ptr<ThreadPool> pool(new ThreadPool(args.config().threads()));
134 builder.SetThreadPool(pool.get());
Craig Tiller5c004c62015-02-23 16:31:57 -0800135
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800136 auto server = builder.BuildAndStart();
137 gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
Craig Tiller5c004c62015-02-23 16:31:57 -0800138
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800139 gpr_free(server_address);
Craig Tiller5c004c62015-02-23 16:31:57 -0800140
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800141 ServerStatus status;
142 status.set_port(FLAGS_port);
143 if (!stream->Write(status)) return Status(grpc::UNKNOWN);
Craig Tiller5c004c62015-02-23 16:31:57 -0800144
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800145 grpc_profiler_start("qps_server.prof");
146 Timer timer;
Craig Tiller5c004c62015-02-23 16:31:57 -0800147
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800148 if (stream->Read(&args)) {
149 gpr_log(GPR_ERROR, "Got a server request, but not expecting one");
150 return Status(grpc::UNKNOWN);
Craig Tiller5c004c62015-02-23 16:31:57 -0800151 }
152
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800153 auto timer_result = timer.Mark();
154 grpc_profiler_stop();
155
156 auto* stats = status.mutable_stats();
157 stats->set_time_elapsed(timer_result.wall);
158 stats->set_time_system(timer_result.system);
159 stats->set_time_user(timer_result.user);
160 stream->Write(status);
Craig Tiller5c004c62015-02-23 16:31:57 -0800161 return Status::OK;
162 }
163
164 private:
165 std::mutex server_mu_;
166};
167
Craig Tiller056ba542015-01-31 21:15:10 -0800168static void RunServer() {
vpai80b6d012014-12-17 11:47:32 -0800169 char* server_address = NULL;
Craig Tiller5c004c62015-02-23 16:31:57 -0800170 gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
vpai80b6d012014-12-17 11:47:32 -0800171
Craig Tiller5c004c62015-02-23 16:31:57 -0800172 ServerImpl service;
vpai80b6d012014-12-17 11:47:32 -0800173
174 ServerBuilder builder;
175 builder.AddPort(server_address);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800176 builder.RegisterService(&service);
Vijay Paic3b02d92015-02-10 10:39:03 -0800177
Craig Tiller5c004c62015-02-23 16:31:57 -0800178 gpr_free(server_address);
Vijay Paic3b02d92015-02-10 10:39:03 -0800179
Craig Tiller5c004c62015-02-23 16:31:57 -0800180 auto server = builder.BuildAndStart();
Craig Tiller056ba542015-01-31 21:15:10 -0800181
182 while (!got_sigint) {
vpai80b6d012014-12-17 11:47:32 -0800183 std::this_thread::sleep_for(std::chrono::seconds(5));
184 }
vpai80b6d012014-12-17 11:47:32 -0800185}
186
187int main(int argc, char** argv) {
188 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100189 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800190
Craig Tiller056ba542015-01-31 21:15:10 -0800191 signal(SIGINT, sigint_handler);
Craig Tiller06059952015-02-18 08:34:56 -0800192
vpai80b6d012014-12-17 11:47:32 -0800193 GPR_ASSERT(FLAGS_port != 0);
vpai80b6d012014-12-17 11:47:32 -0800194 RunServer();
195
196 grpc_shutdown();
197 return 0;
198}