| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * | 
 | 3 |  * Copyright 2014, Google Inc. | 
 | 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 |  | 
 | 39 | #include <google/gflags.h> | 
 | 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 | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 47 | #include "test/core/util/grpc_profiler.h" | 
| vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 48 | #include "test/cpp/qps/qpstest.pb.h" | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 49 |  | 
 | 50 | #include <grpc/grpc.h> | 
 | 51 | #include <grpc/support/log.h> | 
 | 52 |  | 
 | 53 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); | 
 | 54 | DEFINE_int32(port, 0, "Server port."); | 
 | 55 |  | 
 | 56 | using grpc::Server; | 
 | 57 | using grpc::ServerBuilder; | 
| yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 58 | using grpc::ServerContext; | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 59 | using grpc::testing::Payload; | 
 | 60 | using grpc::testing::PayloadType; | 
| vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 61 | using grpc::testing::ServerStats; | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 62 | using grpc::testing::SimpleRequest; | 
 | 63 | using grpc::testing::SimpleResponse; | 
| vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 64 | using grpc::testing::StatsRequest; | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 65 | using grpc::testing::TestService; | 
 | 66 | using grpc::Status; | 
 | 67 |  | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 68 | static bool got_sigint = false; | 
 | 69 |  | 
 | 70 | static void sigint_handler(int x) { got_sigint = 1; } | 
 | 71 |  | 
| vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 72 | static double time_double(struct timeval* tv) { | 
 | 73 |   return tv->tv_sec + 1e-6 * tv->tv_usec; | 
 | 74 | } | 
 | 75 |  | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 76 | static bool SetPayload(PayloadType type, int size, Payload* payload) { | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 77 |   PayloadType response_type = type; | 
 | 78 |   // TODO(yangg): Support UNCOMPRESSABLE payload. | 
 | 79 |   if (type != PayloadType::COMPRESSABLE) { | 
 | 80 |     return false; | 
 | 81 |   } | 
 | 82 |   payload->set_type(response_type); | 
 | 83 |   std::unique_ptr<char[]> body(new char[size]()); | 
 | 84 |   payload->set_body(body.get(), size); | 
 | 85 |   return true; | 
 | 86 | } | 
 | 87 |  | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 88 | namespace { | 
 | 89 |  | 
 | 90 | class TestServiceImpl final : public TestService::Service { | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 91 |  public: | 
| vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 92 |   Status CollectServerStats(ServerContext* context, const StatsRequest*, | 
 | 93 |                             ServerStats* response) { | 
 | 94 |     struct rusage usage; | 
 | 95 |     struct timeval tv; | 
 | 96 |     gettimeofday(&tv, NULL); | 
 | 97 |     getrusage(RUSAGE_SELF, &usage); | 
 | 98 |     response->set_time_now(time_double(&tv)); | 
 | 99 |     response->set_time_user(time_double(&usage.ru_utime)); | 
 | 100 |     response->set_time_system(time_double(&usage.ru_stime)); | 
 | 101 |     return Status::OK; | 
 | 102 |   } | 
| yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 103 |   Status UnaryCall(ServerContext* context, const SimpleRequest* request, | 
 | 104 |                    SimpleResponse* response) { | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 105 |     if (request->has_response_size() && request->response_size() > 0) { | 
 | 106 |       if (!SetPayload(request->response_type(), request->response_size(), | 
 | 107 |                       response->mutable_payload())) { | 
 | 108 |         return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); | 
 | 109 |       } | 
 | 110 |     } | 
 | 111 |     return Status::OK; | 
 | 112 |   } | 
 | 113 | }; | 
 | 114 |  | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 115 | }  // namespace | 
 | 116 |  | 
 | 117 | static void RunServer() { | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 118 |   char* server_address = NULL; | 
 | 119 |   gpr_join_host_port(&server_address, "::", FLAGS_port); | 
 | 120 |  | 
 | 121 |   TestServiceImpl service; | 
 | 122 |  | 
 | 123 |   SimpleRequest request; | 
 | 124 |   SimpleResponse response; | 
 | 125 |  | 
 | 126 |   ServerBuilder builder; | 
 | 127 |   builder.AddPort(server_address); | 
 | 128 |   builder.RegisterService(service.service()); | 
 | 129 |   std::unique_ptr<Server> server(builder.BuildAndStart()); | 
 | 130 |   gpr_log(GPR_INFO, "Server listening on %s\n", server_address); | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 131 |  | 
 | 132 |   grpc_profiler_start("qps_server.prof"); | 
 | 133 |  | 
 | 134 |   while (!got_sigint) { | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 135 |     std::this_thread::sleep_for(std::chrono::seconds(5)); | 
 | 136 |   } | 
 | 137 |  | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 138 |   grpc_profiler_stop(); | 
 | 139 |  | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 140 |   gpr_free(server_address); | 
 | 141 | } | 
 | 142 |  | 
 | 143 | int main(int argc, char** argv) { | 
 | 144 |   grpc_init(); | 
 | 145 |   google::ParseCommandLineFlags(&argc, &argv, true); | 
 | 146 |  | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 147 |   signal(SIGINT, sigint_handler); | 
 | 148 |    | 
| vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 149 |   GPR_ASSERT(FLAGS_port != 0); | 
 | 150 |   GPR_ASSERT(!FLAGS_enable_ssl); | 
 | 151 |   RunServer(); | 
 | 152 |  | 
 | 153 |   grpc_shutdown(); | 
 | 154 |   return 0; | 
 | 155 | } | 
| Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 156 |  |