vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, 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 | |
| 34 | #include <forward_list> |
| 35 | #include <functional> |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/resource.h> |
| 38 | #include <sys/signal.h> |
| 39 | #include <thread> |
| 40 | |
| 41 | #include <gflags/gflags.h> |
| 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/host_port.h> |
| 44 | #include <grpc++/async_unary_call.h> |
| 45 | #include <grpc++/config.h> |
| 46 | #include <grpc++/server.h> |
| 47 | #include <grpc++/server_builder.h> |
| 48 | #include <grpc++/server_context.h> |
Craig Tiller | 61691f9 | 2015-03-02 08:51:52 -0800 | [diff] [blame^] | 49 | #include <grpc++/server_credentials.h> |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 50 | #include <grpc++/status.h> |
| 51 | #include <gtest/gtest.h> |
| 52 | #include "src/cpp/server/thread_pool.h" |
| 53 | #include "test/core/util/grpc_profiler.h" |
| 54 | #include "test/cpp/qps/qpstest.pb.h" |
| 55 | |
| 56 | #include <grpc/grpc.h> |
| 57 | #include <grpc/support/log.h> |
| 58 | |
| 59 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
| 60 | DEFINE_int32(port, 0, "Server port."); |
| 61 | DEFINE_int32(server_threads, 4, "Number of server threads."); |
| 62 | |
| 63 | using grpc::CompletionQueue; |
Craig Tiller | 61691f9 | 2015-03-02 08:51:52 -0800 | [diff] [blame^] | 64 | using grpc::InsecureServerCredentials; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 65 | using grpc::Server; |
| 66 | using grpc::ServerBuilder; |
| 67 | using grpc::ServerContext; |
| 68 | using grpc::ThreadPool; |
| 69 | using grpc::testing::Payload; |
| 70 | using grpc::testing::PayloadType; |
| 71 | using grpc::testing::ServerStats; |
| 72 | using grpc::testing::SimpleRequest; |
| 73 | using grpc::testing::SimpleResponse; |
| 74 | using grpc::testing::StatsRequest; |
| 75 | using grpc::testing::TestService; |
| 76 | using grpc::Status; |
| 77 | |
| 78 | // In some distros, gflags is in the namespace google, and in some others, |
| 79 | // in gflags. This hack is enabling us to find both. |
| 80 | namespace google {} |
| 81 | namespace gflags {} |
| 82 | using namespace google; |
| 83 | using namespace gflags; |
| 84 | |
| 85 | static bool got_sigint = false; |
| 86 | |
| 87 | static void sigint_handler(int x) { got_sigint = 1; } |
| 88 | |
| 89 | static double time_double(struct timeval *tv) { |
| 90 | return tv->tv_sec + 1e-6 * tv->tv_usec; |
| 91 | } |
| 92 | |
| 93 | static bool SetPayload(PayloadType type, int size, Payload *payload) { |
| 94 | PayloadType response_type = type; |
| 95 | // TODO(yangg): Support UNCOMPRESSABLE payload. |
| 96 | if (type != PayloadType::COMPRESSABLE) { |
| 97 | return false; |
| 98 | } |
| 99 | payload->set_type(response_type); |
| 100 | std::unique_ptr<char[]> body(new char[size]()); |
| 101 | payload->set_body(body.get(), size); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | namespace { |
| 106 | |
| 107 | class AsyncQpsServerTest { |
| 108 | public: |
| 109 | AsyncQpsServerTest() : srv_cq_(), async_service_(&srv_cq_), server_(nullptr) { |
| 110 | char *server_address = NULL; |
| 111 | gpr_join_host_port(&server_address, "::", FLAGS_port); |
| 112 | |
| 113 | ServerBuilder builder; |
Craig Tiller | 61691f9 | 2015-03-02 08:51:52 -0800 | [diff] [blame^] | 114 | builder.AddPort(server_address, InsecureServerCredentials()); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 115 | |
| 116 | builder.RegisterAsyncService(&async_service_); |
| 117 | |
| 118 | server_ = builder.BuildAndStart(); |
| 119 | gpr_log(GPR_INFO, "Server listening on %s\n", server_address); |
| 120 | gpr_free(server_address); |
| 121 | |
| 122 | using namespace std::placeholders; |
| 123 | request_unary_ = std::bind(&TestService::AsyncService::RequestUnaryCall, |
| 124 | &async_service_, _1, _2, _3, &srv_cq_, _4); |
| 125 | request_stats_ = |
| 126 | std::bind(&TestService::AsyncService::RequestCollectServerStats, |
| 127 | &async_service_, _1, _2, _3, &srv_cq_, _4); |
| 128 | for (int i = 0; i < 100; i++) { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 129 | contexts_.push_front( |
| 130 | new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>( |
| 131 | request_unary_, UnaryCall)); |
| 132 | contexts_.push_front( |
| 133 | new ServerRpcContextUnaryImpl<StatsRequest, ServerStats>( |
| 134 | request_stats_, CollectServerStats)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | ~AsyncQpsServerTest() { |
| 138 | server_->Shutdown(); |
| 139 | void *ignored_tag; |
| 140 | bool ignored_ok; |
| 141 | srv_cq_.Shutdown(); |
| 142 | while (srv_cq_.Next(&ignored_tag, &ignored_ok)) { |
| 143 | } |
| 144 | while (!contexts_.empty()) { |
| 145 | delete contexts_.front(); |
| 146 | contexts_.pop_front(); |
| 147 | } |
| 148 | } |
| 149 | void ServeRpcs(int num_threads) { |
| 150 | std::vector<std::thread> threads; |
| 151 | for (int i = 0; i < num_threads; i++) { |
| 152 | threads.push_back(std::thread([=]() { |
| 153 | // Wait until work is available or we are shutting down |
| 154 | bool ok; |
| 155 | void *got_tag; |
| 156 | while (srv_cq_.Next(&got_tag, &ok)) { |
| 157 | EXPECT_EQ(ok, true); |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 158 | ServerRpcContext *ctx = detag(got_tag); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 159 | // The tag is a pointer to an RPC context to invoke |
vjpai | 6e2e64a | 2015-02-27 09:28:28 -0800 | [diff] [blame] | 160 | if (ctx->RunNextState() == false) { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 161 | // this RPC context is done, so refresh it |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 162 | ctx->Reset(); |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 163 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 164 | } |
| 165 | return; |
| 166 | })); |
| 167 | } |
| 168 | while (!got_sigint) { |
| 169 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 170 | } |
| 171 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 172 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 173 | private: |
| 174 | class ServerRpcContext { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 175 | public: |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 176 | ServerRpcContext() {} |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 177 | virtual ~ServerRpcContext(){}; |
vjpai | 6e2e64a | 2015-02-27 09:28:28 -0800 | [diff] [blame] | 178 | virtual bool RunNextState() = 0;// do next state, return false if all done |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 179 | virtual void Reset() = 0; // start this back at a clean state |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 180 | }; |
| 181 | static void *tag(ServerRpcContext *func) { |
| 182 | return reinterpret_cast<void *>(func); |
| 183 | } |
| 184 | static ServerRpcContext *detag(void *tag) { |
| 185 | return reinterpret_cast<ServerRpcContext *>(tag); |
| 186 | } |
| 187 | |
| 188 | template <class RequestType, class ResponseType> |
| 189 | class ServerRpcContextUnaryImpl : public ServerRpcContext { |
| 190 | public: |
| 191 | ServerRpcContextUnaryImpl( |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 192 | std::function<void(ServerContext *, RequestType *, |
| 193 | grpc::ServerAsyncResponseWriter<ResponseType> *, |
| 194 | void *)> request_method, |
| 195 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 196 | invoke_method) |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 197 | : next_state_(&ServerRpcContextUnaryImpl::invoker), |
| 198 | request_method_(request_method), |
| 199 | invoke_method_(invoke_method), |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 200 | response_writer_(&srv_ctx_) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 201 | request_method_(&srv_ctx_, &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 202 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 203 | } |
vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 204 | ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
vjpai | 6e2e64a | 2015-02-27 09:28:28 -0800 | [diff] [blame] | 205 | bool RunNextState() GRPC_OVERRIDE { return (this->*next_state_)(); } |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 206 | void Reset() GRPC_OVERRIDE { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 207 | srv_ctx_ = ServerContext(); |
| 208 | req_ = RequestType(); |
| 209 | response_writer_ = |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 210 | grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 211 | |
| 212 | // Then request the method |
| 213 | next_state_ = &ServerRpcContextUnaryImpl::invoker; |
| 214 | request_method_(&srv_ctx_, &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 215 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 216 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 217 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 218 | private: |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 219 | bool finisher() { return false; } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 220 | bool invoker() { |
| 221 | ResponseType response; |
| 222 | |
| 223 | // Call the RPC processing function |
| 224 | grpc::Status status = invoke_method_(&req_, &response); |
| 225 | |
| 226 | // Have the response writer work and invoke on_finish when done |
| 227 | next_state_ = &ServerRpcContextUnaryImpl::finisher; |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 228 | response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | ServerContext srv_ctx_; |
| 232 | RequestType req_; |
| 233 | bool (ServerRpcContextUnaryImpl::*next_state_)(); |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 234 | std::function<void(ServerContext *, RequestType *, |
| 235 | grpc::ServerAsyncResponseWriter<ResponseType> *, void *)> |
| 236 | request_method_; |
| 237 | std::function<grpc::Status(const RequestType *, ResponseType *)> |
| 238 | invoke_method_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 239 | grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 240 | }; |
| 241 | |
| 242 | static Status CollectServerStats(const StatsRequest *, |
| 243 | ServerStats *response) { |
| 244 | struct rusage usage; |
| 245 | struct timeval tv; |
| 246 | gettimeofday(&tv, NULL); |
| 247 | getrusage(RUSAGE_SELF, &usage); |
| 248 | response->set_time_now(time_double(&tv)); |
| 249 | response->set_time_user(time_double(&usage.ru_utime)); |
| 250 | response->set_time_system(time_double(&usage.ru_stime)); |
| 251 | return Status::OK; |
| 252 | } |
| 253 | static Status UnaryCall(const SimpleRequest *request, |
| 254 | SimpleResponse *response) { |
| 255 | if (request->has_response_size() && request->response_size() > 0) { |
| 256 | if (!SetPayload(request->response_type(), request->response_size(), |
| 257 | response->mutable_payload())) { |
| 258 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 259 | } |
| 260 | } |
| 261 | return Status::OK; |
| 262 | } |
| 263 | CompletionQueue srv_cq_; |
| 264 | TestService::AsyncService async_service_; |
| 265 | std::unique_ptr<Server> server_; |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 266 | std::function<void(ServerContext *, SimpleRequest *, |
| 267 | grpc::ServerAsyncResponseWriter<SimpleResponse> *, void *)> |
| 268 | request_unary_; |
| 269 | std::function<void(ServerContext *, StatsRequest *, |
| 270 | grpc::ServerAsyncResponseWriter<ServerStats> *, void *)> |
| 271 | request_stats_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 272 | std::forward_list<ServerRpcContext *> contexts_; |
| 273 | }; |
| 274 | |
| 275 | } // namespace |
| 276 | |
| 277 | static void RunServer() { |
| 278 | AsyncQpsServerTest server; |
| 279 | |
| 280 | grpc_profiler_start("qps_server_async.prof"); |
| 281 | |
| 282 | server.ServeRpcs(FLAGS_server_threads); |
| 283 | |
| 284 | grpc_profiler_stop(); |
| 285 | } |
| 286 | |
| 287 | int main(int argc, char **argv) { |
| 288 | grpc_init(); |
| 289 | ParseCommandLineFlags(&argc, &argv, true); |
| 290 | GPR_ASSERT(FLAGS_port != 0); |
| 291 | GPR_ASSERT(!FLAGS_enable_ssl); |
| 292 | |
| 293 | signal(SIGINT, sigint_handler); |
| 294 | |
| 295 | RunServer(); |
| 296 | |
| 297 | grpc_shutdown(); |
| 298 | google::protobuf::ShutdownProtobufLibrary(); |
| 299 | |
| 300 | return 0; |
| 301 | } |