blob: ebe06c1e4a5691d15a19fbf4efd9e7cef9a0d9b4 [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>
vpai80b6d012014-12-17 11:47:32 -080035#include <thread>
36
Craig Tillercf133f42015-02-26 14:05:56 -080037#include <unistd.h>
38
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010039#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080040#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>
yangga4b6f5d2014-12-17 15:53:12 -080045#include <grpc++/server_context.h>
vpai80b6d012014-12-17 11:47:32 -080046#include <grpc++/status.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080047#include <grpc++/stream.h>
Vijay Paic3b02d92015-02-10 10:39:03 -080048#include "src/cpp/server/thread_pool.h"
Craig Tiller056ba542015-01-31 21:15:10 -080049#include "test/core/util/grpc_profiler.h"
vpai92fe70e2015-01-13 11:21:38 -080050#include "test/cpp/qps/qpstest.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
54#include <grpc/grpc.h>
55#include <grpc/support/log.h>
56
Craig Tiller6af9ed02015-03-02 22:42:10 -080057namespace grpc {
58namespace testing {
Craig Tiller056ba542015-01-31 21:15:10 -080059
Craig Tiller056ba542015-01-31 21:15:10 -080060static bool SetPayload(PayloadType type, int size, Payload* payload) {
vpai80b6d012014-12-17 11:47:32 -080061 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 Tillercf133f42015-02-26 14:05:56 -080072class TestServiceImpl GRPC_FINAL : public TestService::Service {
vpai80b6d012014-12-17 11:47:32 -080073 public:
yangga4b6f5d2014-12-17 15:53:12 -080074 Status UnaryCall(ServerContext* context, const SimpleRequest* request,
Craig Tiller2d0f36c2015-02-23 23:16:17 -080075 SimpleResponse* response) override {
vpai80b6d012014-12-17 11:47:32 -080076 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 Tiller6af9ed02015-03-02 22:42:10 -080086class SynchronousServer GRPC_FINAL : public grpc::testing::Server {
Craig Tiller5c004c62015-02-23 16:31:57 -080087 public:
Craig Tiller6af9ed02015-03-02 22:42:10 -080088 SynchronousServer(const ServerConfig& config, int port) : thread_pool_(config.threads()), impl_(MakeImpl(port)), timer_(new Timer) {}
Craig Tiller5c004c62015-02-23 16:31:57 -080089
Craig Tiller6af9ed02015-03-02 22:42:10 -080090 ServerStats Mark() GRPC_OVERRIDE {
91 std::unique_ptr<Timer> timer(new Timer);
92 timer.swap(timer_);
Craig Tiller5c004c62015-02-23 16:31:57 -080093
Craig Tiller6af9ed02015-03-02 22:42:10 -080094 auto timer_result = timer->Mark();
Craig Tiller5c004c62015-02-23 16:31:57 -080095
Craig Tiller6af9ed02015-03-02 22:42:10 -080096 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 Tiller5c004c62015-02-23 16:31:57 -0800101 }
102
103 private:
Craig Tiller6af9ed02015-03-02 22:42:10 -0800104 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 Tiller5c004c62015-02-23 16:31:57 -0800121};
122
Craig Tiller6af9ed02015-03-02 22:42:10 -0800123std::unique_ptr<grpc::testing::Server> CreateSynchronousServer(const ServerConfig& config, int port) {
124 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