blob: e4ee45a72d074a60b5ef895c8e49cde174833a62 [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
vpai80b6d012014-12-17 11:47:32 -080034#include <cassert>
35#include <memory>
Craig Tiller5c004c62015-02-23 16:31:57 -080036#include <mutex>
vpai80b6d012014-12-17 11:47:32 -080037#include <string>
38#include <thread>
39#include <vector>
40#include <sstream>
41
Craig Tiller5c004c62015-02-23 16:31:57 -080042#include <sys/signal.h>
43
vpai80b6d012014-12-17 11:47:32 -080044#include <grpc/grpc.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080045#include <grpc/support/alloc.h>
vpai80b6d012014-12-17 11:47:32 -080046#include <grpc/support/histogram.h>
47#include <grpc/support/log.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080048#include <grpc/support/host_port.h>
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010049#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080050#include <grpc++/client_context.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080051#include <grpc++/server.h>
52#include <grpc++/server_builder.h>
vjpai46f65232015-03-23 10:10:27 -070053#include <grpc++/status.h>
54#include <grpc++/stream.h>
55#include <gtest/gtest.h>
Craig Tiller056ba542015-01-31 21:15:10 -080056#include "test/core/util/grpc_profiler.h"
yangg59dfc902014-12-19 14:00:14 -080057#include "test/cpp/util/create_test_channel.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080058#include "test/cpp/qps/client.h"
vpai92fe70e2015-01-13 11:21:38 -080059#include "test/cpp/qps/qpstest.pb.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080060#include "test/cpp/qps/histogram.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080061#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080062
Craig Tiller6af9ed02015-03-02 22:42:10 -080063namespace grpc {
64namespace testing {
vpai80b6d012014-12-17 11:47:32 -080065
vjpai46f65232015-03-23 10:10:27 -070066class SynchronousClient : public Client {
Craig Tiller6af9ed02015-03-02 22:42:10 -080067 public:
Craig Tiller88568752015-03-04 10:50:43 -080068 SynchronousClient(const ClientConfig& config) : Client(config) {
vjpai46f65232015-03-23 10:10:27 -070069 num_threads_ =
70 config.outstanding_rpcs_per_channel() * config.client_channels();
71 responses_.resize(num_threads_);
Craig Tiller6af9ed02015-03-02 22:42:10 -080072 }
vpai80b6d012014-12-17 11:47:32 -080073
vjpai46f65232015-03-23 10:10:27 -070074 virtual ~SynchronousClient() { EndThreads(); }
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010075
vjpai46f65232015-03-23 10:10:27 -070076 protected:
77 size_t num_threads_;
78 std::vector<SimpleResponse> responses_;
79};
80
81class SynchronousUnaryClient GRPC_FINAL : public SynchronousClient {
82 public:
83 SynchronousUnaryClient(const ClientConfig& config):
84 SynchronousClient(config) {StartThreads(num_threads_);}
85 ~SynchronousUnaryClient() {}
86
87 void ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
Craig Tiller88568752015-03-04 10:50:43 -080088 auto* stub = channels_[thread_idx % channels_.size()].get_stub();
89 double start = Timer::Now();
90 grpc::ClientContext context;
Craig Tillera182bf12015-03-04 13:54:39 -080091 grpc::Status s =
92 stub->UnaryCall(&context, request_, &responses_[thread_idx]);
Craig Tiller88568752015-03-04 10:50:43 -080093 histogram->Add((Timer::Now() - start) * 1e9);
Craig Tiller6af9ed02015-03-02 22:42:10 -080094 }
Craig Tiller5c004c62015-02-23 16:31:57 -080095};
96
vjpai46f65232015-03-23 10:10:27 -070097class SynchronousStreamingClient GRPC_FINAL : public SynchronousClient {
98 public:
99 SynchronousStreamingClient(const ClientConfig& config):
100 SynchronousClient(config) {
101 for (size_t thread_idx=0;thread_idx<num_threads_;thread_idx++){
102 auto* stub = channels_[thread_idx % channels_.size()].get_stub();
103 stream_ = stub->StreamingCall(&context_);
104 }
105 StartThreads(num_threads_);
106 }
107 ~SynchronousStreamingClient() {
108 if (stream_) {
109 SimpleResponse response;
110 stream_->WritesDone();
Vijay Pai55bb5bd2015-03-23 12:44:09 -0700111 EXPECT_TRUE(stream_->Finish().IsOk());
vjpai46f65232015-03-23 10:10:27 -0700112 }
113 }
Vijay Pai55bb5bd2015-03-23 12:44:09 -0700114
vjpai46f65232015-03-23 10:10:27 -0700115 void ThreadFunc(Histogram* histogram, size_t thread_idx) GRPC_OVERRIDE {
116 double start = Timer::Now();
117 EXPECT_TRUE(stream_->Write(request_));
118 EXPECT_TRUE(stream_->Read(&responses_[thread_idx]));
119 histogram->Add((Timer::Now() - start) * 1e9);
120 }
121 private:
122 grpc::ClientContext context_;
Vijay Pai55bb5bd2015-03-23 12:44:09 -0700123 std::unique_ptr<grpc::ClientReaderWriter<SimpleRequest,
124 SimpleResponse>> stream_;
vjpai46f65232015-03-23 10:10:27 -0700125};
126
127std::unique_ptr<Client>
128CreateSynchronousUnaryClient(const ClientConfig& config) {
129 return std::unique_ptr<Client>(new SynchronousUnaryClient(config));
130}
131std::unique_ptr<Client>
132CreateSynchronousStreamingClient(const ClientConfig& config) {
133 return std::unique_ptr<Client>(new SynchronousStreamingClient(config));
vpai80b6d012014-12-17 11:47:32 -0800134}
135
Craig Tiller6af9ed02015-03-02 22:42:10 -0800136} // namespace testing
137} // namespace grpc