vpai | 80b6d01 | 2014-12-17 11:47:32 -0800 | [diff] [blame^] | 1 | /* |
| 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> |
| 46 | #include <grpc++/channel_interface.h> |
| 47 | #include <grpc++/client_context.h> |
| 48 | #include <grpc++/create_channel.h> |
| 49 | #include <grpc++/status.h> |
| 50 | #include "test/cpp/util/test_ssl_channel.h" |
| 51 | #include "test/cpp/interop/test.pb.h" |
| 52 | |
| 53 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
| 54 | DEFINE_int32(server_port, 0, "Server port."); |
| 55 | DEFINE_string(server_host, "127.0.0.1", "Server host."); |
| 56 | DEFINE_int32(client_threads, 4, "Number of client threads."); |
| 57 | |
| 58 | // We have a configurable number of channels for sending RPCs. |
| 59 | // RPCs are sent round-robin on the available channels by the |
| 60 | // various threads. Interesting cases are 1 global channel or |
| 61 | // 1 per-thread channel, but we can support any number. |
| 62 | // The channels are assigned round-robin on an RPC by RPC basis |
| 63 | // rather than just at initialization time in order to also measure the |
| 64 | // impact of cache thrashing caused by channel changes. This is an issue |
| 65 | // if you are not in one of the above "interesting cases" |
| 66 | DEFINE_int32(client_channels, 4, "Number of client channels."); |
| 67 | |
| 68 | DEFINE_int32(num_rpcs, 1000, "Number of RPCs per thread."); |
| 69 | DEFINE_int32(payload_size, 1, "Payload size in bytes"); |
| 70 | |
| 71 | // Alternatively, specify parameters for test as a workload so that multiple |
| 72 | // tests are initiated back-to-back. This is convenient for keeping a borg |
| 73 | // allocation consistent. This is a space-separated list of |
| 74 | // [threads channels num_rpcs payload_size ]* |
| 75 | DEFINE_string(workload, "", "Workload parameters"); |
| 76 | |
| 77 | using grpc::ChannelInterface; |
| 78 | using grpc::CreateChannel; |
| 79 | using grpc::TestSslChannel; |
| 80 | using grpc::testing::SimpleRequest; |
| 81 | using grpc::testing::SimpleResponse; |
| 82 | using grpc::testing::TestService; |
| 83 | |
| 84 | std::shared_ptr<ChannelInterface> CreateTestChannel( |
| 85 | const grpc::string& server) { |
| 86 | std::shared_ptr<ChannelInterface> channel; |
| 87 | if (FLAGS_enable_ssl) { |
| 88 | channel.reset(new TestSslChannel(server)); |
| 89 | } else { |
| 90 | channel = CreateChannel(server); |
| 91 | } |
| 92 | return channel; |
| 93 | } |
| 94 | |
| 95 | static double now() { |
| 96 | gpr_timespec tv = gpr_now(); |
| 97 | return 1e9 * tv.tv_sec + tv.tv_nsec; |
| 98 | } |
| 99 | |
| 100 | void RunTest(const int client_threads, const int client_channels, |
| 101 | const int num_rpcs, const int payload_size) { |
| 102 | gpr_log(GPR_INFO, |
| 103 | "QPS test with parameters\n" |
| 104 | "enable_ssl = %d\n" |
| 105 | "client_channels = %d\n" |
| 106 | "client_threads = %d\n" |
| 107 | "num_rpcs = %d\n" |
| 108 | "payload_size = %d\n" |
| 109 | "server_host:server_port = %s:%d\n\n", |
| 110 | FLAGS_enable_ssl, client_channels, client_threads, num_rpcs, |
| 111 | payload_size, FLAGS_server_host.c_str(), FLAGS_server_port); |
| 112 | |
| 113 | std::ostringstream oss; |
| 114 | oss << FLAGS_server_host << ":" << FLAGS_server_port; |
| 115 | |
| 116 | class ClientChannelInfo { |
| 117 | public: |
| 118 | explicit ClientChannelInfo(const grpc::string &server) |
| 119 | : channel_(CreateTestChannel(server)), |
| 120 | stub_(TestService::NewStub(channel_)) {} |
| 121 | ChannelInterface *get_channel() { return channel_.get(); } |
| 122 | TestService::Stub *get_stub() { return stub_.get(); } |
| 123 | |
| 124 | private: |
| 125 | std::shared_ptr<ChannelInterface> channel_; |
| 126 | std::unique_ptr<TestService::Stub> stub_; |
| 127 | }; |
| 128 | |
| 129 | std::vector<ClientChannelInfo> channels; |
| 130 | for (int i = 0; i < client_channels; i++) { |
| 131 | channels.push_back(ClientChannelInfo(oss.str())); |
| 132 | } |
| 133 | |
| 134 | std::vector<std::thread> threads; // Will add threads when ready to execute |
| 135 | std::vector<::gpr_histogram *> thread_stats(client_threads); |
| 136 | |
| 137 | for (int i = 0; i < client_threads; i++) { |
| 138 | gpr_histogram *hist = gpr_histogram_create(0.01, 60e9); |
| 139 | GPR_ASSERT(hist != NULL); |
| 140 | thread_stats[i] = hist; |
| 141 | |
| 142 | threads.push_back(std::thread( |
| 143 | [hist, client_threads, client_channels, num_rpcs, payload_size, |
| 144 | &channels](int channel_num) { |
| 145 | SimpleRequest request; |
| 146 | SimpleResponse response; |
| 147 | request.set_response_type(grpc::testing::PayloadType::COMPRESSABLE); |
| 148 | request.set_response_size(payload_size); |
| 149 | |
| 150 | for (int j = 0; j < num_rpcs; j++) { |
| 151 | TestService::Stub *stub = channels[channel_num].get_stub(); |
| 152 | double start = now(); |
| 153 | grpc::ClientContext context; |
| 154 | grpc::Status s = stub->UnaryCall(&context, request, &response); |
| 155 | gpr_histogram_add(hist, now() - start); |
| 156 | |
| 157 | GPR_ASSERT((s.code() == grpc::StatusCode::OK) && |
| 158 | (response.payload().type() == |
| 159 | grpc::testing::PayloadType::COMPRESSABLE) && |
| 160 | (response.payload().body().length() == |
| 161 | static_cast<size_t>(payload_size))); |
| 162 | |
| 163 | // Now do runtime round-robin assignment of the next channel number |
| 164 | channel_num += client_threads; |
| 165 | channel_num %= client_channels; |
| 166 | } |
| 167 | }, |
| 168 | i % client_channels)); |
| 169 | } |
| 170 | |
| 171 | gpr_histogram *hist = gpr_histogram_create(0.01, 60e9); |
| 172 | GPR_ASSERT(hist != NULL); |
| 173 | for (auto& t : threads) { |
| 174 | t.join(); |
| 175 | } |
| 176 | for (int i = 0; i < client_threads; i++) { |
| 177 | gpr_histogram *h = thread_stats[i]; |
| 178 | gpr_log(GPR_INFO, "latency at thread %d (50/95/99/99.9): %f/%f/%f/%f", |
| 179 | i, |
| 180 | gpr_histogram_percentile(h, 50), |
| 181 | gpr_histogram_percentile(h, 95), |
| 182 | gpr_histogram_percentile(h, 99), |
| 183 | gpr_histogram_percentile(h, 99.9)); |
| 184 | gpr_histogram_merge(hist, h); |
| 185 | gpr_histogram_destroy(h); |
| 186 | } |
| 187 | |
| 188 | gpr_log( |
| 189 | GPR_INFO, |
| 190 | "latency across %d threads with %d channels and %d payload " |
| 191 | "(50/95/99/99.9): %f / %f / %f / %f", |
| 192 | client_threads, client_channels, payload_size, |
| 193 | gpr_histogram_percentile(hist, 50), gpr_histogram_percentile(hist, 95), |
| 194 | gpr_histogram_percentile(hist, 99), gpr_histogram_percentile(hist, 99.9)); |
| 195 | gpr_histogram_destroy(hist); |
| 196 | } |
| 197 | |
| 198 | int main(int argc, char **argv) { |
| 199 | grpc_init(); |
| 200 | google::ParseCommandLineFlags(&argc, &argv, true); |
| 201 | |
| 202 | GPR_ASSERT(FLAGS_server_port); |
| 203 | |
| 204 | if (FLAGS_workload.length() == 0) { |
| 205 | RunTest(FLAGS_client_threads, FLAGS_client_channels, FLAGS_num_rpcs, |
| 206 | FLAGS_payload_size); |
| 207 | } else { |
| 208 | std::istringstream workload(FLAGS_workload); |
| 209 | int client_threads, client_channels, num_rpcs, payload_size; |
| 210 | workload >> client_threads; |
| 211 | while (!workload.eof()) { |
| 212 | workload >> client_channels >> num_rpcs >> payload_size; |
| 213 | RunTest(client_threads, client_channels, num_rpcs, payload_size); |
| 214 | workload >> client_threads; |
| 215 | } |
| 216 | gpr_log(GPR_INFO, "Done with specified workload."); |
| 217 | } |
| 218 | |
| 219 | grpc_shutdown(); |
| 220 | return 0; |
| 221 | } |