blob: 2999d6aae2cbfd8bb87d5e46fec13eaeb2f35227 [file] [log] [blame]
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -07001/*
2 *
3 * Copyright 2015, 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 *is % allowed in string
32 */
33
34#include <memory>
35#include <string>
36#include <thread>
37#include <utility>
38#include <vector>
39
40#include <gflags/gflags.h>
41#include <grpc/support/time.h>
42#include <grpc++/create_channel.h>
43#include <grpc++/grpc++.h>
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -080044#include <grpc++/impl/thd.h>
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070045
46#include "test/cpp/interop/interop_client.h"
47#include "test/cpp/interop/stress_interop_client.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070048#include "test/cpp/util/metrics_server.h"
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070049#include "test/cpp/util/test_config.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070050#include "test/proto/metrics.grpc.pb.h"
51#include "test/proto/metrics.pb.h"
52
53DEFINE_int32(metrics_port, 8081, "The metrics server port.");
54
55DEFINE_int32(metrics_collection_interval_secs, 5,
56 "How often (in seconds) should metrics be recorded.");
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070057
58DEFINE_int32(sleep_duration_ms, 0,
59 "The duration (in millisec) between two"
60 " consecutive test calls (per server) issued by the server.");
61
62DEFINE_int32(test_duration_secs, -1,
63 "The length of time (in seconds) to run"
64 " the test. Enter -1 if the test should run continuously until"
65 " forcefully terminated.");
66
67DEFINE_string(server_addresses, "localhost:8080",
68 "The list of server"
69 " addresses in the format:\n"
70 " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n"
71 " Note: <name> can be servername or IP address.");
72
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070073DEFINE_int32(num_stubs_per_channel, 1,
74 "Number of stubs per each channels to server. This number also "
75 "indicates the max number of parallel RPC calls on each channel "
76 "at any given time.");
77
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070078// TODO(sreek): Add more test cases here in future
79DEFINE_string(test_cases, "",
80 "List of test cases to call along with the"
81 " relative weights in the following format:\n"
82 " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n"
83 " The following testcases are currently supported:\n"
84 " empty_unary\n"
85 " large_unary\n"
86 " large_compressed_unary\n"
87 " client_streaming\n"
88 " server_streaming\n"
89 " empty_stream\n"
90 " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n"
91 " The above will execute 'empty_unary', 20% of the time,"
92 " 'large_unary', 10% of the time and 'empty_stream' the remaining"
93 " 70% of the time");
94
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070095using grpc::testing::kTestCaseList;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070096using grpc::testing::MetricsService;
97using grpc::testing::MetricsServiceImpl;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070098using grpc::testing::StressTestInteropClient;
99using grpc::testing::TestCaseType;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700100using grpc::testing::UNKNOWN_TEST;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700101using grpc::testing::WeightedRandomTestSelector;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700102
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700103TestCaseType GetTestTypeFromName(const grpc::string& test_name) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700104 TestCaseType test_case = UNKNOWN_TEST;
105
106 for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) {
107 if (test_name == it->second) {
108 test_case = it->first;
109 break;
110 }
111 }
112
113 return test_case;
114}
115
116// Converts a string of comma delimited tokens to a vector of tokens
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700117bool ParseCommaDelimitedString(const grpc::string& comma_delimited_str,
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800118 std::vector<grpc::string>& tokens) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700119 size_t bpos = 0;
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700120 size_t epos = grpc::string::npos;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700121
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700122 while ((epos = comma_delimited_str.find(',', bpos)) != grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700123 tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos));
124 bpos = epos + 1;
125 }
126
127 tokens.emplace_back(comma_delimited_str.substr(bpos)); // Last token
128 return true;
129}
130
131// Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...."
132// Output:
133// - Whether parsing was successful (return value)
134// - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700135bool ParseTestCasesString(const grpc::string& test_cases,
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800136 std::vector<std::pair<TestCaseType, int>>& tests) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700137 bool is_success = true;
138
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800139 std::vector<grpc::string> tokens;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700140 ParseCommaDelimitedString(test_cases, tokens);
141
142 for (auto it = tokens.begin(); it != tokens.end(); it++) {
143 // Token is in the form <test_name>:<test_weight>
144 size_t colon_pos = it->find(':');
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700145 if (colon_pos == grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700146 gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str());
147 is_success = false;
148 break;
149 }
150
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700151 grpc::string test_name = it->substr(0, colon_pos);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700152 int weight = std::stoi(it->substr(colon_pos + 1));
153 TestCaseType test_case = GetTestTypeFromName(test_name);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700154 if (test_case == UNKNOWN_TEST) {
155 gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str());
156 is_success = false;
157 break;
158 }
159
160 tests.emplace_back(std::make_pair(test_case, weight));
161 }
162
163 return is_success;
164}
165
166// For debugging purposes
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800167void LogParameterInfo(const std::vector<grpc::string>& addresses,
168 const std::vector<std::pair<TestCaseType, int>>& tests) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700169 gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str());
170 gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str());
171 gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms);
172 gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs);
173
174 int num = 0;
175 for (auto it = addresses.begin(); it != addresses.end(); it++) {
176 gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str());
177 }
178
179 num = 0;
180 for (auto it = tests.begin(); it != tests.end(); it++) {
181 TestCaseType test_case = it->first;
182 int weight = it->second;
183 gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case,
184 weight);
185 }
186}
187
188int main(int argc, char** argv) {
189 grpc::testing::InitTest(&argc, &argv, true);
190
191 srand(time(NULL));
192
193 // Parse the server addresses
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800194 std::vector<grpc::string> server_addresses;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700195 ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses);
196
197 // Parse test cases and weights
198 if (FLAGS_test_cases.length() == 0) {
Sree Kuchibhotlae6cd0e72015-10-22 15:56:07 -0700199 gpr_log(GPR_INFO, "Not running tests. The 'test_cases' string is empty");
Sree Kuchibhotla117c8af2015-10-26 10:59:17 -0700200 return 1;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700201 }
202
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800203 std::vector<std::pair<TestCaseType, int>> tests;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700204 if (!ParseTestCasesString(FLAGS_test_cases, tests)) {
205 gpr_log(GPR_ERROR, "Error in parsing test cases string %s ",
206 FLAGS_test_cases.c_str());
207 return 1;
208 }
209
210 LogParameterInfo(server_addresses, tests);
211
212 WeightedRandomTestSelector test_selector(tests);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700213 MetricsServiceImpl metrics_service;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700214
215 gpr_log(GPR_INFO, "Starting test(s)..");
Sree Kuchibhotla728a6102015-10-16 10:56:31 -0700216
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800217 std::vector<grpc::thread> test_threads;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700218
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700219 int thread_idx = 0;
220 for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700221 // TODO(sreek): This will change once we add support for other tests
222 // that won't work with InsecureCredentials()
223 std::shared_ptr<grpc::Channel> channel(
224 CreateChannel(*it, grpc::InsecureCredentials()));
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700225
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700226 // Make multiple stubs (as defined by num_stubs_per_channel flag) to use the
227 // same channel. This is to test calling multiple RPC calls in parallel on
228 // each channel.
229 for (int i = 0; i < FLAGS_num_stubs_per_channel; i++) {
230 StressTestInteropClient* client = new StressTestInteropClient(
231 ++thread_idx, *it, channel, test_selector, FLAGS_test_duration_secs,
232 FLAGS_sleep_duration_ms, FLAGS_metrics_collection_interval_secs);
233
234 bool is_already_created;
Sree Kuchibhotlac36b4802015-11-04 14:25:05 -0800235 grpc::string metricName =
236 "/stress_test/qps/thread/" + std::to_string(thread_idx);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700237 test_threads.emplace_back(
Sree Kuchibhotlab047c0f2015-11-16 11:52:54 -0800238 grpc::thread(&StressTestInteropClient::MainLoop, client,
239 metrics_service.CreateGauge(metricName, &is_already_created)));
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700240
Sree Kuchibhotla4d0f2f92015-11-03 15:55:43 -0800241 // The Gauge should not have been already created
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700242 GPR_ASSERT(!is_already_created);
243 }
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700244 }
245
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700246 // Start metrics server before waiting for the stress test threads
247 std::unique_ptr<grpc::Server> metrics_server =
248 metrics_service.StartServer(FLAGS_metrics_port);
249
250 // Wait for the stress test threads to complete
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700251 for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
252 it->join();
253 }
254
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700255 metrics_server->Wait();
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700256 return 0;
257}