vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 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 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 34 | #include <cassert> |
| 35 | #include <memory> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 36 | #include <mutex> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 37 | #include <string> |
| 38 | #include <thread> |
| 39 | #include <vector> |
| 40 | #include <sstream> |
| 41 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 42 | #include <sys/signal.h> |
| 43 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 44 | #include <grpc/grpc.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 45 | #include <grpc/support/alloc.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 46 | #include <grpc/support/histogram.h> |
| 47 | #include <grpc/support/log.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 48 | #include <grpc/support/host_port.h> |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 49 | #include <gflags/gflags.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 50 | #include <grpc++/client_context.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 51 | #include <grpc++/status.h> |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 52 | #include <grpc++/server.h> |
| 53 | #include <grpc++/server_builder.h> |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 54 | #include "test/core/util/grpc_profiler.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 55 | #include "test/cpp/util/create_test_channel.h" |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 56 | #include "test/cpp/qps/qpstest.pb.h" |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 57 | #include "test/cpp/qps/timer.h" |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 58 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 59 | DEFINE_int32(driver_port, 0, "Client driver port."); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 60 | |
| 61 | using grpc::ChannelInterface; |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 62 | using grpc::CreateTestChannel; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 63 | using grpc::ServerBuilder; |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 64 | using grpc::ServerContext; |
| 65 | using grpc::Status; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 66 | using grpc::testing::ClientArgs; |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 67 | using grpc::testing::ClientConfig; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 68 | using grpc::testing::ClientResult; |
| 69 | using grpc::testing::QpsClient; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 70 | using grpc::testing::SimpleRequest; |
| 71 | using grpc::testing::SimpleResponse; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 72 | using grpc::testing::StatsRequest; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 73 | using grpc::testing::TestService; |
| 74 | |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 75 | // In some distros, gflags is in the namespace google, and in some others, |
| 76 | // in gflags. This hack is enabling us to find both. |
| 77 | namespace google { } |
| 78 | namespace gflags { } |
| 79 | using namespace google; |
| 80 | using namespace gflags; |
| 81 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 82 | static double now() { |
| 83 | gpr_timespec tv = gpr_now(); |
| 84 | return 1e9 * tv.tv_sec + tv.tv_nsec; |
| 85 | } |
| 86 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 87 | static bool got_sigint = false; |
| 88 | |
| 89 | static void sigint_handler(int x) { got_sigint = 1; } |
| 90 | |
| 91 | ClientResult RunTest(const ClientArgs& args) { |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 92 | const auto& config = args.config(); |
| 93 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 94 | 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 Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 100 | "payload_size = %d\n", |
| 101 | config.enable_ssl(), config.client_channels(), config.client_threads(), config.num_rpcs(), |
| 102 | config.payload_size()); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 103 | |
| 104 | class ClientChannelInfo { |
| 105 | public: |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 106 | explicit ClientChannelInfo(const grpc::string& target, const ClientConfig& config) |
| 107 | : channel_(CreateTestChannel(target, config.enable_ssl())), |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 108 | 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 Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 118 | for (int i = 0; i < config.client_channels(); i++) { |
| 119 | channels.push_back(ClientChannelInfo(args.server_targets(i % args.server_targets_size()), config)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | std::vector<std::thread> threads; // Will add threads when ready to execute |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 123 | std::vector< ::gpr_histogram *> thread_stats(config.client_threads()); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 124 | |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 125 | grpc::ClientContext context_stats_begin; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 126 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 127 | grpc_profiler_start("qps_client.prof"); |
| 128 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 129 | Timer timer; |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 130 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 131 | for (int i = 0; i < config.client_threads(); i++) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 132 | gpr_histogram *hist = gpr_histogram_create(0.01, 60e9); |
| 133 | GPR_ASSERT(hist != NULL); |
| 134 | thread_stats[i] = hist; |
| 135 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 136 | threads.push_back( |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 137 | std::thread([hist, config, &channels](int channel_num) { |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 138 | SimpleRequest request; |
| 139 | SimpleResponse response; |
| 140 | request.set_response_type( |
| 141 | grpc::testing::PayloadType::COMPRESSABLE); |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 142 | request.set_response_size(config.payload_size()); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 143 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 144 | for (int j = 0; j < config.num_rpcs(); j++) { |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 145 | 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); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 152 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 153 | GPR_ASSERT((s.code() == grpc::StatusCode::OK) && |
| 154 | (response.payload().type() == |
| 155 | grpc::testing::PayloadType::COMPRESSABLE) && |
| 156 | (response.payload().body().length() == |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 157 | static_cast<size_t>(config.payload_size()))); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 158 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 159 | // Now do runtime round-robin assignment of the next |
| 160 | // channel number |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 161 | channel_num += config.client_threads(); |
| 162 | channel_num %= config.client_channels(); |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 163 | } |
| 164 | }, |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 165 | i % config.client_channels())); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 168 | for (auto &t : threads) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 169 | t.join(); |
| 170 | } |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 171 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 172 | auto timer_result = timer.Mark(); |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 173 | |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 174 | grpc_profiler_stop(); |
| 175 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 176 | gpr_histogram *hist = gpr_histogram_create(0.01, 60e9); |
| 177 | GPR_ASSERT(hist != NULL); |
| 178 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 179 | for (int i = 0; i < config.client_threads(); i++) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 180 | gpr_histogram *h = thread_stats[i]; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 181 | 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), |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 184 | gpr_histogram_percentile(h, 99.9)); |
| 185 | gpr_histogram_merge(hist, h); |
| 186 | gpr_histogram_destroy(h); |
| 187 | } |
| 188 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 189 | 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 Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 195 | 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 Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 199 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 200 | gpr_histogram_destroy(hist); |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 201 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 202 | return result; |
| 203 | } |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 204 | |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 205 | class ClientImpl final : public QpsClient::Service { |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 206 | public: |
Craig Tiller | 2d0f36c | 2015-02-23 23:16:17 -0800 | [diff] [blame^] | 207 | Status RunTest(ServerContext* ctx, const ClientArgs* args, ClientResult* result) override { |
| 208 | *result = ::RunTest(*args); |
| 209 | return Status::OK; |
| 210 | } |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 211 | |
| 212 | private: |
| 213 | std::mutex client_mu_; |
| 214 | }; |
| 215 | |
| 216 | static 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 | } |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | int main(int argc, char **argv) { |
| 236 | grpc_init(); |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 237 | ParseCommandLineFlags(&argc, &argv, true); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 238 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 239 | signal(SIGINT, sigint_handler); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 240 | |
Craig Tiller | 5c004c6 | 2015-02-23 16:31:57 -0800 | [diff] [blame] | 241 | RunServer(); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 242 | |
| 243 | grpc_shutdown(); |
| 244 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 245 | } |