blob: c149fbc738fb7c530a233b376b2bd7e1554e415d [file] [log] [blame]
vpai80b6d012014-12-17 11:47:32 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
vpai80b6d012014-12-17 11:47:32 -08004 * 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 Tiller056ba542015-01-31 21:15:10 -080034#include <sys/signal.h>
yang-g9e2f90c2015-08-21 15:35:03 -070035#include <unistd.h>
vpai80b6d012014-12-17 11:47:32 -080036#include <thread>
37
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010038#include <gflags/gflags.h>
yang-g9e2f90c2015-08-21 15:35:03 -070039#include <grpc/grpc.h>
vpai80b6d012014-12-17 11:47:32 -080040#include <grpc/support/alloc.h>
41#include <grpc/support/host_port.h>
yang-g9e2f90c2015-08-21 15:35:03 -070042#include <grpc/support/log.h>
43#include <grpc++/support/dynamic_thread_pool.h>
44#include <grpc++/support/fixed_size_thread_pool.h>
vpai80b6d012014-12-17 11:47:32 -080045#include <grpc++/server.h>
46#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080047#include <grpc++/server_context.h>
Craig Tiller68de8e92015-03-05 15:45:46 -080048#include <grpc++/server_credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070049
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020050#include "test/cpp/qps/qpstest.grpc.pb.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080051#include "test/cpp/qps/server.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080052#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080053
vpai80b6d012014-12-17 11:47:32 -080054
Craig Tiller6af9ed02015-03-02 22:42:10 -080055namespace grpc {
56namespace testing {
Craig Tiller056ba542015-01-31 21:15:10 -080057
Craig Tillercf133f42015-02-26 14:05:56 -080058class TestServiceImpl GRPC_FINAL : public TestService::Service {
vpai80b6d012014-12-17 11:47:32 -080059 public:
yangga4b6f5d2014-12-17 15:53:12 -080060 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
Craig Tiller43ef5822015-03-04 20:01:09 -080061 SimpleResponse* response) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -070062 if (request->response_size() > 0) {
Craig Tillera182bf12015-03-04 13:54:39 -080063 if (!Server::SetPayload(request->response_type(),
64 request->response_size(),
65 response->mutable_payload())) {
vpai80b6d012014-12-17 11:47:32 -080066 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
67 }
68 }
69 return Status::OK;
70 }
Craig Tiller5c8737d2015-05-21 11:42:17 -070071 Status StreamingCall(
72 ServerContext* context,
73 ServerReaderWriter<SimpleResponse, SimpleRequest>* stream) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -070074 SimpleRequest request;
75 while (stream->Read(&request)) {
76 SimpleResponse response;
77 if (request.response_size() > 0) {
Craig Tiller5c8737d2015-05-21 11:42:17 -070078 if (!Server::SetPayload(request.response_type(),
79 request.response_size(),
80 response.mutable_payload())) {
81 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
82 }
vjpai46f65232015-03-23 10:10:27 -070083 }
84 stream->Write(response);
85 }
86 return Status::OK;
87 }
vpai80b6d012014-12-17 11:47:32 -080088};
89
Craig Tiller6af9ed02015-03-02 22:42:10 -080090class SynchronousServer GRPC_FINAL : public grpc::testing::Server {
Craig Tiller5c004c62015-02-23 16:31:57 -080091 public:
Craig Tiller10923c22015-03-03 14:24:49 -080092 SynchronousServer(const ServerConfig& config, int port)
Vijay Pai1f3e6c12015-07-23 16:18:21 -070093 : 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 Tiller5c004c62015-02-23 16:31:57 -0800100
101 private:
Craig Tiller6af9ed02015-03-02 22:42:10 -0800102 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 Noblecfd60732015-03-18 16:27:43 -0700107 builder.AddListeningPort(server_address, InsecureServerCredentials());
Craig Tiller6af9ed02015-03-02 22:42:10 -0800108 gpr_free(server_address);
109
110 builder.RegisterService(&service_);
111
Vijay Pai1f3e6c12015-07-23 16:18:21 -0700112 builder.SetThreadPool(thread_pool_.get());
Craig Tiller2eaf1592015-03-04 14:47:18 -0800113
Craig Tiller6af9ed02015-03-02 22:42:10 -0800114 return builder.BuildAndStart();
115 }
116
117 TestServiceImpl service_;
Vijay Pai1f3e6c12015-07-23 16:18:21 -0700118 std::unique_ptr<ThreadPoolInterface> thread_pool_;
Craig Tiller6af9ed02015-03-02 22:42:10 -0800119 std::unique_ptr<grpc::Server> impl_;
Craig Tiller5c004c62015-02-23 16:31:57 -0800120};
121
Craig Tiller10923c22015-03-03 14:24:49 -0800122std::unique_ptr<grpc::testing::Server> CreateSynchronousServer(
123 const ServerConfig& config, int port) {
Craig Tiller6af9ed02015-03-02 22:42:10 -0800124 return std::unique_ptr<Server>(new SynchronousServer(config, port));
vpai80b6d012014-12-17 11:47:32 -0800125}
126
Craig Tiller6af9ed02015-03-02 22:42:10 -0800127} // namespace testing
128} // namespace grpc