vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Alistair Veitch | 75d5c0f | 2016-02-02 09:43:02 -0800 | [diff] [blame^] | 3 | * Copyright 2015-2016, 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 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 46 | #include "test/cpp/qps/server.h" |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 47 | #include "test/cpp/qps/timer.h" |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 48 | #include "src/proto/grpc/testing/services.grpc.pb.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 | |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 53 | class BenchmarkServiceImpl GRPC_FINAL : public BenchmarkService::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: |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 87 | explicit SynchronousServer(const ServerConfig& config) : Server(config) { |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 88 | ServerBuilder builder; |
| 89 | |
| 90 | char* server_address = NULL; |
vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 91 | |
Alistair Veitch | 75d5c0f | 2016-02-02 09:43:02 -0800 | [diff] [blame^] | 92 | gpr_join_host_port(&server_address, "::", port()); |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 93 | builder.AddListeningPort(server_address, |
| 94 | Server::CreateServerCredentials(config)); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 95 | gpr_free(server_address); |
| 96 | |
| 97 | builder.RegisterService(&service_); |
| 98 | |
vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 99 | impl_ = builder.BuildAndStart(); |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 100 | } |
Vijay Pai | ce84670 | 2015-11-04 00:30:12 -0800 | [diff] [blame] | 101 | |
vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 102 | private: |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 103 | BenchmarkServiceImpl service_; |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 104 | std::unique_ptr<grpc::Server> impl_; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Craig Tiller | 10923c2 | 2015-03-03 14:24:49 -0800 | [diff] [blame] | 107 | std::unique_ptr<grpc::testing::Server> CreateSynchronousServer( |
vjpai | 72a6332 | 2015-10-29 02:23:11 -0700 | [diff] [blame] | 108 | const ServerConfig& config) { |
| 109 | return std::unique_ptr<Server>(new SynchronousServer(config)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 112 | } // namespace testing |
| 113 | } // namespace grpc |