blob: 214711e205be6a7181df8a469aa47a8af05a83ae [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"
Craig Tiller2d0f36c2015-02-23 23:16:17 -080057#include "test/cpp/qps/timer.h"
vpai80b6d012014-12-17 11:47:32 -080058
Craig Tiller5c004c62015-02-23 16:31:57 -080059DEFINE_int32(driver_port, 0, "Client driver port.");
vpai80b6d012014-12-17 11:47:32 -080060
61using grpc::ChannelInterface;
yangg59dfc902014-12-19 14:00:14 -080062using grpc::CreateTestChannel;
Craig Tiller5c004c62015-02-23 16:31:57 -080063using grpc::ServerBuilder;
Craig Tiller2d0f36c2015-02-23 23:16:17 -080064using grpc::ServerContext;
65using grpc::Status;
Craig Tiller5c004c62015-02-23 16:31:57 -080066using grpc::testing::ClientArgs;
Craig Tiller2d0f36c2015-02-23 23:16:17 -080067using grpc::testing::ClientConfig;
Craig Tiller5c004c62015-02-23 16:31:57 -080068using grpc::testing::ClientResult;
69using grpc::testing::QpsClient;
vpai80b6d012014-12-17 11:47:32 -080070using grpc::testing::SimpleRequest;
71using grpc::testing::SimpleResponse;
vpai92fe70e2015-01-13 11:21:38 -080072using grpc::testing::StatsRequest;
vpai80b6d012014-12-17 11:47:32 -080073using grpc::testing::TestService;
74
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010075// In some distros, gflags is in the namespace google, and in some others,
76// in gflags. This hack is enabling us to find both.
77namespace google { }
78namespace gflags { }
79using namespace google;
80using namespace gflags;
81
vpai80b6d012014-12-17 11:47:32 -080082static double now() {
83 gpr_timespec tv = gpr_now();
84 return 1e9 * tv.tv_sec + tv.tv_nsec;
85}
86
Craig Tiller5c004c62015-02-23 16:31:57 -080087static bool got_sigint = false;
88
89static void sigint_handler(int x) { got_sigint = 1; }
90
91ClientResult RunTest(const ClientArgs& args) {
Craig Tiller2d0f36c2015-02-23 23:16:17 -080092 const auto& config = args.config();
93
vpai80b6d012014-12-17 11:47:32 -080094 gpr_log(GPR_INFO,
95 "QPS test with parameters\n"
96 "enable_ssl = %d\n"
97 "client_channels = %d\n"
98 "client_threads = %d\n"
99 "num_rpcs = %d\n"
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800100 "payload_size = %d\n",
101 config.enable_ssl(), config.client_channels(), config.client_threads(), config.num_rpcs(),
102 config.payload_size());
vpai80b6d012014-12-17 11:47:32 -0800103
104 class ClientChannelInfo {
105 public:
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800106 explicit ClientChannelInfo(const grpc::string& target, const ClientConfig& config)
107 : channel_(CreateTestChannel(target, config.enable_ssl())),
vpai80b6d012014-12-17 11:47:32 -0800108 stub_(TestService::NewStub(channel_)) {}
109 ChannelInterface *get_channel() { return channel_.get(); }
110 TestService::Stub *get_stub() { return stub_.get(); }
111
112 private:
113 std::shared_ptr<ChannelInterface> channel_;
114 std::unique_ptr<TestService::Stub> stub_;
115 };
116
117 std::vector<ClientChannelInfo> channels;
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800118 for (int i = 0; i < config.client_channels(); i++) {
119 channels.push_back(ClientChannelInfo(args.server_targets(i % args.server_targets_size()), config));
vpai80b6d012014-12-17 11:47:32 -0800120 }
121
122 std::vector<std::thread> threads; // Will add threads when ready to execute
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800123 std::vector< ::gpr_histogram *> thread_stats(config.client_threads());
vpai80b6d012014-12-17 11:47:32 -0800124
vpai92fe70e2015-01-13 11:21:38 -0800125 grpc::ClientContext context_stats_begin;
vpai92fe70e2015-01-13 11:21:38 -0800126
Craig Tiller056ba542015-01-31 21:15:10 -0800127 grpc_profiler_start("qps_client.prof");
128
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800129 Timer timer;
Craig Tiller5c004c62015-02-23 16:31:57 -0800130
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800131 for (int i = 0; i < config.client_threads(); i++) {
vpai80b6d012014-12-17 11:47:32 -0800132 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
133 GPR_ASSERT(hist != NULL);
134 thread_stats[i] = hist;
135
Yang Gao5fd0d292015-01-26 00:19:48 -0800136 threads.push_back(
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800137 std::thread([hist, config, &channels](int channel_num) {
Yang Gao5fd0d292015-01-26 00:19:48 -0800138 SimpleRequest request;
139 SimpleResponse response;
140 request.set_response_type(
141 grpc::testing::PayloadType::COMPRESSABLE);
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800142 request.set_response_size(config.payload_size());
vpai80b6d012014-12-17 11:47:32 -0800143
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800144 for (int j = 0; j < config.num_rpcs(); j++) {
Yang Gao5fd0d292015-01-26 00:19:48 -0800145 TestService::Stub *stub =
146 channels[channel_num].get_stub();
147 double start = now();
148 grpc::ClientContext context;
149 grpc::Status s =
150 stub->UnaryCall(&context, request, &response);
151 gpr_histogram_add(hist, now() - start);
vpai80b6d012014-12-17 11:47:32 -0800152
Yang Gao5fd0d292015-01-26 00:19:48 -0800153 GPR_ASSERT((s.code() == grpc::StatusCode::OK) &&
154 (response.payload().type() ==
155 grpc::testing::PayloadType::COMPRESSABLE) &&
156 (response.payload().body().length() ==
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800157 static_cast<size_t>(config.payload_size())));
vpai80b6d012014-12-17 11:47:32 -0800158
Yang Gao5fd0d292015-01-26 00:19:48 -0800159 // Now do runtime round-robin assignment of the next
160 // channel number
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800161 channel_num += config.client_threads();
162 channel_num %= config.client_channels();
Yang Gao5fd0d292015-01-26 00:19:48 -0800163 }
164 },
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800165 i % config.client_channels()));
vpai80b6d012014-12-17 11:47:32 -0800166 }
167
Craig Tillerb5dcec52015-01-13 11:13:42 -0800168 for (auto &t : threads) {
vpai80b6d012014-12-17 11:47:32 -0800169 t.join();
170 }
Craig Tiller056ba542015-01-31 21:15:10 -0800171
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800172 auto timer_result = timer.Mark();
Craig Tiller5c004c62015-02-23 16:31:57 -0800173
Craig Tiller056ba542015-01-31 21:15:10 -0800174 grpc_profiler_stop();
175
Craig Tiller5c004c62015-02-23 16:31:57 -0800176 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
177 GPR_ASSERT(hist != NULL);
178
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800179 for (int i = 0; i < config.client_threads(); i++) {
vpai80b6d012014-12-17 11:47:32 -0800180 gpr_histogram *h = thread_stats[i];
vpai92fe70e2015-01-13 11:21:38 -0800181 gpr_log(GPR_INFO, "latency at thread %d (50/90/95/99/99.9): %f/%f/%f/%f/%f",
182 i, gpr_histogram_percentile(h, 50), gpr_histogram_percentile(h, 90),
183 gpr_histogram_percentile(h, 95), gpr_histogram_percentile(h, 99),
vpai80b6d012014-12-17 11:47:32 -0800184 gpr_histogram_percentile(h, 99.9));
185 gpr_histogram_merge(hist, h);
186 gpr_histogram_destroy(h);
187 }
188
Craig Tiller5c004c62015-02-23 16:31:57 -0800189 ClientResult result;
190 auto* latencies = result.mutable_latencies();
191 latencies->set_l_50(gpr_histogram_percentile(hist, 50));
192 latencies->set_l_90(gpr_histogram_percentile(hist, 90));
193 latencies->set_l_99(gpr_histogram_percentile(hist, 99));
194 latencies->set_l_999(gpr_histogram_percentile(hist, 99.9));
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800195 result.set_num_rpcs(config.client_threads() * config.num_rpcs());
196 result.set_time_elapsed(timer_result.wall);
197 result.set_time_system(timer_result.system);
198 result.set_time_user(timer_result.user);
Craig Tiller5c004c62015-02-23 16:31:57 -0800199
vpai80b6d012014-12-17 11:47:32 -0800200 gpr_histogram_destroy(hist);
vpai92fe70e2015-01-13 11:21:38 -0800201
Craig Tiller5c004c62015-02-23 16:31:57 -0800202 return result;
203}
vpai92fe70e2015-01-13 11:21:38 -0800204
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800205class ClientImpl final : public QpsClient::Service {
Craig Tiller5c004c62015-02-23 16:31:57 -0800206 public:
Craig Tiller2d0f36c2015-02-23 23:16:17 -0800207 Status RunTest(ServerContext* ctx, const ClientArgs* args, ClientResult* result) override {
208 *result = ::RunTest(*args);
209 return Status::OK;
210 }
Craig Tiller5c004c62015-02-23 16:31:57 -0800211
212 private:
213 std::mutex client_mu_;
214};
215
216static void RunServer() {
217 char* server_address = NULL;
218 gpr_join_host_port(&server_address, "::", FLAGS_driver_port);
219
220 ClientImpl service;
221
222 ServerBuilder builder;
223 builder.AddPort(server_address);
224 builder.RegisterService(&service);
225
226 gpr_free(server_address);
227
228 auto server = builder.BuildAndStart();
229
230 while (!got_sigint) {
231 std::this_thread::sleep_for(std::chrono::seconds(5));
232 }
vpai80b6d012014-12-17 11:47:32 -0800233}
234
235int main(int argc, char **argv) {
236 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100237 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800238
Craig Tiller5c004c62015-02-23 16:31:57 -0800239 signal(SIGINT, sigint_handler);
vpai80b6d012014-12-17 11:47:32 -0800240
Craig Tiller5c004c62015-02-23 16:31:57 -0800241 RunServer();
vpai80b6d012014-12-17 11:47:32 -0800242
243 grpc_shutdown();
244 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800245}