OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 <thread> |
| 35 |
| 36 #include <gflags/gflags.h> |
| 37 #include <grpc++/security/server_credentials.h> |
| 38 #include <grpc++/server.h> |
| 39 #include <grpc++/server_builder.h> |
| 40 #include <grpc++/server_context.h> |
| 41 #include <grpc/grpc.h> |
| 42 #include <grpc/support/alloc.h> |
| 43 #include <grpc/support/host_port.h> |
| 44 #include <grpc/support/log.h> |
| 45 |
| 46 #include "src/proto/grpc/testing/services.grpc.pb.h" |
| 47 #include "test/cpp/qps/server.h" |
| 48 #include "test/cpp/qps/usage_timer.h" |
| 49 |
| 50 namespace grpc { |
| 51 namespace testing { |
| 52 |
| 53 class BenchmarkServiceImpl GRPC_FINAL : public BenchmarkService::Service { |
| 54 public: |
| 55 Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
| 56 SimpleResponse* response) GRPC_OVERRIDE { |
| 57 if (request->response_size() > 0) { |
| 58 if (!Server::SetPayload(request->response_type(), |
| 59 request->response_size(), |
| 60 response->mutable_payload())) { |
| 61 return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 62 } |
| 63 } |
| 64 return Status::OK; |
| 65 } |
| 66 Status StreamingCall( |
| 67 ServerContext* context, |
| 68 ServerReaderWriter<SimpleResponse, SimpleRequest>* stream) GRPC_OVERRIDE { |
| 69 SimpleRequest request; |
| 70 while (stream->Read(&request)) { |
| 71 SimpleResponse response; |
| 72 if (request.response_size() > 0) { |
| 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 } |
| 78 } |
| 79 stream->Write(response); |
| 80 } |
| 81 return Status::OK; |
| 82 } |
| 83 }; |
| 84 |
| 85 class SynchronousServer GRPC_FINAL : public grpc::testing::Server { |
| 86 public: |
| 87 explicit SynchronousServer(const ServerConfig& config) : Server(config) { |
| 88 ServerBuilder builder; |
| 89 |
| 90 char* server_address = NULL; |
| 91 |
| 92 gpr_join_host_port(&server_address, "::", port()); |
| 93 builder.AddListeningPort(server_address, |
| 94 Server::CreateServerCredentials(config)); |
| 95 gpr_free(server_address); |
| 96 |
| 97 builder.RegisterService(&service_); |
| 98 |
| 99 impl_ = builder.BuildAndStart(); |
| 100 } |
| 101 |
| 102 private: |
| 103 BenchmarkServiceImpl service_; |
| 104 std::unique_ptr<grpc::Server> impl_; |
| 105 }; |
| 106 |
| 107 std::unique_ptr<grpc::testing::Server> CreateSynchronousServer( |
| 108 const ServerConfig& config) { |
| 109 return std::unique_ptr<Server>(new SynchronousServer(config)); |
| 110 } |
| 111 |
| 112 } // namespace testing |
| 113 } // namespace grpc |
OLD | NEW |