Home | History | Annotate | Download | only in interop
      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_STRESS_INTEROP_CLIENT_H
     20 #define GRPC_TEST_CPP_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, grpc::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   WeightedRandomTestSelector(const vector<pair<TestCaseType, int>>& tests);
     81 
     82   // Returns a weighted-randomly chosen test case based on the test cases and
     83   // weights passed in the constructor
     84   TestCaseType GetNextTest() const;
     85 
     86  private:
     87   const vector<pair<TestCaseType, int>> tests_;
     88   int total_weight_;
     89 };
     90 
     91 class StressTestInteropClient {
     92  public:
     93   StressTestInteropClient(int test_id, const grpc::string& server_address,
     94                           ChannelCreationFunc channel_creation_func,
     95                           const WeightedRandomTestSelector& test_selector,
     96                           long test_duration_secs, long sleep_duration_ms,
     97                           bool do_not_abort_on_transient_failures);
     98 
     99   // The main function. Use this as the thread entry point.
    100   // qps_gauge is the QpsGauge to record the requests per second metric
    101   void MainLoop(const std::shared_ptr<QpsGauge>& qps_gauge);
    102 
    103  private:
    104   bool RunTest(TestCaseType test_case);
    105 
    106   int test_id_;
    107   const grpc::string& server_address_;
    108   ChannelCreationFunc channel_creation_func_;
    109   std::unique_ptr<InteropClient> interop_client_;
    110   const WeightedRandomTestSelector& test_selector_;
    111   long test_duration_secs_;
    112   long sleep_duration_ms_;
    113 };
    114 
    115 }  // namespace testing
    116 }  // namespace grpc
    117 
    118 #endif  // GRPC_TEST_CPP_STRESS_INTEROP_CLIENT_H
    119