stress_interop_client.h 3.6 KB

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