1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include <memory>
20 #include <thread>
21
22 #include "absl/flags/flag.h"
23 #include "absl/strings/str_split.h"
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/gpr/string.h"
30 #include "test/core/util/test_config.h"
31 #include "test/cpp/interop/client_helper.h"
32 #include "test/cpp/interop/interop_client.h"
33 #include "test/cpp/util/create_test_channel.h"
34 #include "test/cpp/util/test_config.h"
35
36 ABSL_FLAG(std::string, server_uris, "",
37 "Comma-separated list of sever URIs to make RPCs to");
38 ABSL_FLAG(std::string, credentials_types, "",
39 "Comma-separated list of credentials, each entry is used for the "
40 "server of the corresponding index in server_uris. Supported values: "
41 "compute_engine_channel_creds, INSECURE_CREDENTIALS");
42 ABSL_FLAG(int32_t, soak_iterations, 10,
43 "The number of iterations to use for the two soak tests: rpc_soak "
44 "and channel_soak");
45 ABSL_FLAG(int32_t, soak_max_failures, 0,
46 "The number of iterations in soak tests that are allowed to fail "
47 "(either due to non-OK status code or exceeding the per-iteration "
48 "max acceptable latency).");
49 ABSL_FLAG(int32_t, soak_per_iteration_max_acceptable_latency_ms, 1000,
50 "The number of milliseconds a single iteration in the two soak tests "
51 "(rpc_soak and channel_soak) should take.");
52 ABSL_FLAG(
53 int32_t, soak_overall_timeout_seconds, 10,
54 "The overall number of seconds after which a soak test should stop and "
55 "fail, if the desired number of iterations have not yet completed.");
56 ABSL_FLAG(int32_t, soak_min_time_ms_between_rpcs, 0,
57 "The minimum time in milliseconds between consecutive RPCs in a soak "
58 "test (rpc_soak or channel_soak), useful for limiting QPS");
59 ABSL_FLAG(
60 int32_t, soak_request_size, 271828,
61 "The request size in a soak RPC. "
62 "The default value is set based on the interop large unary test case.");
63 ABSL_FLAG(
64 int32_t, soak_response_size, 314159,
65 "The response size in a soak RPCi. "
66 "The default value is set based on the interop large unary test case.");
67 ABSL_FLAG(std::string, test_case, "rpc_soak",
68 "Configure different test cases. Valid options are: "
69 "rpc_soak: sends --soak_iterations large_unary RPCs; "
70 "channel_soak: sends --soak_iterations RPCs, rebuilding the channel "
71 "each time");
72
main(int argc,char ** argv)73 int main(int argc, char** argv) {
74 grpc::testing::TestEnvironment env(&argc, argv);
75 grpc::testing::InitTest(&argc, &argv, true);
76 gpr_log(GPR_INFO, "Testing these cases: %s",
77 absl::GetFlag(FLAGS_test_case).c_str());
78 std::string test_case = absl::GetFlag(FLAGS_test_case);
79 // validate flags
80 std::vector<std::string> uris =
81 absl::StrSplit(absl::GetFlag(FLAGS_server_uris), ',');
82 std::vector<std::string> creds =
83 absl::StrSplit(absl::GetFlag(FLAGS_credentials_types), ',');
84 if (uris.size() != creds.size()) {
85 gpr_log(GPR_ERROR,
86 "Number of entries in --server_uris %ld != number of entries in "
87 "--credentials_types %ld",
88 uris.size(), creds.size());
89 GPR_ASSERT(0);
90 }
91 if (uris.empty()) {
92 gpr_log(GPR_ERROR, "--server_uris has zero entries");
93 GPR_ASSERT(0);
94 }
95 // construct and start clients
96 std::vector<std::thread> threads;
97 for (size_t i = 0; i < uris.size(); i++) {
98 threads.push_back(std::thread([uris, creds, i, test_case]() {
99 auto channel_creation_func = [uris, creds, i](grpc::ChannelArguments) {
100 return grpc::CreateTestChannel(uris[i], creds[i],
101 nullptr /* call creds */);
102 };
103 grpc::testing::InteropClient client(channel_creation_func, true, false);
104 if (test_case == "rpc_soak") {
105 client.DoRpcSoakTest(
106 uris[i], absl::GetFlag(FLAGS_soak_iterations),
107 absl::GetFlag(FLAGS_soak_max_failures),
108 absl::GetFlag(FLAGS_soak_per_iteration_max_acceptable_latency_ms),
109 absl::GetFlag(FLAGS_soak_min_time_ms_between_rpcs),
110 absl::GetFlag(FLAGS_soak_overall_timeout_seconds),
111 absl::GetFlag(FLAGS_soak_request_size),
112 absl::GetFlag(FLAGS_soak_response_size));
113 } else if (test_case == "channel_soak") {
114 client.DoChannelSoakTest(
115 uris[i], absl::GetFlag(FLAGS_soak_iterations),
116 absl::GetFlag(FLAGS_soak_max_failures),
117 absl::GetFlag(FLAGS_soak_per_iteration_max_acceptable_latency_ms),
118 absl::GetFlag(FLAGS_soak_min_time_ms_between_rpcs),
119 absl::GetFlag(FLAGS_soak_overall_timeout_seconds),
120 absl::GetFlag(FLAGS_soak_request_size),
121 absl::GetFlag(FLAGS_soak_response_size));
122 } else {
123 gpr_log(GPR_ERROR,
124 "Invalid test case, must be either rpc_soak or channel_soak");
125 GPR_ASSERT(0);
126 }
127 }));
128 }
129 for (auto& thd : threads) {
130 thd.join();
131 }
132 gpr_log(GPR_INFO, "All clients done!");
133 return 0;
134 }
135