blob: 8e136349a15e490ede14dc1a913258d403140372 [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>
Vijay Paic3b02d92015-02-10 10:39:03 -080047#include "src/cpp/server/thread_pool.h"
Craig Tiller056ba542015-01-31 21:15:10 -080048#include "test/core/util/grpc_profiler.h"
vpai92fe70e2015-01-13 11:21:38 -080049#include "test/cpp/qps/qpstest.pb.h"
vpai80b6d012014-12-17 11:47:32 -080050
51#include <grpc/grpc.h>
52#include <grpc/support/log.h>
53
54DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
55DEFINE_int32(port, 0, "Server port.");
Vijay Paic3b02d92015-02-10 10:39:03 -080056DEFINE_int32(server_threads, 4, "Number of server threads.");
vpai80b6d012014-12-17 11:47:32 -080057
58using grpc::Server;
59using grpc::ServerBuilder;
yangga4b6f5d2014-12-17 15:53:12 -080060using grpc::ServerContext;
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;
69using grpc::Status;
70
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010071// In some distros, gflags is in the namespace google, and in some others,
72// in gflags. This hack is enabling us to find both.
73namespace google { }
74namespace gflags { }
75using namespace google;
76using namespace gflags;
77
Craig Tiller056ba542015-01-31 21:15:10 -080078static bool got_sigint = false;
79
80static void sigint_handler(int x) { got_sigint = 1; }
81
vpai92fe70e2015-01-13 11:21:38 -080082static double time_double(struct timeval* tv) {
83 return tv->tv_sec + 1e-6 * tv->tv_usec;
84}
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:
vpai92fe70e2015-01-13 11:21:38 -0800102 Status CollectServerStats(ServerContext* context, const StatsRequest*,
103 ServerStats* response) {
104 struct rusage usage;
105 struct timeval tv;
106 gettimeofday(&tv, NULL);
107 getrusage(RUSAGE_SELF, &usage);
108 response->set_time_now(time_double(&tv));
109 response->set_time_user(time_double(&usage.ru_utime));
110 response->set_time_system(time_double(&usage.ru_stime));
111 return Status::OK;
112 }
yangga4b6f5d2014-12-17 15:53:12 -0800113 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
114 SimpleResponse* response) {
vpai80b6d012014-12-17 11:47:32 -0800115 if (request->has_response_size() && request->response_size() > 0) {
116 if (!SetPayload(request->response_type(), request->response_size(),
117 response->mutable_payload())) {
118 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
119 }
120 }
121 return Status::OK;
122 }
123};
124
Craig Tiller056ba542015-01-31 21:15:10 -0800125} // namespace
126
127static void RunServer() {
vpai80b6d012014-12-17 11:47:32 -0800128 char* server_address = NULL;
129 gpr_join_host_port(&server_address, "::", FLAGS_port);
130
131 TestServiceImpl service;
132
133 SimpleRequest request;
134 SimpleResponse response;
135
136 ServerBuilder builder;
137 builder.AddPort(server_address);
Craig Tillerf8ac5d82015-02-09 16:24:20 -0800138 builder.RegisterService(&service);
Vijay Paic3b02d92015-02-10 10:39:03 -0800139
Vijay Pai4ca479c2015-02-10 10:55:53 -0800140 std::unique_ptr<ThreadPool> pool(new ThreadPool(FLAGS_server_threads));
141 builder.SetThreadPool(pool.get());
Vijay Paic3b02d92015-02-10 10:39:03 -0800142
vpai80b6d012014-12-17 11:47:32 -0800143 std::unique_ptr<Server> server(builder.BuildAndStart());
144 gpr_log(GPR_INFO, "Server listening on %s\n", server_address);
Craig Tiller056ba542015-01-31 21:15:10 -0800145
146 grpc_profiler_start("qps_server.prof");
147
148 while (!got_sigint) {
vpai80b6d012014-12-17 11:47:32 -0800149 std::this_thread::sleep_for(std::chrono::seconds(5));
150 }
151
Craig Tiller056ba542015-01-31 21:15:10 -0800152 grpc_profiler_stop();
153
vpai80b6d012014-12-17 11:47:32 -0800154 gpr_free(server_address);
155}
156
157int main(int argc, char** argv) {
158 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100159 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800160
Craig Tiller056ba542015-01-31 21:15:10 -0800161 signal(SIGINT, sigint_handler);
Craig Tiller06059952015-02-18 08:34:56 -0800162
vpai80b6d012014-12-17 11:47:32 -0800163 GPR_ASSERT(FLAGS_port != 0);
164 GPR_ASSERT(!FLAGS_enable_ssl);
165 RunServer();
166
167 grpc_shutdown();
168 return 0;
169}