vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 4 | * 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 Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 34 | #include <sys/signal.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 35 | #include <thread> |
| 36 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 39 | #include <gflags/gflags.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 40 | #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> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 45 | #include <grpc++/server_context.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 46 | #include <grpc++/status.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 47 | #include <grpc++/stream.h> |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 48 | #include "src/cpp/server/thread_pool.h" |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 49 | #include "test/core/util/grpc_profiler.h" |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 50 | #include "test/cpp/qps/qpstest.pb.h" |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 51 | #include "test/cpp/qps/timer.h" |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 52 | |
| 53 | #include <grpc/grpc.h> |
| 54 | #include <grpc/support/log.h> |
| 55 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 56 | DEFINE_int32(port, 0, "Server port."); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 57 | DEFINE_int32(driver_port, 0, "Server driver port."); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 58 | |
| 59 | using grpc::Server; |
| 60 | using grpc::ServerBuilder; |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 61 | using grpc::ServerContext; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 62 | using grpc::ServerReaderWriter; |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 63 | using grpc::ThreadPool; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 64 | using grpc::testing::Payload; |
| 65 | using grpc::testing::PayloadType; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 66 | using grpc::testing::ServerStats; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 67 | using grpc::testing::SimpleRequest; |
| 68 | using grpc::testing::SimpleResponse; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 69 | using grpc::testing::StatsRequest; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 70 | using grpc::testing::TestService; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 71 | using grpc::testing::QpsServer; |
| 72 | using grpc::testing::ServerArgs; |
| 73 | using grpc::testing::ServerStats; |
| 74 | using grpc::testing::ServerStatus; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 75 | using grpc::Status; |
| 76 | |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 77 | // In some distros, gflags is in the namespace google, and in some others, |
| 78 | // in gflags. This hack is enabling us to find both. |
| 79 | namespace google { } |
| 80 | namespace gflags { } |
| 81 | using namespace google; |
| 82 | using namespace gflags; |
| 83 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 84 | static bool got_sigint = false; |
| 85 | |
| 86 | static void sigint_handler(int x) { got_sigint = 1; } |
| 87 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 88 | static bool SetPayload(PayloadType type, int size, Payload* payload) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 89 | 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 Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 100 | namespace { |
| 101 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 102 | class TestServiceImpl GRPC_FINAL : public TestService::Service { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 103 | public: |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 104 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 105 | SimpleResponse* response) override { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 106 | 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 Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 116 | } // namespace |
| 117 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 118 | class ServerImpl : public QpsServer::Service { |
| 119 | public: |
| 120 | Status RunServer(ServerContext* ctx, ServerReaderWriter<ServerStatus, ServerArgs>* stream) { |
| 121 | ServerArgs args; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 122 | if (!stream->Read(&args)) return Status::OK; |
| 123 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 124 | std::lock_guard<std::mutex> lock(server_mu_); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 125 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 126 | char* server_address = NULL; |
| 127 | gpr_join_host_port(&server_address, "::", FLAGS_port); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 128 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 129 | TestServiceImpl service; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 130 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 131 | ServerBuilder builder; |
| 132 | builder.AddPort(server_address); |
| 133 | builder.RegisterService(&service); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 134 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 135 | std::unique_ptr<ThreadPool> pool(new ThreadPool(args.config().threads())); |
| 136 | builder.SetThreadPool(pool.get()); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 137 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 138 | auto server = builder.BuildAndStart(); |
| 139 | gpr_log(GPR_INFO, "Server listening on %s\n", server_address); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 140 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 141 | gpr_free(server_address); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 142 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 143 | ServerStatus status; |
| 144 | status.set_port(FLAGS_port); |
| 145 | if (!stream->Write(status)) return Status(grpc::UNKNOWN); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 146 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 147 | grpc_profiler_start("qps_server.prof"); |
| 148 | Timer timer; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 149 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 150 | if (stream->Read(&args)) { |
| 151 | gpr_log(GPR_ERROR, "Got a server request, but not expecting one"); |
| 152 | return Status(grpc::UNKNOWN); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 155 | 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 Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 163 | return Status::OK; |
| 164 | } |
| 165 | |
| 166 | private: |
| 167 | std::mutex server_mu_; |
| 168 | }; |
| 169 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 170 | static void RunServer() { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 171 | char* server_address = NULL; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 172 | gpr_join_host_port(&server_address, "::", FLAGS_driver_port); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 173 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 174 | ServerImpl service; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 175 | |
| 176 | ServerBuilder builder; |
| 177 | builder.AddPort(server_address); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 178 | builder.RegisterService(&service); |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 179 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 180 | gpr_free(server_address); |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 181 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 182 | auto server = builder.BuildAndStart(); |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 183 | |
| 184 | while (!got_sigint) { |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 185 | sleep(5); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 186 | } |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | int main(int argc, char** argv) { |
Craig Tiller | 8ecb170 | 2015-02-24 17:08:23 -0800 | [diff] [blame] | 190 | signal(SIGINT, sigint_handler); |
| 191 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 192 | grpc_init(); |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 193 | ParseCommandLineFlags(&argc, &argv, true); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 194 | |
| 195 | GPR_ASSERT(FLAGS_port != 0); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 196 | RunServer(); |
| 197 | |
| 198 | grpc_shutdown(); |
| 199 | return 0; |
| 200 | } |