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> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame^] | 35 | #include <unistd.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 36 | #include <thread> |
| 37 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 38 | #include <gflags/gflags.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame^] | 39 | #include <grpc/grpc.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> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame^] | 42 | #include <grpc/support/log.h> |
| 43 | #include <grpc++/support/dynamic_thread_pool.h> |
| 44 | #include <grpc++/support/fixed_size_thread_pool.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 45 | #include <grpc++/server.h> |
| 46 | #include <grpc++/server_builder.h> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 47 | #include <grpc++/server_context.h> |
Craig Tiller | 68de8e9 | 2015-03-05 15:45:46 -0800 | [diff] [blame] | 48 | #include <grpc++/server_credentials.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame^] | 49 | |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 50 | #include "test/cpp/qps/qpstest.grpc.pb.h" |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 51 | #include "test/cpp/qps/server.h" |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 52 | #include "test/cpp/qps/timer.h" |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 53 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 54 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 55 | namespace grpc { |
| 56 | namespace testing { |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 57 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 58 | class TestServiceImpl GRPC_FINAL : public TestService::Service { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 59 | public: |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 60 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
Craig Tiller | 43ef582 | 2015-03-04 20:01:09 -0800 | [diff] [blame] | 61 | SimpleResponse* response) GRPC_OVERRIDE { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 62 | if (request->response_size() > 0) { |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 63 | if (!Server::SetPayload(request->response_type(), |
| 64 | request->response_size(), |
| 65 | response->mutable_payload())) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 66 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 67 | } |
| 68 | } |
| 69 | return Status::OK; |
| 70 | } |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 71 | Status StreamingCall( |
| 72 | ServerContext* context, |
| 73 | ServerReaderWriter<SimpleResponse, SimpleRequest>* stream) GRPC_OVERRIDE { |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 74 | SimpleRequest request; |
| 75 | while (stream->Read(&request)) { |
| 76 | SimpleResponse response; |
| 77 | if (request.response_size() > 0) { |
Craig Tiller | 5c8737d | 2015-05-21 11:42:17 -0700 | [diff] [blame] | 78 | if (!Server::SetPayload(request.response_type(), |
| 79 | request.response_size(), |
| 80 | response.mutable_payload())) { |
| 81 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 82 | } |
vjpai | 46f6523 | 2015-03-23 10:10:27 -0700 | [diff] [blame] | 83 | } |
| 84 | stream->Write(response); |
| 85 | } |
| 86 | return Status::OK; |
| 87 | } |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 90 | class SynchronousServer GRPC_FINAL : public grpc::testing::Server { |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 91 | public: |
Craig Tiller | 10923c2 | 2015-03-03 14:24:49 -0800 | [diff] [blame] | 92 | SynchronousServer(const ServerConfig& config, int port) |
Vijay Pai | 1f3e6c1 | 2015-07-23 16:18:21 -0700 | [diff] [blame] | 93 | : thread_pool_(), impl_(MakeImpl(port)) { |
| 94 | if (config.threads() > 0) { |
| 95 | thread_pool_.reset(new FixedSizeThreadPool(config.threads())); |
| 96 | } else { |
| 97 | thread_pool_.reset(new DynamicThreadPool(-config.threads())); |
| 98 | } |
| 99 | } |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 102 | std::unique_ptr<grpc::Server> MakeImpl(int port) { |
| 103 | ServerBuilder builder; |
| 104 | |
| 105 | char* server_address = NULL; |
| 106 | gpr_join_host_port(&server_address, "::", port); |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 107 | builder.AddListeningPort(server_address, InsecureServerCredentials()); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 108 | gpr_free(server_address); |
| 109 | |
| 110 | builder.RegisterService(&service_); |
| 111 | |
Vijay Pai | 1f3e6c1 | 2015-07-23 16:18:21 -0700 | [diff] [blame] | 112 | builder.SetThreadPool(thread_pool_.get()); |
Craig Tiller | 2eaf159 | 2015-03-04 14:47:18 -0800 | [diff] [blame] | 113 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 114 | return builder.BuildAndStart(); |
| 115 | } |
| 116 | |
| 117 | TestServiceImpl service_; |
Vijay Pai | 1f3e6c1 | 2015-07-23 16:18:21 -0700 | [diff] [blame] | 118 | std::unique_ptr<ThreadPoolInterface> thread_pool_; |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 119 | std::unique_ptr<grpc::Server> impl_; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
Craig Tiller | 10923c2 | 2015-03-03 14:24:49 -0800 | [diff] [blame] | 122 | std::unique_ptr<grpc::testing::Server> CreateSynchronousServer( |
| 123 | const ServerConfig& config, int port) { |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 124 | return std::unique_ptr<Server>(new SynchronousServer(config, port)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 127 | } // namespace testing |
| 128 | } // namespace grpc |