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