xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/stress_interop_client.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 // is % allowed in string
17 //
18 
19 #ifndef GRPC_TEST_CPP_INTEROP_STRESS_INTEROP_CLIENT_H
20 #define GRPC_TEST_CPP_INTEROP_STRESS_INTEROP_CLIENT_H
21 
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include <grpcpp/create_channel.h>
27 
28 #include "test/cpp/interop/interop_client.h"
29 #include "test/cpp/util/metrics_server.h"
30 
31 namespace grpc {
32 namespace testing {
33 
34 using std::pair;
35 using std::vector;
36 
37 enum TestCaseType {
38   UNKNOWN_TEST = -1,
39   EMPTY_UNARY,
40   LARGE_UNARY,
41   CLIENT_COMPRESSED_UNARY,
42   CLIENT_COMPRESSED_STREAMING,
43   CLIENT_STREAMING,
44   SERVER_STREAMING,
45   SERVER_COMPRESSED_UNARY,
46   SERVER_COMPRESSED_STREAMING,
47   SLOW_CONSUMER,
48   HALF_DUPLEX,
49   PING_PONG,
50   CANCEL_AFTER_BEGIN,
51   CANCEL_AFTER_FIRST_RESPONSE,
52   TIMEOUT_ON_SLEEPING_SERVER,
53   EMPTY_STREAM,
54   STATUS_CODE_AND_MESSAGE,
55   CUSTOM_METADATA
56 };
57 
58 const vector<pair<TestCaseType, std::string>> kTestCaseList = {
59     {EMPTY_UNARY, "empty_unary"},
60     {LARGE_UNARY, "large_unary"},
61     {CLIENT_COMPRESSED_UNARY, "client_compressed_unary"},
62     {CLIENT_COMPRESSED_STREAMING, "client_compressed_streaming"},
63     {CLIENT_STREAMING, "client_streaming"},
64     {SERVER_STREAMING, "server_streaming"},
65     {SERVER_COMPRESSED_UNARY, "server_compressed_unary"},
66     {SERVER_COMPRESSED_STREAMING, "server_compressed_streaming"},
67     {SLOW_CONSUMER, "slow_consumer"},
68     {HALF_DUPLEX, "half_duplex"},
69     {PING_PONG, "ping_pong"},
70     {CANCEL_AFTER_BEGIN, "cancel_after_begin"},
71     {CANCEL_AFTER_FIRST_RESPONSE, "cancel_after_first_response"},
72     {TIMEOUT_ON_SLEEPING_SERVER, "timeout_on_sleeping_server"},
73     {EMPTY_STREAM, "empty_stream"},
74     {STATUS_CODE_AND_MESSAGE, "status_code_and_message"},
75     {CUSTOM_METADATA, "custom_metadata"}};
76 
77 class WeightedRandomTestSelector {
78  public:
79   // Takes a vector of <test_case, weight> pairs as the input
80   explicit WeightedRandomTestSelector(
81       const vector<pair<TestCaseType, int>>& tests);
82 
83   // Returns a weighted-randomly chosen test case based on the test cases and
84   // weights passed in the constructor
85   TestCaseType GetNextTest() const;
86 
87  private:
88   const vector<pair<TestCaseType, int>> tests_;
89   int total_weight_;
90 };
91 
92 class StressTestInteropClient {
93  public:
94   StressTestInteropClient(int test_id, const std::string& server_address,
95                           ChannelCreationFunc channel_creation_func,
96                           const WeightedRandomTestSelector& test_selector,
97                           long test_duration_secs, long sleep_duration_ms,
98                           bool do_not_abort_on_transient_failures);
99 
100   // The main function. Use this as the thread entry point.
101   // qps_gauge is the QpsGauge to record the requests per second metric
102   void MainLoop(const std::shared_ptr<QpsGauge>& qps_gauge);
103 
104  private:
105   bool RunTest(TestCaseType test_case);
106 
107   int test_id_;
108   const std::string& server_address_;
109   ChannelCreationFunc channel_creation_func_;
110   std::unique_ptr<InteropClient> interop_client_;
111   const WeightedRandomTestSelector& test_selector_;
112   long test_duration_secs_;
113   long sleep_duration_ms_;
114 };
115 
116 }  // namespace testing
117 }  // namespace grpc
118 
119 #endif  // GRPC_TEST_CPP_INTEROP_STRESS_INTEROP_CLIENT_H
120