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> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 35 | #include <thread> |
| 36 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 39 | #include <gflags/gflags.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> |
| 42 | #include <grpc++/config.h> |
| 43 | #include <grpc++/server.h> |
| 44 | #include <grpc++/server_builder.h> |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 45 | #include <grpc++/server_context.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 46 | #include <grpc++/status.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 47 | #include <grpc++/stream.h> |
Vijay Pai | c3b02d9 | 2015-02-10 10:39:03 -0800 | [diff] [blame] | 48 | #include "src/cpp/server/thread_pool.h" |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 49 | #include "test/core/util/grpc_profiler.h" |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 50 | #include "test/cpp/qps/qpstest.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 | |
| 54 | #include <grpc/grpc.h> |
| 55 | #include <grpc/support/log.h> |
| 56 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 57 | namespace grpc { |
| 58 | namespace testing { |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 59 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 60 | static bool SetPayload(PayloadType type, int size, Payload* payload) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 61 | PayloadType response_type = type; |
| 62 | // TODO(yangg): Support UNCOMPRESSABLE payload. |
| 63 | if (type != PayloadType::COMPRESSABLE) { |
| 64 | return false; |
| 65 | } |
| 66 | payload->set_type(response_type); |
| 67 | std::unique_ptr<char[]> body(new char[size]()); |
| 68 | payload->set_body(body.get(), size); |
| 69 | return true; |
| 70 | } |
| 71 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 72 | class TestServiceImpl GRPC_FINAL : public TestService::Service { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 73 | public: |
yangg | a4b6f5d | 2014-12-17 15:53:12 -0800 | [diff] [blame] | 74 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame] | 75 | SimpleResponse* response) override { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 76 | if (request->has_response_size() && request->response_size() > 0) { |
| 77 | if (!SetPayload(request->response_type(), request->response_size(), |
| 78 | response->mutable_payload())) { |
| 79 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 80 | } |
| 81 | } |
| 82 | return Status::OK; |
| 83 | } |
| 84 | }; |
| 85 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 86 | class SynchronousServer GRPC_FINAL : public grpc::testing::Server { |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 87 | public: |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 88 | SynchronousServer(const ServerConfig& config, int port) : thread_pool_(config.threads()), impl_(MakeImpl(port)), timer_(new Timer) {} |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 89 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 90 | ServerStats Mark() GRPC_OVERRIDE { |
| 91 | std::unique_ptr<Timer> timer(new Timer); |
| 92 | timer.swap(timer_); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 93 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 94 | auto timer_result = timer->Mark(); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 95 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 96 | ServerStats stats; |
| 97 | stats.set_time_elapsed(timer_result.wall); |
| 98 | stats.set_time_system(timer_result.system); |
| 99 | stats.set_time_user(timer_result.user); |
| 100 | return stats; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | private: |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 104 | std::unique_ptr<grpc::Server> MakeImpl(int port) { |
| 105 | ServerBuilder builder; |
| 106 | |
| 107 | char* server_address = NULL; |
| 108 | gpr_join_host_port(&server_address, "::", port); |
| 109 | builder.AddPort(server_address); |
| 110 | gpr_free(server_address); |
| 111 | |
| 112 | builder.RegisterService(&service_); |
| 113 | |
| 114 | return builder.BuildAndStart(); |
| 115 | } |
| 116 | |
| 117 | TestServiceImpl service_; |
| 118 | ThreadPool thread_pool_; |
| 119 | std::unique_ptr<grpc::Server> impl_; |
| 120 | std::unique_ptr<Timer> timer_; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame^] | 123 | std::unique_ptr<grpc::testing::Server> CreateSynchronousServer(const ServerConfig& config, int port) { |
| 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 |