blob: 8a33ab2ca3f0645f79d3c69f66d206c6bc250936 [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
vpai80b6d012014-12-17 11:47:32 -080083static double now() {
84 gpr_timespec tv = gpr_now();
85 return 1e9 * tv.tv_sec + tv.tv_nsec;
86}
87
88void RunTest(const int client_threads, const int client_channels,
89 const int num_rpcs, const int payload_size) {
90 gpr_log(GPR_INFO,
91 "QPS test with parameters\n"
92 "enable_ssl = %d\n"
93 "client_channels = %d\n"
94 "client_threads = %d\n"
95 "num_rpcs = %d\n"
96 "payload_size = %d\n"
97 "server_host:server_port = %s:%d\n\n",
98 FLAGS_enable_ssl, client_channels, client_threads, num_rpcs,
99 payload_size, FLAGS_server_host.c_str(), FLAGS_server_port);
100
101 std::ostringstream oss;
102 oss << FLAGS_server_host << ":" << FLAGS_server_port;
103
104 class ClientChannelInfo {
105 public:
106 explicit ClientChannelInfo(const grpc::string &server)
yangg59dfc902014-12-19 14:00:14 -0800107 : channel_(CreateTestChannel(server, FLAGS_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;
118 for (int i = 0; i < client_channels; i++) {
119 channels.push_back(ClientChannelInfo(oss.str()));
120 }
121
122 std::vector<std::thread> threads; // Will add threads when ready to execute
123 std::vector<::gpr_histogram *> thread_stats(client_threads);
124
vpai92fe70e2015-01-13 11:21:38 -0800125 TestService::Stub *stub_stats = channels[0].get_stub();
126 grpc::ClientContext context_stats_begin;
127 StatsRequest stats_request;
128 ServerStats server_stats_begin;
129 stats_request.set_test_num(0);
130 grpc::Status status_beg = stub_stats->CollectServerStats(
131 &context_stats_begin, stats_request, &server_stats_begin);
132
Craig Tiller056ba542015-01-31 21:15:10 -0800133 grpc_profiler_start("qps_client.prof");
134
vpai80b6d012014-12-17 11:47:32 -0800135 for (int i = 0; i < client_threads; i++) {
136 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
137 GPR_ASSERT(hist != NULL);
138 thread_stats[i] = hist;
139
Yang Gao5fd0d292015-01-26 00:19:48 -0800140 threads.push_back(
141 std::thread([hist, client_threads, client_channels, num_rpcs,
142 payload_size, &channels](int channel_num) {
143 SimpleRequest request;
144 SimpleResponse response;
145 request.set_response_type(
146 grpc::testing::PayloadType::COMPRESSABLE);
147 request.set_response_size(payload_size);
vpai80b6d012014-12-17 11:47:32 -0800148
Yang Gao5fd0d292015-01-26 00:19:48 -0800149 for (int j = 0; j < num_rpcs; j++) {
150 TestService::Stub *stub =
151 channels[channel_num].get_stub();
152 double start = now();
153 grpc::ClientContext context;
154 grpc::Status s =
155 stub->UnaryCall(&context, request, &response);
156 gpr_histogram_add(hist, now() - start);
vpai80b6d012014-12-17 11:47:32 -0800157
Yang Gao5fd0d292015-01-26 00:19:48 -0800158 GPR_ASSERT((s.code() == grpc::StatusCode::OK) &&
159 (response.payload().type() ==
160 grpc::testing::PayloadType::COMPRESSABLE) &&
161 (response.payload().body().length() ==
162 static_cast<size_t>(payload_size)));
vpai80b6d012014-12-17 11:47:32 -0800163
Yang Gao5fd0d292015-01-26 00:19:48 -0800164 // Now do runtime round-robin assignment of the next
165 // channel number
166 channel_num += client_threads;
167 channel_num %= client_channels;
168 }
169 },
170 i % client_channels));
vpai80b6d012014-12-17 11:47:32 -0800171 }
172
173 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
174 GPR_ASSERT(hist != NULL);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800175 for (auto &t : threads) {
vpai80b6d012014-12-17 11:47:32 -0800176 t.join();
177 }
Craig Tiller056ba542015-01-31 21:15:10 -0800178
179 grpc_profiler_stop();
180
vpai80b6d012014-12-17 11:47:32 -0800181 for (int i = 0; i < client_threads; i++) {
182 gpr_histogram *h = thread_stats[i];
vpai92fe70e2015-01-13 11:21:38 -0800183 gpr_log(GPR_INFO, "latency at thread %d (50/90/95/99/99.9): %f/%f/%f/%f/%f",
184 i, gpr_histogram_percentile(h, 50), gpr_histogram_percentile(h, 90),
185 gpr_histogram_percentile(h, 95), gpr_histogram_percentile(h, 99),
vpai80b6d012014-12-17 11:47:32 -0800186 gpr_histogram_percentile(h, 99.9));
187 gpr_histogram_merge(hist, h);
188 gpr_histogram_destroy(h);
189 }
190
191 gpr_log(
192 GPR_INFO,
193 "latency across %d threads with %d channels and %d payload "
vpai92fe70e2015-01-13 11:21:38 -0800194 "(50/90/95/99/99.9): %f / %f / %f / %f / %f",
vpai80b6d012014-12-17 11:47:32 -0800195 client_threads, client_channels, payload_size,
vpai92fe70e2015-01-13 11:21:38 -0800196 gpr_histogram_percentile(hist, 50), gpr_histogram_percentile(hist, 90),
197 gpr_histogram_percentile(hist, 95), gpr_histogram_percentile(hist, 99),
198 gpr_histogram_percentile(hist, 99.9));
vpai80b6d012014-12-17 11:47:32 -0800199 gpr_histogram_destroy(hist);
vpai92fe70e2015-01-13 11:21:38 -0800200
201 grpc::ClientContext context_stats_end;
202 ServerStats server_stats_end;
203 grpc::Status status_end = stub_stats->CollectServerStats(
204 &context_stats_end, stats_request, &server_stats_end);
205
206 double elapsed = server_stats_end.time_now() - server_stats_begin.time_now();
207 int total_rpcs = client_threads * num_rpcs;
208 double utime = server_stats_end.time_user() - server_stats_begin.time_user();
209 double stime =
210 server_stats_end.time_system() - server_stats_begin.time_system();
211 gpr_log(GPR_INFO,
212 "Elapsed time: %.3f\n"
213 "RPC Count: %d\n"
214 "QPS: %.3f\n"
215 "System time: %.3f\n"
216 "User time: %.3f\n"
217 "Resource usage: %.1f%%\n",
218 elapsed, total_rpcs, total_rpcs / elapsed, stime, utime,
219 (stime + utime) / elapsed * 100.0);
vpai80b6d012014-12-17 11:47:32 -0800220}
221
222int main(int argc, char **argv) {
223 grpc_init();
224 google::ParseCommandLineFlags(&argc, &argv, true);
225
226 GPR_ASSERT(FLAGS_server_port);
227
228 if (FLAGS_workload.length() == 0) {
229 RunTest(FLAGS_client_threads, FLAGS_client_channels, FLAGS_num_rpcs,
230 FLAGS_payload_size);
231 } else {
232 std::istringstream workload(FLAGS_workload);
233 int client_threads, client_channels, num_rpcs, payload_size;
234 workload >> client_threads;
235 while (!workload.eof()) {
236 workload >> client_channels >> num_rpcs >> payload_size;
237 RunTest(client_threads, client_channels, num_rpcs, payload_size);
238 workload >> client_threads;
239 }
240 gpr_log(GPR_INFO, "Done with specified workload.");
241 }
242
243 grpc_shutdown();
244 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800245}