blob: 666f25054ab608ec521f7b6a99c074e7fa757641 [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>
36#include <string>
37#include <thread>
38#include <vector>
39#include <sstream>
40
41#include <grpc/grpc.h>
42#include <grpc/support/histogram.h>
43#include <grpc/support/log.h>
Nicolas "Pixel" Nobleba608202015-02-20 02:48:03 +010044#include <gflags/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080045#include <grpc++/client_context.h>
vpai80b6d012014-12-17 11:47:32 -080046#include <grpc++/status.h>
Craig Tiller056ba542015-01-31 21:15:10 -080047#include "test/core/util/grpc_profiler.h"
yangg59dfc902014-12-19 14:00:14 -080048#include "test/cpp/util/create_test_channel.h"
vpai92fe70e2015-01-13 11:21:38 -080049#include "test/cpp/qps/qpstest.pb.h"
vpai80b6d012014-12-17 11:47:32 -080050
51DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
52DEFINE_int32(server_port, 0, "Server port.");
53DEFINE_string(server_host, "127.0.0.1", "Server host.");
54DEFINE_int32(client_threads, 4, "Number of client threads.");
55
56// We have a configurable number of channels for sending RPCs.
57// RPCs are sent round-robin on the available channels by the
58// various threads. Interesting cases are 1 global channel or
59// 1 per-thread channel, but we can support any number.
60// The channels are assigned round-robin on an RPC by RPC basis
61// rather than just at initialization time in order to also measure the
62// impact of cache thrashing caused by channel changes. This is an issue
63// if you are not in one of the above "interesting cases"
64DEFINE_int32(client_channels, 4, "Number of client channels.");
65
66DEFINE_int32(num_rpcs, 1000, "Number of RPCs per thread.");
67DEFINE_int32(payload_size, 1, "Payload size in bytes");
68
69// Alternatively, specify parameters for test as a workload so that multiple
70// tests are initiated back-to-back. This is convenient for keeping a borg
71// allocation consistent. This is a space-separated list of
72// [threads channels num_rpcs payload_size ]*
73DEFINE_string(workload, "", "Workload parameters");
74
75using grpc::ChannelInterface;
yangg59dfc902014-12-19 14:00:14 -080076using grpc::CreateTestChannel;
vpai92fe70e2015-01-13 11:21:38 -080077using grpc::testing::ServerStats;
vpai80b6d012014-12-17 11:47:32 -080078using grpc::testing::SimpleRequest;
79using grpc::testing::SimpleResponse;
vpai92fe70e2015-01-13 11:21:38 -080080using grpc::testing::StatsRequest;
vpai80b6d012014-12-17 11:47:32 -080081using grpc::testing::TestService;
82
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +010083// In some distros, gflags is in the namespace google, and in some others,
84// in gflags. This hack is enabling us to find both.
85namespace google { }
86namespace gflags { }
87using namespace google;
88using namespace gflags;
89
vpai80b6d012014-12-17 11:47:32 -080090static double now() {
91 gpr_timespec tv = gpr_now();
92 return 1e9 * tv.tv_sec + tv.tv_nsec;
93}
94
95void RunTest(const int client_threads, const int client_channels,
96 const int num_rpcs, const int payload_size) {
97 gpr_log(GPR_INFO,
98 "QPS test with parameters\n"
99 "enable_ssl = %d\n"
100 "client_channels = %d\n"
101 "client_threads = %d\n"
102 "num_rpcs = %d\n"
103 "payload_size = %d\n"
104 "server_host:server_port = %s:%d\n\n",
105 FLAGS_enable_ssl, client_channels, client_threads, num_rpcs,
106 payload_size, FLAGS_server_host.c_str(), FLAGS_server_port);
107
108 std::ostringstream oss;
109 oss << FLAGS_server_host << ":" << FLAGS_server_port;
110
111 class ClientChannelInfo {
112 public:
113 explicit ClientChannelInfo(const grpc::string &server)
yangg59dfc902014-12-19 14:00:14 -0800114 : channel_(CreateTestChannel(server, FLAGS_enable_ssl)),
vpai80b6d012014-12-17 11:47:32 -0800115 stub_(TestService::NewStub(channel_)) {}
116 ChannelInterface *get_channel() { return channel_.get(); }
117 TestService::Stub *get_stub() { return stub_.get(); }
118
119 private:
120 std::shared_ptr<ChannelInterface> channel_;
121 std::unique_ptr<TestService::Stub> stub_;
122 };
123
124 std::vector<ClientChannelInfo> channels;
125 for (int i = 0; i < client_channels; i++) {
126 channels.push_back(ClientChannelInfo(oss.str()));
127 }
128
129 std::vector<std::thread> threads; // Will add threads when ready to execute
130 std::vector<::gpr_histogram *> thread_stats(client_threads);
131
vpai92fe70e2015-01-13 11:21:38 -0800132 TestService::Stub *stub_stats = channels[0].get_stub();
133 grpc::ClientContext context_stats_begin;
134 StatsRequest stats_request;
135 ServerStats server_stats_begin;
136 stats_request.set_test_num(0);
137 grpc::Status status_beg = stub_stats->CollectServerStats(
138 &context_stats_begin, stats_request, &server_stats_begin);
139
Craig Tiller056ba542015-01-31 21:15:10 -0800140 grpc_profiler_start("qps_client.prof");
141
vpai80b6d012014-12-17 11:47:32 -0800142 for (int i = 0; i < client_threads; i++) {
143 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
144 GPR_ASSERT(hist != NULL);
145 thread_stats[i] = hist;
146
Yang Gao5fd0d292015-01-26 00:19:48 -0800147 threads.push_back(
148 std::thread([hist, client_threads, client_channels, num_rpcs,
149 payload_size, &channels](int channel_num) {
150 SimpleRequest request;
151 SimpleResponse response;
152 request.set_response_type(
153 grpc::testing::PayloadType::COMPRESSABLE);
154 request.set_response_size(payload_size);
vpai80b6d012014-12-17 11:47:32 -0800155
Yang Gao5fd0d292015-01-26 00:19:48 -0800156 for (int j = 0; j < num_rpcs; j++) {
157 TestService::Stub *stub =
158 channels[channel_num].get_stub();
159 double start = now();
160 grpc::ClientContext context;
161 grpc::Status s =
162 stub->UnaryCall(&context, request, &response);
163 gpr_histogram_add(hist, now() - start);
vpai80b6d012014-12-17 11:47:32 -0800164
Yang Gao5fd0d292015-01-26 00:19:48 -0800165 GPR_ASSERT((s.code() == grpc::StatusCode::OK) &&
166 (response.payload().type() ==
167 grpc::testing::PayloadType::COMPRESSABLE) &&
168 (response.payload().body().length() ==
169 static_cast<size_t>(payload_size)));
vpai80b6d012014-12-17 11:47:32 -0800170
Yang Gao5fd0d292015-01-26 00:19:48 -0800171 // Now do runtime round-robin assignment of the next
172 // channel number
173 channel_num += client_threads;
174 channel_num %= client_channels;
175 }
176 },
177 i % client_channels));
vpai80b6d012014-12-17 11:47:32 -0800178 }
179
180 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
181 GPR_ASSERT(hist != NULL);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800182 for (auto &t : threads) {
vpai80b6d012014-12-17 11:47:32 -0800183 t.join();
184 }
Craig Tiller056ba542015-01-31 21:15:10 -0800185
186 grpc_profiler_stop();
187
vpai80b6d012014-12-17 11:47:32 -0800188 for (int i = 0; i < client_threads; i++) {
189 gpr_histogram *h = thread_stats[i];
vpai92fe70e2015-01-13 11:21:38 -0800190 gpr_log(GPR_INFO, "latency at thread %d (50/90/95/99/99.9): %f/%f/%f/%f/%f",
191 i, gpr_histogram_percentile(h, 50), gpr_histogram_percentile(h, 90),
192 gpr_histogram_percentile(h, 95), gpr_histogram_percentile(h, 99),
vpai80b6d012014-12-17 11:47:32 -0800193 gpr_histogram_percentile(h, 99.9));
194 gpr_histogram_merge(hist, h);
195 gpr_histogram_destroy(h);
196 }
197
198 gpr_log(
199 GPR_INFO,
200 "latency across %d threads with %d channels and %d payload "
vpai92fe70e2015-01-13 11:21:38 -0800201 "(50/90/95/99/99.9): %f / %f / %f / %f / %f",
vpai80b6d012014-12-17 11:47:32 -0800202 client_threads, client_channels, payload_size,
vpai92fe70e2015-01-13 11:21:38 -0800203 gpr_histogram_percentile(hist, 50), gpr_histogram_percentile(hist, 90),
204 gpr_histogram_percentile(hist, 95), gpr_histogram_percentile(hist, 99),
205 gpr_histogram_percentile(hist, 99.9));
vpai80b6d012014-12-17 11:47:32 -0800206 gpr_histogram_destroy(hist);
vpai92fe70e2015-01-13 11:21:38 -0800207
208 grpc::ClientContext context_stats_end;
209 ServerStats server_stats_end;
210 grpc::Status status_end = stub_stats->CollectServerStats(
211 &context_stats_end, stats_request, &server_stats_end);
212
213 double elapsed = server_stats_end.time_now() - server_stats_begin.time_now();
214 int total_rpcs = client_threads * num_rpcs;
215 double utime = server_stats_end.time_user() - server_stats_begin.time_user();
216 double stime =
217 server_stats_end.time_system() - server_stats_begin.time_system();
218 gpr_log(GPR_INFO,
219 "Elapsed time: %.3f\n"
220 "RPC Count: %d\n"
221 "QPS: %.3f\n"
222 "System time: %.3f\n"
223 "User time: %.3f\n"
224 "Resource usage: %.1f%%\n",
225 elapsed, total_rpcs, total_rpcs / elapsed, stime, utime,
226 (stime + utime) / elapsed * 100.0);
vpai80b6d012014-12-17 11:47:32 -0800227}
228
229int main(int argc, char **argv) {
230 grpc_init();
Nicolas "Pixel" Noble7e80efc2015-02-20 03:13:38 +0100231 ParseCommandLineFlags(&argc, &argv, true);
vpai80b6d012014-12-17 11:47:32 -0800232
233 GPR_ASSERT(FLAGS_server_port);
234
235 if (FLAGS_workload.length() == 0) {
236 RunTest(FLAGS_client_threads, FLAGS_client_channels, FLAGS_num_rpcs,
237 FLAGS_payload_size);
238 } else {
239 std::istringstream workload(FLAGS_workload);
240 int client_threads, client_channels, num_rpcs, payload_size;
241 workload >> client_threads;
242 while (!workload.eof()) {
243 workload >> client_channels >> num_rpcs >> payload_size;
244 RunTest(client_threads, client_channels, num_rpcs, payload_size);
245 workload >> client_threads;
246 }
247 gpr_log(GPR_INFO, "Done with specified workload.");
248 }
249
250 grpc_shutdown();
251 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800252}