blob: 827c1ec09b02c30dddaedb53a0ab5edeba2afcf0 [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>
vpai80b6d012014-12-17 11:47:32 -080051#include <grpc++/status.h>
Craig Tiller5c004c62015-02-23 16:31:57 -080052#include <grpc++/server.h>
53#include <grpc++/server_builder.h>
Craig Tiller056ba542015-01-31 21:15:10 -080054#include "test/core/util/grpc_profiler.h"
yangg59dfc902014-12-19 14:00:14 -080055#include "test/cpp/util/create_test_channel.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080056#include "test/cpp/qps/client.h"
vpai92fe70e2015-01-13 11:21:38 -080057#include "test/cpp/qps/qpstest.pb.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080058#include "test/cpp/qps/histogram.h"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080059#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080060
Craig Tiller6af9ed02015-03-02 22:42:10 -080061namespace grpc {
62namespace testing {
vpai80b6d012014-12-17 11:47:32 -080063
Craig Tiller6af9ed02015-03-02 22:42:10 -080064class SynchronousClient GRPC_FINAL : public Client {
65 public:
66 SynchronousClient(const ClientConfig& config) : timer_(new Timer) {
67 for (int i = 0; i < config.client_channels(); i++) {
68 channels_.push_back(ClientChannelInfo(config.server_targets(i % config.server_targets_size()), config));
69 auto* stub = channels_.back().get_stub();
70 for (int j = 0; j < config.outstanding_rpcs_per_channel(); j++) {
71 threads_.emplace_back(new Thread(stub, config));
72 }
73 }
74 }
vpai80b6d012014-12-17 11:47:32 -080075
Craig Tiller6af9ed02015-03-02 22:42:10 -080076 ClientStats Mark() {
77 Histogram latencies;
78 std::vector<Histogram> to_merge(threads_.size());
79 for (size_t i = 0; i < threads_.size(); i++) {
80 threads_[i]->BeginSwap(&to_merge[i]);
81 }
82 std::unique_ptr<Timer> timer(new Timer);
83 timer_.swap(timer);
84 for (size_t i = 0; i < threads_.size(); i++) {
85 threads_[i]->EndSwap();
86 latencies.Merge(&to_merge[i]);
87 }
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010088
Craig Tiller6af9ed02015-03-02 22:42:10 -080089 auto timer_result = timer->Mark();
vpai80b6d012014-12-17 11:47:32 -080090
Craig Tiller6af9ed02015-03-02 22:42:10 -080091 ClientStats stats;
92 auto* l = stats.mutable_latencies();
93 l->set_l_50(latencies.Percentile(50));
94 l->set_l_90(latencies.Percentile(90));
95 l->set_l_99(latencies.Percentile(99));
96 l->set_l_999(latencies.Percentile(99.9));
97 stats.set_num_rpcs(latencies.Count());
98 stats.set_time_elapsed(timer_result.wall);
99 stats.set_time_system(timer_result.system);
100 stats.set_time_user(timer_result.user);
101 return stats;
102 }
Craig Tiller5c004c62015-02-23 16:31:57 -0800103
Craig Tiller6af9ed02015-03-02 22:42:10 -0800104 private:
105 class Thread {
106 public:
107 Thread(TestService::Stub* stub, const ClientConfig& config) : stub_(stub), config_(config), done_(false), new_(nullptr), impl_([this]() {
108 SimpleRequest request;
109 SimpleResponse response;
110 request.set_response_type(
111 grpc::testing::PayloadType::COMPRESSABLE);
112 request.set_response_size(config_.payload_size());
113 for (;;) {
114 {
115 std::lock_guard<std::mutex> g(mu_);
116 if (done_) return;
117 if (new_) {
118 new_->Swap(&histogram_);
119 new_ = nullptr;
120 cv_.notify_one();
121 }
122 }
123 double start = Timer::Now();
124 grpc::ClientContext context;
125 grpc::Status s =
126 stub_->UnaryCall(&context, request, &response);
127 histogram_.Add((Timer::Now() - start) * 1e9);
128 }
129 }) {}
Craig Tiller5c004c62015-02-23 16:31:57 -0800130
Craig Tiller6af9ed02015-03-02 22:42:10 -0800131 ~Thread() {
132 {
133 std::lock_guard<std::mutex> g(mu_);
134 done_ = true;
135 }
136 impl_.join();
137 }
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800138
Craig Tiller6af9ed02015-03-02 22:42:10 -0800139 void BeginSwap(Histogram* n) {
140 std::lock_guard<std::mutex> g(mu_);
141 new_ = n;
142 }
143
144 void EndSwap() {
145 std::unique_lock<std::mutex> g(mu_);
146 cv_.wait(g, [this]() { return new_ == nullptr; });
147 }
148
149 private:
150 Thread(const Thread&);
151 Thread& operator=(const Thread&);
152
153 TestService::Stub* stub_;
154 ClientConfig config_;
155 std::mutex mu_;
156 std::condition_variable cv_;
157 bool done_;
158 Histogram *new_;
159 Histogram histogram_;
160 std::thread impl_;
161 };
vpai80b6d012014-12-17 11:47:32 -0800162
163 class ClientChannelInfo {
164 public:
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800165 explicit ClientChannelInfo(const grpc::string& target, const ClientConfig& config)
166 : channel_(CreateTestChannel(target, config.enable_ssl())),
vpai80b6d012014-12-17 11:47:32 -0800167 stub_(TestService::NewStub(channel_)) {}
168 ChannelInterface *get_channel() { return channel_.get(); }
169 TestService::Stub *get_stub() { return stub_.get(); }
170
171 private:
172 std::shared_ptr<ChannelInterface> channel_;
173 std::unique_ptr<TestService::Stub> stub_;
174 };
Craig Tiller6af9ed02015-03-02 22:42:10 -0800175 std::vector<ClientChannelInfo> channels_;
176 std::vector<std::unique_ptr<Thread>> threads_;
177 std::unique_ptr<Timer> timer_;
Craig Tiller5c004c62015-02-23 16:31:57 -0800178};
179
Craig Tiller6af9ed02015-03-02 22:42:10 -0800180std::unique_ptr<Client> CreateSynchronousClient(const ClientConfig& config) {
181 return std::unique_ptr<Client>(new SynchronousClient(config));
vpai80b6d012014-12-17 11:47:32 -0800182}
183
Craig Tiller6af9ed02015-03-02 22:42:10 -0800184} // namespace testing
185} // namespace grpc