blob: 0b940e6cb37f6e78e3791bf246efe9ec25558c30 [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>
44
45#include "test/cpp/interop/interop_client.h"
46#include "test/cpp/interop/stress_interop_client.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070047#include "test/cpp/util/metrics_server.h"
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070048#include "test/cpp/util/test_config.h"
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070049#include "test/proto/metrics.grpc.pb.h"
50#include "test/proto/metrics.pb.h"
51
52DEFINE_int32(metrics_port, 8081, "The metrics server port.");
53
54DEFINE_int32(metrics_collection_interval_secs, 5,
55 "How often (in seconds) should metrics be recorded.");
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070056
57DEFINE_int32(sleep_duration_ms, 0,
58 "The duration (in millisec) between two"
59 " consecutive test calls (per server) issued by the server.");
60
61DEFINE_int32(test_duration_secs, -1,
62 "The length of time (in seconds) to run"
63 " the test. Enter -1 if the test should run continuously until"
64 " forcefully terminated.");
65
66DEFINE_string(server_addresses, "localhost:8080",
67 "The list of server"
68 " addresses in the format:\n"
69 " \"<name_1>:<port_1>,<name_2>:<port_1>...<name_N>:<port_N>\"\n"
70 " Note: <name> can be servername or IP address.");
71
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -070072DEFINE_int32(num_stubs_per_channel, 1,
73 "Number of stubs per each channels to server. This number also "
74 "indicates the max number of parallel RPC calls on each channel "
75 "at any given time.");
76
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070077// TODO(sreek): Add more test cases here in future
78DEFINE_string(test_cases, "",
79 "List of test cases to call along with the"
80 " relative weights in the following format:\n"
81 " \"<testcase_1:w_1>,<testcase_2:w_2>...<testcase_n:w_n>\"\n"
82 " The following testcases are currently supported:\n"
83 " empty_unary\n"
84 " large_unary\n"
85 " large_compressed_unary\n"
86 " client_streaming\n"
87 " server_streaming\n"
88 " empty_stream\n"
89 " Example: \"empty_unary:20,large_unary:10,empty_stream:70\"\n"
90 " The above will execute 'empty_unary', 20% of the time,"
91 " 'large_unary', 10% of the time and 'empty_stream' the remaining"
92 " 70% of the time");
93
94using std::make_pair;
95using std::pair;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -070096using std::thread;
97using std::vector;
98
99using grpc::testing::kTestCaseList;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700100using grpc::testing::MetricsService;
101using grpc::testing::MetricsServiceImpl;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700102using grpc::testing::StressTestInteropClient;
103using grpc::testing::TestCaseType;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700104using grpc::testing::UNKNOWN_TEST;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700105using grpc::testing::WeightedRandomTestSelector;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700106
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700107TestCaseType GetTestTypeFromName(const grpc::string& test_name) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700108 TestCaseType test_case = UNKNOWN_TEST;
109
110 for (auto it = kTestCaseList.begin(); it != kTestCaseList.end(); it++) {
111 if (test_name == it->second) {
112 test_case = it->first;
113 break;
114 }
115 }
116
117 return test_case;
118}
119
120// Converts a string of comma delimited tokens to a vector of tokens
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700121bool ParseCommaDelimitedString(const grpc::string& comma_delimited_str,
122 vector<grpc::string>& tokens) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700123 size_t bpos = 0;
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700124 size_t epos = grpc::string::npos;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700125
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700126 while ((epos = comma_delimited_str.find(',', bpos)) != grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700127 tokens.emplace_back(comma_delimited_str.substr(bpos, epos - bpos));
128 bpos = epos + 1;
129 }
130
131 tokens.emplace_back(comma_delimited_str.substr(bpos)); // Last token
132 return true;
133}
134
135// Input: Test case string "<testcase_name:weight>,<testcase_name:weight>...."
136// Output:
137// - Whether parsing was successful (return value)
138// - Vector of (test_type_enum, weight) pairs returned via 'tests' parameter
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700139bool ParseTestCasesString(const grpc::string& test_cases,
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700140 vector<pair<TestCaseType, int>>& tests) {
141 bool is_success = true;
142
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700143 vector<grpc::string> tokens;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700144 ParseCommaDelimitedString(test_cases, tokens);
145
146 for (auto it = tokens.begin(); it != tokens.end(); it++) {
147 // Token is in the form <test_name>:<test_weight>
148 size_t colon_pos = it->find(':');
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700149 if (colon_pos == grpc::string::npos) {
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700150 gpr_log(GPR_ERROR, "Error in parsing test case string: %s", it->c_str());
151 is_success = false;
152 break;
153 }
154
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700155 grpc::string test_name = it->substr(0, colon_pos);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700156 int weight = std::stoi(it->substr(colon_pos + 1));
157 TestCaseType test_case = GetTestTypeFromName(test_name);
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700158 if (test_case == UNKNOWN_TEST) {
159 gpr_log(GPR_ERROR, "Unknown test case: %s", test_name.c_str());
160 is_success = false;
161 break;
162 }
163
164 tests.emplace_back(std::make_pair(test_case, weight));
165 }
166
167 return is_success;
168}
169
170// For debugging purposes
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700171void LogParameterInfo(const vector<grpc::string>& addresses,
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700172 const vector<pair<TestCaseType, int>>& tests) {
173 gpr_log(GPR_INFO, "server_addresses: %s", FLAGS_server_addresses.c_str());
174 gpr_log(GPR_INFO, "test_cases : %s", FLAGS_test_cases.c_str());
175 gpr_log(GPR_INFO, "sleep_duration_ms: %d", FLAGS_sleep_duration_ms);
176 gpr_log(GPR_INFO, "test_duration_secs: %d", FLAGS_test_duration_secs);
177
178 int num = 0;
179 for (auto it = addresses.begin(); it != addresses.end(); it++) {
180 gpr_log(GPR_INFO, "%d:%s", ++num, it->c_str());
181 }
182
183 num = 0;
184 for (auto it = tests.begin(); it != tests.end(); it++) {
185 TestCaseType test_case = it->first;
186 int weight = it->second;
187 gpr_log(GPR_INFO, "%d. TestCaseType: %d, Weight: %d", ++num, test_case,
188 weight);
189 }
190}
191
192int main(int argc, char** argv) {
193 grpc::testing::InitTest(&argc, &argv, true);
194
195 srand(time(NULL));
196
197 // Parse the server addresses
Sree Kuchibhotlaf51ea7a2015-10-26 13:34:41 -0700198 vector<grpc::string> server_addresses;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700199 ParseCommaDelimitedString(FLAGS_server_addresses, server_addresses);
200
201 // Parse test cases and weights
202 if (FLAGS_test_cases.length() == 0) {
Sree Kuchibhotlae6cd0e72015-10-22 15:56:07 -0700203 gpr_log(GPR_INFO, "Not running tests. The 'test_cases' string is empty");
Sree Kuchibhotla117c8af2015-10-26 10:59:17 -0700204 return 1;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700205 }
206
207 vector<pair<TestCaseType, int>> tests;
208 if (!ParseTestCasesString(FLAGS_test_cases, tests)) {
209 gpr_log(GPR_ERROR, "Error in parsing test cases string %s ",
210 FLAGS_test_cases.c_str());
211 return 1;
212 }
213
214 LogParameterInfo(server_addresses, tests);
215
216 WeightedRandomTestSelector test_selector(tests);
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700217 MetricsServiceImpl metrics_service;
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700218
219 gpr_log(GPR_INFO, "Starting test(s)..");
Sree Kuchibhotla728a6102015-10-16 10:56:31 -0700220
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700221 vector<thread> test_threads;
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700222
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700223 int thread_idx = 0;
224 for (auto it = server_addresses.begin(); it != server_addresses.end(); it++) {
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700225 // TODO(sreek): This will change once we add support for other tests
226 // that won't work with InsecureCredentials()
227 std::shared_ptr<grpc::Channel> channel(
228 CreateChannel(*it, grpc::InsecureCredentials()));
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700229
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700230 // Make multiple stubs (as defined by num_stubs_per_channel flag) to use the
231 // same channel. This is to test calling multiple RPC calls in parallel on
232 // each channel.
233 for (int i = 0; i < FLAGS_num_stubs_per_channel; i++) {
234 StressTestInteropClient* client = new StressTestInteropClient(
235 ++thread_idx, *it, channel, test_selector, FLAGS_test_duration_secs,
236 FLAGS_sleep_duration_ms, FLAGS_metrics_collection_interval_secs);
237
238 bool is_already_created;
239 grpc::string metricName = "/stress_test/rps/thread/" + std::to_string(i);
240 test_threads.emplace_back(
241 thread(&StressTestInteropClient::MainLoop, client,
242 metrics_service.CreateGuage(metricName, is_already_created)));
243
244 // The Guage should not have been already created
245 GPR_ASSERT(!is_already_created);
246 }
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700247 }
248
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700249 // Start metrics server before waiting for the stress test threads
250 std::unique_ptr<grpc::Server> metrics_server =
251 metrics_service.StartServer(FLAGS_metrics_port);
252
253 // Wait for the stress test threads to complete
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700254 for (auto it = test_threads.begin(); it != test_threads.end(); it++) {
255 it->join();
256 }
257
Sree Kuchibhotlab5e98c52015-10-27 22:55:26 -0700258 metrics_server->Wait();
Sree Kuchibhotlafbc376f2015-10-16 10:56:31 -0700259 return 0;
260}