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" |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 55 | #include "test/cpp/qps/server.h" |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 56 | |
| 57 | #include <grpc/grpc.h> |
| 58 | #include <grpc/support/log.h> |
| 59 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 60 | namespace grpc { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 61 | namespace testing { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 62 | |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 63 | class AsyncQpsServerTest : public Server { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 64 | public: |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 65 | AsyncQpsServerTest(const ServerConfig& config, int port) |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 66 | : srv_cq_(), async_service_(&srv_cq_), server_(nullptr) { |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 67 | char* server_address = NULL; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 68 | gpr_join_host_port(&server_address, "::", port); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 69 | |
| 70 | ServerBuilder builder; |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 71 | builder.AddListeningPort(server_address, InsecureServerCredentials()); |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 72 | gpr_free(server_address); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 73 | |
| 74 | builder.RegisterAsyncService(&async_service_); |
| 75 | |
| 76 | server_ = builder.BuildAndStart(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 77 | |
| 78 | using namespace std::placeholders; |
| 79 | request_unary_ = std::bind(&TestService::AsyncService::RequestUnaryCall, |
| 80 | &async_service_, _1, _2, _3, &srv_cq_, _4); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 81 | for (int i = 0; i < 100; i++) { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 82 | contexts_.push_front( |
| 83 | new ServerRpcContextUnaryImpl<SimpleRequest, SimpleResponse>( |
| 84 | request_unary_, UnaryCall)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 85 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 86 | for (int i = 0; i < config.threads(); i++) { |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 87 | threads_.push_back(std::thread([=]() { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 88 | // Wait until work is available or we are shutting down |
| 89 | bool ok; |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 90 | void* got_tag; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 91 | while (srv_cq_.Next(&got_tag, &ok)) { |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 92 | if (ok) { |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 93 | ServerRpcContext* ctx = detag(got_tag); |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 94 | // The tag is a pointer to an RPC context to invoke |
| 95 | if (ctx->RunNextState() == false) { |
| 96 | // this RPC context is done, so refresh it |
| 97 | ctx->Reset(); |
| 98 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 99 | } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 100 | } |
| 101 | return; |
| 102 | })); |
| 103 | } |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 104 | } |
| 105 | ~AsyncQpsServerTest() { |
| 106 | server_->Shutdown(); |
| 107 | srv_cq_.Shutdown(); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 108 | for (auto& thr : threads_) { |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 109 | thr.join(); |
| 110 | } |
| 111 | while (!contexts_.empty()) { |
| 112 | delete contexts_.front(); |
| 113 | contexts_.pop_front(); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 116 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 117 | private: |
| 118 | class ServerRpcContext { |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 119 | public: |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 120 | ServerRpcContext() {} |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 121 | virtual ~ServerRpcContext(){}; |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 122 | virtual bool RunNextState() = 0; // do next state, return false if all done |
| 123 | virtual void Reset() = 0; // start this back at a clean state |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 124 | }; |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 125 | static void* tag(ServerRpcContext* func) { |
| 126 | return reinterpret_cast<void*>(func); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 127 | } |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 128 | static ServerRpcContext* detag(void* tag) { |
| 129 | return reinterpret_cast<ServerRpcContext*>(tag); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | template <class RequestType, class ResponseType> |
| 133 | class ServerRpcContextUnaryImpl : public ServerRpcContext { |
| 134 | public: |
| 135 | ServerRpcContextUnaryImpl( |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 136 | std::function<void(ServerContext*, RequestType*, |
| 137 | grpc::ServerAsyncResponseWriter<ResponseType>*, |
| 138 | void*)> request_method, |
| 139 | std::function<grpc::Status(const RequestType*, ResponseType*)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 140 | invoke_method) |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 141 | : next_state_(&ServerRpcContextUnaryImpl::invoker), |
| 142 | request_method_(request_method), |
| 143 | invoke_method_(invoke_method), |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 144 | response_writer_(&srv_ctx_) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 145 | request_method_(&srv_ctx_, &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 146 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 147 | } |
vjpai | 3c11066 | 2015-02-27 07:17:16 -0800 | [diff] [blame] | 148 | ~ServerRpcContextUnaryImpl() GRPC_OVERRIDE {} |
vjpai | 6e2e64a | 2015-02-27 09:28:28 -0800 | [diff] [blame] | 149 | bool RunNextState() GRPC_OVERRIDE { return (this->*next_state_)(); } |
vjpai | 5b39f9a | 2015-02-27 09:33:00 -0800 | [diff] [blame] | 150 | void Reset() GRPC_OVERRIDE { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 151 | srv_ctx_ = ServerContext(); |
| 152 | req_ = RequestType(); |
| 153 | response_writer_ = |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 154 | grpc::ServerAsyncResponseWriter<ResponseType>(&srv_ctx_); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 155 | |
| 156 | // Then request the method |
| 157 | next_state_ = &ServerRpcContextUnaryImpl::invoker; |
| 158 | request_method_(&srv_ctx_, &req_, &response_writer_, |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 159 | AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 160 | } |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 161 | |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 162 | private: |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 163 | bool finisher() { return false; } |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 164 | bool invoker() { |
| 165 | ResponseType response; |
| 166 | |
| 167 | // Call the RPC processing function |
| 168 | grpc::Status status = invoke_method_(&req_, &response); |
| 169 | |
| 170 | // Have the response writer work and invoke on_finish when done |
| 171 | next_state_ = &ServerRpcContextUnaryImpl::finisher; |
Vijay Pai | 64ac47f | 2015-02-26 17:59:51 -0800 | [diff] [blame] | 172 | response_writer_.Finish(response, status, AsyncQpsServerTest::tag(this)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | ServerContext srv_ctx_; |
| 176 | RequestType req_; |
| 177 | bool (ServerRpcContextUnaryImpl::*next_state_)(); |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 178 | std::function<void(ServerContext*, RequestType*, |
| 179 | grpc::ServerAsyncResponseWriter<ResponseType>*, void*)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 180 | request_method_; |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 181 | std::function<grpc::Status(const RequestType*, ResponseType*)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 182 | invoke_method_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 183 | grpc::ServerAsyncResponseWriter<ResponseType> response_writer_; |
| 184 | }; |
| 185 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 186 | static Status UnaryCall(const SimpleRequest* request, |
| 187 | SimpleResponse* response) { |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 188 | if (request->has_response_size() && request->response_size() > 0) { |
| 189 | if (!SetPayload(request->response_type(), request->response_size(), |
| 190 | response->mutable_payload())) { |
| 191 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 192 | } |
| 193 | } |
| 194 | return Status::OK; |
| 195 | } |
| 196 | CompletionQueue srv_cq_; |
| 197 | TestService::AsyncService async_service_; |
Vijay Pai | acf6f31 | 2015-03-02 15:13:39 -0800 | [diff] [blame] | 198 | std::vector<std::thread> threads_; |
Craig Tiller | d6479d6 | 2015-03-04 12:50:11 -0800 | [diff] [blame] | 199 | std::unique_ptr<grpc::Server> server_; |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 200 | std::function<void(ServerContext*, SimpleRequest*, |
| 201 | grpc::ServerAsyncResponseWriter<SimpleResponse>*, void*)> |
vjpai | 4e1e1bc | 2015-02-27 23:47:12 -0800 | [diff] [blame] | 202 | request_unary_; |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 203 | std::forward_list<ServerRpcContext*> contexts_; |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 206 | std::unique_ptr<Server> CreateAsyncServer(const ServerConfig& config, |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 207 | int port) { |
| 208 | return std::unique_ptr<Server>(new AsyncQpsServerTest(config, port)); |
vjpai | dea740f | 2015-02-26 16:35:35 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 211 | } // namespace testing |
| 212 | } // namespace grpc |