blob: 4b778820d070436eda157cef5ca64f065d54f167 [file] [log] [blame]
vpai80b6d012014-12-17 11:47:32 -08001/*
2 *
Alistair Veitch75d5c0f2016-02-02 09:43:02 -08003 * Copyright 2015-2016, 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
34#include <thread>
35
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010036#include <gflags/gflags.h>
yang-g9e2f90c2015-08-21 15:35:03 -070037#include <grpc/grpc.h>
vpai80b6d012014-12-17 11:47:32 -080038#include <grpc/support/alloc.h>
39#include <grpc/support/host_port.h>
yang-g9e2f90c2015-08-21 15:35:03 -070040#include <grpc/support/log.h>
vpai80b6d012014-12-17 11:47:32 -080041#include <grpc++/server.h>
42#include <grpc++/server_builder.h>
yangga4b6f5d2014-12-17 15:53:12 -080043#include <grpc++/server_context.h>
Julien Boeuf5be92a32015-08-28 16:28:18 -070044#include <grpc++/security/server_credentials.h>
yang-g9e2f90c2015-08-21 15:35:03 -070045
Craig Tiller6af9ed02015-03-02 22:42:10 -080046#include "test/cpp/qps/server.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080047#include "test/cpp/qps/timer.h"
Craig Tiller1b4e3302015-12-17 16:35:00 -080048#include "src/proto/grpc/testing/services.grpc.pb.h"
vpai80b6d012014-12-17 11:47:32 -080049
Craig Tiller6af9ed02015-03-02 22:42:10 -080050namespace grpc {
51namespace testing {
Craig Tiller056ba542015-01-31 21:15:10 -080052
vjpai119c1032015-10-29 01:21:04 -070053class BenchmarkServiceImpl GRPC_FINAL : public BenchmarkService::Service {
vpai80b6d012014-12-17 11:47:32 -080054 public:
yangga4b6f5d2014-12-17 15:53:12 -080055 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
Craig Tiller43ef5822015-03-04 20:01:09 -080056 SimpleResponse* response) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -070057 if (request->response_size() > 0) {
Craig Tillera182bf12015-03-04 13:54:39 -080058 if (!Server::SetPayload(request->response_type(),
59 request->response_size(),
60 response->mutable_payload())) {
vpai80b6d012014-12-17 11:47:32 -080061 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
62 }
63 }
64 return Status::OK;
65 }
Craig Tiller5c8737d2015-05-21 11:42:17 -070066 Status StreamingCall(
67 ServerContext* context,
68 ServerReaderWriter<SimpleResponse, SimpleRequest>* stream) GRPC_OVERRIDE {
vjpai46f65232015-03-23 10:10:27 -070069 SimpleRequest request;
70 while (stream->Read(&request)) {
71 SimpleResponse response;
72 if (request.response_size() > 0) {
Craig Tiller5c8737d2015-05-21 11:42:17 -070073 if (!Server::SetPayload(request.response_type(),
74 request.response_size(),
75 response.mutable_payload())) {
76 return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
77 }
vjpai46f65232015-03-23 10:10:27 -070078 }
79 stream->Write(response);
80 }
81 return Status::OK;
82 }
vpai80b6d012014-12-17 11:47:32 -080083};
84
Craig Tiller6af9ed02015-03-02 22:42:10 -080085class SynchronousServer GRPC_FINAL : public grpc::testing::Server {
Craig Tiller5c004c62015-02-23 16:31:57 -080086 public:
Vijay Paice846702015-11-04 00:30:12 -080087 explicit SynchronousServer(const ServerConfig& config) : Server(config) {
Craig Tiller6af9ed02015-03-02 22:42:10 -080088 ServerBuilder builder;
89
90 char* server_address = NULL;
vjpai72a63322015-10-29 02:23:11 -070091
Alistair Veitch75d5c0f2016-02-02 09:43:02 -080092 gpr_join_host_port(&server_address, "::", port());
Vijay Paice846702015-11-04 00:30:12 -080093 builder.AddListeningPort(server_address,
94 Server::CreateServerCredentials(config));
Craig Tiller6af9ed02015-03-02 22:42:10 -080095 gpr_free(server_address);
96
97 builder.RegisterService(&service_);
98
vjpai72a63322015-10-29 02:23:11 -070099 impl_ = builder.BuildAndStart();
Craig Tiller6af9ed02015-03-02 22:42:10 -0800100 }
Vijay Paice846702015-11-04 00:30:12 -0800101
vjpai72a63322015-10-29 02:23:11 -0700102 private:
vjpai119c1032015-10-29 01:21:04 -0700103 BenchmarkServiceImpl service_;
Craig Tiller6af9ed02015-03-02 22:42:10 -0800104 std::unique_ptr<grpc::Server> impl_;
Craig Tiller5c004c62015-02-23 16:31:57 -0800105};
106
Craig Tiller10923c22015-03-03 14:24:49 -0800107std::unique_ptr<grpc::testing::Server> CreateSynchronousServer(
vjpai72a63322015-10-29 02:23:11 -0700108 const ServerConfig& config) {
109 return std::unique_ptr<Server>(new SynchronousServer(config));
vpai80b6d012014-12-17 11:47:32 -0800110}
111
Craig Tiller6af9ed02015-03-02 22:42:10 -0800112} // namespace testing
113} // namespace grpc