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 | |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 34 | #include <sys/time.h> |
| 35 | #include <sys/resource.h> |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 36 | #include <sys/signal.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 37 | #include <thread> |
| 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> |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame^] | 46 | #include <grpc++/server_credentials.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 47 | #include <grpc++/status.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" |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 51 | |
| 52 | #include <grpc/grpc.h> |
| 53 | #include <grpc/support/log.h> |
| 54 | |
| 55 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
| 56 | DEFINE_int32(port, 0, "Server port."); |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 57 | DEFINE_int32(server_threads, 4, "Number of server threads."); |
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; |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 62 | using grpc::ThreadPool; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 63 | using grpc::testing::Payload; |
| 64 | using grpc::testing::PayloadType; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 65 | using grpc::testing::ServerStats; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 66 | using grpc::testing::SimpleRequest; |
| 67 | using grpc::testing::SimpleResponse; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 68 | using grpc::testing::StatsRequest; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 69 | using grpc::testing::TestService; |
| 70 | using grpc::Status; |
| 71 | |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 72 | // In some distros, gflags is in the namespace google, and in some others, |
| 73 | // in gflags. This hack is enabling us to find both. |
| 74 | namespace google { } |
| 75 | namespace gflags { } |
| 76 | using namespace google; |
| 77 | using namespace gflags; |
| 78 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 79 | static bool got_sigint = false; |
| 80 | |
| 81 | static void sigint_handler(int x) { got_sigint = 1; } |
| 82 | |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 83 | static double time_double(struct timeval* tv) { |
| 84 | return tv->tv_sec + 1e-6 * tv->tv_usec; |
| 85 | } |
| 86 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 87 | static bool SetPayload(PayloadType type, int size, Payload* payload) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 88 | PayloadType response_type = type; |
| 89 | // TODO(yangg): Support UNCOMPRESSABLE payload. |
| 90 | if (type != PayloadType::COMPRESSABLE) { |
| 91 | return false; |
| 92 | } |
| 93 | payload->set_type(response_type); |
| 94 | std::unique_ptr<char[]> body(new char[size]()); |
| 95 | payload->set_body(body.get(), size); |
| 96 | return true; |
| 97 | } |
| 98 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 99 | namespace { |
| 100 | |
| 101 | class TestServiceImpl final : public TestService::Service { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 102 | public: |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 103 | Status CollectServerStats(ServerContext* context, const StatsRequest*, |
| 104 | ServerStats* response) { |
| 105 | struct rusage usage; |
| 106 | struct timeval tv; |
| 107 | gettimeofday(&tv, NULL); |
| 108 | getrusage(RUSAGE_SELF, &usage); |
| 109 | response->set_time_now(time_double(&tv)); |
| 110 | response->set_time_user(time_double(&usage.ru_utime)); |
| 111 | response->set_time_system(time_double(&usage.ru_stime)); |
| 112 | return Status::OK; |
| 113 | } |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 114 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
| 115 | SimpleResponse* response) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 116 | if (request->has_response_size() && request->response_size() > 0) { |
| 117 | if (!SetPayload(request->response_type(), request->response_size(), |
| 118 | response->mutable_payload())) { |
| 119 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 120 | } |
| 121 | } |
| 122 | return Status::OK; |
| 123 | } |
| 124 | }; |
| 125 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 126 | } // namespace |
| 127 | |
| 128 | static void RunServer() { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 129 | char* server_address = NULL; |
| 130 | gpr_join_host_port(&server_address, "::", FLAGS_port); |
| 131 | |
| 132 | TestServiceImpl service; |
| 133 | |
| 134 | SimpleRequest request; |
| 135 | SimpleResponse response; |
| 136 | |
| 137 | ServerBuilder builder; |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame^] | 138 | builder.AddPort(server_address, grpc::InsecureServerCredentials()); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 139 | builder.RegisterService(&service); |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 140 | |
Vijay Pai | 4ca479c | 2015-02-10 10:55:53 -0800 | [diff] [blame] | 141 | std::unique_ptr<ThreadPool> pool(new ThreadPool(FLAGS_server_threads)); |
| 142 | builder.SetThreadPool(pool.get()); |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 143 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 144 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 145 | gpr_log(GPR_INFO, "Server listening on %s\n", server_address); |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 146 | |
| 147 | grpc_profiler_start("qps_server.prof"); |
| 148 | |
| 149 | while (!got_sigint) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 150 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 151 | } |
| 152 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 153 | grpc_profiler_stop(); |
| 154 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 155 | gpr_free(server_address); |
| 156 | } |
| 157 | |
| 158 | int main(int argc, char** argv) { |
| 159 | grpc_init(); |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 160 | ParseCommandLineFlags(&argc, &argv, true); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 161 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 162 | signal(SIGINT, sigint_handler); |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 163 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 164 | GPR_ASSERT(FLAGS_port != 0); |
| 165 | GPR_ASSERT(!FLAGS_enable_ssl); |
| 166 | RunServer(); |
| 167 | |
| 168 | grpc_shutdown(); |
| 169 | return 0; |
| 170 | } |