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> |
| 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> |
| 44 | #include <google/gflags.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 45 | #include <grpc++/client_context.h> |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 46 | #include <grpc++/status.h> |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 47 | #include "test/core/util/grpc_profiler.h" |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 48 | #include "test/cpp/util/create_test_channel.h" |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 49 | #include "test/cpp/qps/qpstest.pb.h" |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 50 | |
| 51 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
| 52 | DEFINE_int32(server_port, 0, "Server port."); |
| 53 | DEFINE_string(server_host, "127.0.0.1", "Server host."); |
| 54 | DEFINE_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" |
| 64 | DEFINE_int32(client_channels, 4, "Number of client channels."); |
| 65 | |
| 66 | DEFINE_int32(num_rpcs, 1000, "Number of RPCs per thread."); |
| 67 | DEFINE_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 ]* |
| 73 | DEFINE_string(workload, "", "Workload parameters"); |
| 74 | |
| 75 | using grpc::ChannelInterface; |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 76 | using grpc::CreateTestChannel; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 77 | using grpc::testing::ServerStats; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 78 | using grpc::testing::SimpleRequest; |
| 79 | using grpc::testing::SimpleResponse; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 80 | using grpc::testing::StatsRequest; |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 81 | using grpc::testing::TestService; |
| 82 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 83 | static double now() { |
| 84 | gpr_timespec tv = gpr_now(); |
| 85 | return 1e9 * tv.tv_sec + tv.tv_nsec; |
| 86 | } |
| 87 | |
| 88 | void 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) |
yangg | 59dfc90 | 2014-12-19 14:00:14 -0800 | [diff] [blame] | 107 | : channel_(CreateTestChannel(server, FLAGS_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; |
| 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 | |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 125 | 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 Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 133 | grpc_profiler_start("qps_client.prof"); |
| 134 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 135 | 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 Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 140 | 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); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 148 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 149 | 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); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 157 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 158 | 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))); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 163 | |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 164 | // 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)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | gpr_histogram *hist = gpr_histogram_create(0.01, 60e9); |
| 174 | GPR_ASSERT(hist != NULL); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 175 | for (auto &t : threads) { |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 176 | t.join(); |
| 177 | } |
Craig Tiller | 056ba54 | 2015-01-31 21:15:10 -0800 | [diff] [blame] | 178 | |
| 179 | grpc_profiler_stop(); |
| 180 | |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 181 | for (int i = 0; i < client_threads; i++) { |
| 182 | gpr_histogram *h = thread_stats[i]; |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 183 | 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), |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 186 | 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 " |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 194 | "(50/90/95/99/99.9): %f / %f / %f / %f / %f", |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 195 | client_threads, client_channels, payload_size, |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 196 | 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)); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 199 | gpr_histogram_destroy(hist); |
vpai | 92fe70e | 2015-01-13 11:21:38 -0800 | [diff] [blame] | 200 | |
| 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); |
vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int 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 Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame^] | 245 | } |