blob: be27c12b30a701b1c2537df3ad56ffdb49e6ac1b [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
Craig Tillercf133f42015-02-26 14:05:56 -080039#include <unistd.h>
40
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010041#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080042#include <grpc/support/alloc.h>
43#include <grpc/support/host_port.h>
44#include <grpc++/config.h>
45#include <grpc++/server.h>
46#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080047#include <grpc++/server_context.h>
vpai80b6d012014-12-17 11:47:32 -080048#include <grpc++/status.h>
Vijay Paic3b02d92015-02-10 10:39:03 -080049#include "src/cpp/server/thread_pool.h"
Craig Tiller056ba542015-01-31 21:15:10 -080050#include "test/core/util/grpc_profiler.h"
vpai92fe70e2015-01-13 11:21:38 -080051#include "test/cpp/qps/qpstest.pb.h"
vpai80b6d012014-12-17 11:47:32 -080052
53#include <grpc/grpc.h>
54#include <grpc/support/log.h>
55
56DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
57DEFINE_int32(port, 0, "Server port.");
Vijay Paic3b02d92015-02-10 10:39:03 -080058DEFINE_int32(server_threads, 4, "Number of server threads.");
vpai80b6d012014-12-17 11:47:32 -080059
60using grpc::Server;
61using grpc::ServerBuilder;
yangga4b6f5d2014-12-17 15:53:12 -080062using grpc::ServerContext;
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;
71using grpc::Status;
72
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010073// In some distros, gflags is in the namespace google, and in some others,
74// in gflags. This hack is enabling us to find both.
75namespace google { }
76namespace gflags { }
77using namespace google;
78using namespace gflags;
79
Craig Tiller056ba542015-01-31 21:15:10 -080080static bool got_sigint = false;
81
82static void sigint_handler(int x) { got_sigint = 1; }
83
vpai92fe70e2015-01-13 11:21:38 -080084static double time_double(struct timeval* tv) {
85 return tv->tv_sec + 1e-6 * tv->tv_usec;
86}
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:
vpai92fe70e2015-01-13 11:21:38 -0800104 Status CollectServerStats(ServerContext* context, const StatsRequest*,
105 ServerStats* response) {
106 struct rusage usage;
107 struct timeval tv;
108 gettimeofday(&tv, NULL);
109 getrusage(RUSAGE_SELF, &usage);
110 response->set_time_now(time_double(&tv));
111 response->set_time_user(time_double(&usage.ru_utime));
112 response->set_time_system(time_double(&usage.ru_stime));
113 return Status::OK;
114 }
yangga4b6f5d2014-12-17 15:53:12 -0800115 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
116 SimpleResponse* response) {
vpai80b6d012014-12-17 11:47:32 -0800117 if (request->has_response_size() && request->response_size() > 0) {
118 if (!SetPayload(request->response_type(), request->response_size(),
119 response->mutable_payload())) {
120 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
121 }
122 }
123 return Status::OK;
124 }
125};
126
Craig Tiller056ba542015-01-31 21:15:10 -0800127} // namespace
128
129static void RunServer() {
vpai80b6d012014-12-17 11:47:32 -0800130 char* server_address = NULL;
131 gpr_join_host_port(&server_address, "::", FLAGS_port);
132
133 TestServiceImpl service;
134
135 SimpleRequest request;
136 SimpleResponse response;
137
138 ServerBuilder builder;
139 builder.AddPort(server_address);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800140 builder.RegisterService(&service);
Vijay Paic3b02d92015-02-10 10:39:03 -0800141
Vijay Pai4ca479c2015-02-10 10:55:53 -0800142 std::unique_ptr<ThreadPool> pool(new ThreadPool(FLAGS_server_threads));
143 builder.SetThreadPool(pool.get());
Vijay Paic3b02d92015-02-10 10:39:03 -0800144
vpai80b6d012014-12-17 11:47:32 -0800145 std::unique_ptr<Server> server(builder.BuildAndStart());
146 gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
Craig Tiller056ba542015-01-31 21:15:10 -0800147
148 grpc_profiler_start("qps_server.prof");
149
150 while (!got_sigint) {
Craig Tillercf133f42015-02-26 14:05:56 -0800151 sleep(5);
vpai80b6d012014-12-17 11:47:32 -0800152 }
153
Craig Tiller056ba542015-01-31 21:15:10 -0800154 grpc_profiler_stop();
155
vpai80b6d012014-12-17 11:47:32 -0800156 gpr_free(server_address);
157}
158
159int main(int argc, char** argv) {
160 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100161 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800162
Craig Tiller056ba542015-01-31 21:15:10 -0800163 signal(SIGINT, sigint_handler);
Craig Tiller06059952015-02-18 08:34:56 -0800164
vpai80b6d012014-12-17 11:47:32 -0800165 GPR_ASSERT(FLAGS_port != 0);
166 GPR_ASSERT(!FLAGS_enable_ssl);
167 RunServer();
168
169 grpc_shutdown();
170 return 0;
171}