blob: 287cef819240ced389f30b483444365cccbcffb3 [file] [log] [blame]
vpai80b6d012014-12-17 11:47:32 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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
34
35#include <cassert>
36#include <memory>
37#include <string>
38#include <thread>
39#include <vector>
40#include <sstream>
41
42#include <grpc/grpc.h>
43#include <grpc/support/histogram.h>
44#include <grpc/support/log.h>
45#include <google/gflags.h>
vpai80b6d012014-12-17 11:47:32 -080046#include <grpc++/client_context.h>
vpai80b6d012014-12-17 11:47:32 -080047#include <grpc++/status.h>
yangg59dfc902014-12-19 14:00:14 -080048#include "test/cpp/util/create_test_channel.h"
vpai80b6d012014-12-17 11:47:32 -080049#include "test/cpp/interop/test.pb.h"
50
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;
vpai80b6d012014-12-17 11:47:32 -080077using grpc::testing::SimpleRequest;
78using grpc::testing::SimpleResponse;
79using grpc::testing::TestService;
80
vpai80b6d012014-12-17 11:47:32 -080081static double now() {
82 gpr_timespec tv = gpr_now();
83 return 1e9 * tv.tv_sec + tv.tv_nsec;
84}
85
86void RunTest(const int client_threads, const int client_channels,
87 const int num_rpcs, const int payload_size) {
88 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"
95 "server_host:server_port = %s:%d\n\n",
96 FLAGS_enable_ssl, client_channels, client_threads, num_rpcs,
97 payload_size, FLAGS_server_host.c_str(), FLAGS_server_port);
98
99 std::ostringstream oss;
100 oss << FLAGS_server_host << ":" << FLAGS_server_port;
101
102 class ClientChannelInfo {
103 public:
104 explicit ClientChannelInfo(const grpc::string &server)
yangg59dfc902014-12-19 14:00:14 -0800105 : channel_(CreateTestChannel(server, FLAGS_enable_ssl)),
vpai80b6d012014-12-17 11:47:32 -0800106 stub_(TestService::NewStub(channel_)) {}
107 ChannelInterface *get_channel() { return channel_.get(); }
108 TestService::Stub *get_stub() { return stub_.get(); }
109
110 private:
111 std::shared_ptr<ChannelInterface> channel_;
112 std::unique_ptr<TestService::Stub> stub_;
113 };
114
115 std::vector<ClientChannelInfo> channels;
116 for (int i = 0; i < client_channels; i++) {
117 channels.push_back(ClientChannelInfo(oss.str()));
118 }
119
120 std::vector<std::thread> threads; // Will add threads when ready to execute
121 std::vector<::gpr_histogram *> thread_stats(client_threads);
122
123 for (int i = 0; i < client_threads; i++) {
124 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
125 GPR_ASSERT(hist != NULL);
126 thread_stats[i] = hist;
127
128 threads.push_back(std::thread(
129 [hist, client_threads, client_channels, num_rpcs, payload_size,
130 &channels](int channel_num) {
131 SimpleRequest request;
132 SimpleResponse response;
133 request.set_response_type(grpc::testing::PayloadType::COMPRESSABLE);
134 request.set_response_size(payload_size);
135
136 for (int j = 0; j < num_rpcs; j++) {
137 TestService::Stub *stub = channels[channel_num].get_stub();
138 double start = now();
139 grpc::ClientContext context;
140 grpc::Status s = stub->UnaryCall(&context, request, &response);
141 gpr_histogram_add(hist, now() - start);
142
143 GPR_ASSERT((s.code() == grpc::StatusCode::OK) &&
144 (response.payload().type() ==
145 grpc::testing::PayloadType::COMPRESSABLE) &&
146 (response.payload().body().length() ==
147 static_cast<size_t>(payload_size)));
148
149 // Now do runtime round-robin assignment of the next channel number
150 channel_num += client_threads;
151 channel_num %= client_channels;
152 }
153 },
154 i % client_channels));
155 }
156
157 gpr_histogram *hist = gpr_histogram_create(0.01, 60e9);
158 GPR_ASSERT(hist != NULL);
159 for (auto& t : threads) {
160 t.join();
161 }
162 for (int i = 0; i < client_threads; i++) {
163 gpr_histogram *h = thread_stats[i];
164 gpr_log(GPR_INFO, "latency at thread %d (50/95/99/99.9): %f/%f/%f/%f",
165 i,
166 gpr_histogram_percentile(h, 50),
167 gpr_histogram_percentile(h, 95),
168 gpr_histogram_percentile(h, 99),
169 gpr_histogram_percentile(h, 99.9));
170 gpr_histogram_merge(hist, h);
171 gpr_histogram_destroy(h);
172 }
173
174 gpr_log(
175 GPR_INFO,
176 "latency across %d threads with %d channels and %d payload "
177 "(50/95/99/99.9): %f / %f / %f / %f",
178 client_threads, client_channels, payload_size,
179 gpr_histogram_percentile(hist, 50), gpr_histogram_percentile(hist, 95),
180 gpr_histogram_percentile(hist, 99), gpr_histogram_percentile(hist, 99.9));
181 gpr_histogram_destroy(hist);
182}
183
184int main(int argc, char **argv) {
185 grpc_init();
186 google::ParseCommandLineFlags(&argc, &argv, true);
187
188 GPR_ASSERT(FLAGS_server_port);
189
190 if (FLAGS_workload.length() == 0) {
191 RunTest(FLAGS_client_threads, FLAGS_client_channels, FLAGS_num_rpcs,
192 FLAGS_payload_size);
193 } else {
194 std::istringstream workload(FLAGS_workload);
195 int client_threads, client_channels, num_rpcs, payload_size;
196 workload >> client_threads;
197 while (!workload.eof()) {
198 workload >> client_channels >> num_rpcs >> payload_size;
199 RunTest(client_threads, client_channels, num_rpcs, payload_size);
200 workload >> client_threads;
201 }
202 gpr_log(GPR_INFO, "Done with specified workload.");
203 }
204
205 grpc_shutdown();
206 return 0;
207}