blob: 213e5d92149eac03b334c7c2c829272c3d88d98f [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"
vpai92fe70e2015-01-13 11:21:38 -080056#include "test/cpp/qps/qpstest.pb.h"
vpai80b6d012014-12-17 11:47:32 -080057
Craig Tiller5c004c62015-02-23 16:31:57 -080058DEFINE_int32(driver_port, 0, "Client driver port.");
vpai80b6d012014-12-17 11:47:32 -080059
60using grpc::ChannelInterface;
yangg59dfc902014-12-19 14:00:14 -080061using grpc::CreateTestChannel;
Craig Tiller5c004c62015-02-23 16:31:57 -080062using grpc::ServerBuilder;
63using grpc::testing::ClientArgs;
64using grpc::testing::ClientResult;
65using grpc::testing::QpsClient;
vpai80b6d012014-12-17 11:47:32 -080066using grpc::testing::SimpleRequest;
67using grpc::testing::SimpleResponse;
vpai92fe70e2015-01-13 11:21:38 -080068using grpc::testing::StatsRequest;
vpai80b6d012014-12-17 11:47:32 -080069using grpc::testing::TestService;
70
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010071// In some distros, gflags is in the namespace google, and in some others,
72// in gflags. This hack is enabling us to find both.
73namespace google { }
74namespace gflags { }
75using namespace google;
76using namespace gflags;
77
vpai80b6d012014-12-17 11:47:32 -080078static double now() {
79 gpr_timespec tv = gpr_now();
80 return 1e9 * tv.tv_sec + tv.tv_nsec;
81}
82
Craig Tiller5c004c62015-02-23 16:31:57 -080083static bool got_sigint = false;
84
85static void sigint_handler(int x) { got_sigint = 1; }
86
87ClientResult RunTest(const ClientArgs& args) {
vpai80b6d012014-12-17 11:47:32 -080088 gpr_log(GPR_INFO,
89 "QPS test with parameters\n"
90 "enable_ssl = %d\n"
91 "client_channels = %d\n"
92 "client_threads = %d\n"
93 "num_rpcs = %d\n"
94 "payload_size = %d\n"
Craig Tiller5c004c62015-02-23 16:31:57 -080095 "server_target = %s\n\n",
96 args.enable_ssl(), args.client_channels(), args.client_threads(), args.num_rpcs(),
97 args.payload_size(), args.server_target().c_str());
vpai80b6d012014-12-17 11:47:32 -080098
99 class ClientChannelInfo {
100 public:
Craig Tiller5c004c62015-02-23 16:31:57 -0800101 explicit ClientChannelInfo(const ClientArgs& args)
102 : channel_(CreateTestChannel(args.server_target(), args.enable_ssl())),
vpai80b6d012014-12-17 11:47:32 -0800103 stub_(TestService::NewStub(channel_)) {}
104 ChannelInterface *get_channel() { return channel_.get(); }
105 TestService::Stub *get_stub() { return stub_.get(); }
106
107 private:
108 std::shared_ptr<ChannelInterface> channel_;
109 std::unique_ptr<TestService::Stub> stub_;
110 };
111
112 std::vector<ClientChannelInfo> channels;
Craig Tiller5c004c62015-02-23 16:31:57 -0800113 for (int i = 0; i < args.client_channels(); i++) {
114 channels.push_back(ClientChannelInfo(args));
vpai80b6d012014-12-17 11:47:32 -0800115 }
116
117 std::vector<std::thread> threads; // Will add threads when ready to execute
Craig Tiller5c004c62015-02-23 16:31:57 -0800118 std::vector< ::gpr_histogram *> thread_stats(args.client_threads());
vpai80b6d012014-12-17 11:47:32 -0800119
vpai92fe70e2015-01-13 11:21:38 -0800120 grpc::ClientContext context_stats_begin;
vpai92fe70e2015-01-13 11:21:38 -0800121
Craig Tiller056ba542015-01-31 21:15:10 -0800122 grpc_profiler_start("qps_client.prof");
123
Craig Tiller5c004c62015-02-23 16:31:57 -0800124 gpr_timespec start = gpr_now();
125
126 for (int i = 0; i < args.client_threads(); i++) {
vpai80b6d012014-12-17 11:47:32 -0800127 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
128 GPR_ASSERT(hist != NULL);
129 thread_stats[i] = hist;
130
Yang Gao5fd0d292015-01-26 00:19:48 -0800131 threads.push_back(
Craig Tiller5c004c62015-02-23 16:31:57 -0800132 std::thread([hist, args, &channels](int channel_num) {
Yang Gao5fd0d292015-01-26 00:19:48 -0800133 SimpleRequest request;
134 SimpleResponse response;
135 request.set_response_type(
136 grpc::testing::PayloadType::COMPRESSABLE);
Craig Tiller5c004c62015-02-23 16:31:57 -0800137 request.set_response_size(args.payload_size());
vpai80b6d012014-12-17 11:47:32 -0800138
Craig Tiller5c004c62015-02-23 16:31:57 -0800139 for (int j = 0; j < args.num_rpcs(); j++) {
Yang Gao5fd0d292015-01-26 00:19:48 -0800140 TestService::Stub *stub =
141 channels[channel_num].get_stub();
142 double start = now();
143 grpc::ClientContext context;
144 grpc::Status s =
145 stub->UnaryCall(&context, request, &response);
146 gpr_histogram_add(hist, now() - start);
vpai80b6d012014-12-17 11:47:32 -0800147
Yang Gao5fd0d292015-01-26 00:19:48 -0800148 GPR_ASSERT((s.code() == grpc::StatusCode::OK) &&
149 (response.payload().type() ==
150 grpc::testing::PayloadType::COMPRESSABLE) &&
151 (response.payload().body().length() ==
Craig Tiller5c004c62015-02-23 16:31:57 -0800152 static_cast<size_t>(args.payload_size())));
vpai80b6d012014-12-17 11:47:32 -0800153
Yang Gao5fd0d292015-01-26 00:19:48 -0800154 // Now do runtime round-robin assignment of the next
155 // channel number
Craig Tiller5c004c62015-02-23 16:31:57 -0800156 channel_num += args.client_threads();
157 channel_num %= args.client_channels();
Yang Gao5fd0d292015-01-26 00:19:48 -0800158 }
159 },
Craig Tiller5c004c62015-02-23 16:31:57 -0800160 i % args.client_channels()));
vpai80b6d012014-12-17 11:47:32 -0800161 }
162
Craig Tillerb5dcec52015-01-13 11:13:42 -0800163 for (auto &t : threads) {
vpai80b6d012014-12-17 11:47:32 -0800164 t.join();
165 }
Craig Tiller056ba542015-01-31 21:15:10 -0800166
Craig Tiller5c004c62015-02-23 16:31:57 -0800167 gpr_timespec stop = gpr_now();
168
Craig Tiller056ba542015-01-31 21:15:10 -0800169 grpc_profiler_stop();
170
Craig Tiller5c004c62015-02-23 16:31:57 -0800171 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
172 GPR_ASSERT(hist != NULL);
173
174 for (int i = 0; i < args.client_threads(); i++) {
vpai80b6d012014-12-17 11:47:32 -0800175 gpr_histogram *h = thread_stats[i];
vpai92fe70e2015-01-13 11:21:38 -0800176 gpr_log(GPR_INFO, "latency at thread %d (50/90/95/99/99.9): %f/%f/%f/%f/%f",
177 i, gpr_histogram_percentile(h, 50), gpr_histogram_percentile(h, 90),
178 gpr_histogram_percentile(h, 95), gpr_histogram_percentile(h, 99),
vpai80b6d012014-12-17 11:47:32 -0800179 gpr_histogram_percentile(h, 99.9));
180 gpr_histogram_merge(hist, h);
181 gpr_histogram_destroy(h);
182 }
183
Craig Tiller5c004c62015-02-23 16:31:57 -0800184 ClientResult result;
185 auto* latencies = result.mutable_latencies();
186 latencies->set_l_50(gpr_histogram_percentile(hist, 50));
187 latencies->set_l_90(gpr_histogram_percentile(hist, 90));
188 latencies->set_l_99(gpr_histogram_percentile(hist, 99));
189 latencies->set_l_999(gpr_histogram_percentile(hist, 99.9));
190 gpr_timespec elapsed = gpr_time_sub(stop, start);
191 result.set_num_rpcs(args.client_threads() * args.num_rpcs());
192 result.set_time_elapsed(elapsed.tv_sec + 1e-9 * elapsed.tv_nsec);
193
vpai80b6d012014-12-17 11:47:32 -0800194 gpr_histogram_destroy(hist);
vpai92fe70e2015-01-13 11:21:38 -0800195
Craig Tiller5c004c62015-02-23 16:31:57 -0800196 return result;
197}
vpai92fe70e2015-01-13 11:21:38 -0800198
Craig Tiller5c004c62015-02-23 16:31:57 -0800199class ClientImpl : public QpsClient::Service {
200 public:
201
202 private:
203 std::mutex client_mu_;
204};
205
206static void RunServer() {
207 char* server_address = NULL;
208 gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
209
210 ClientImpl service;
211
212 ServerBuilder builder;
213 builder.AddPort(server_address);
214 builder.RegisterService(&service);
215
216 gpr_free(server_address);
217
218 auto server = builder.BuildAndStart();
219
220 while (!got_sigint) {
221 std::this_thread::sleep_for(std::chrono::seconds(5));
222 }
vpai80b6d012014-12-17 11:47:32 -0800223}
224
225int main(int argc, char **argv) {
226 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100227 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800228
Craig Tiller5c004c62015-02-23 16:31:57 -0800229 signal(SIGINT, sigint_handler);
vpai80b6d012014-12-17 11:47:32 -0800230
Craig Tiller5c004c62015-02-23 16:31:57 -0800231 RunServer();
vpai80b6d012014-12-17 11:47:32 -0800232
233 grpc_shutdown();
234 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800235}