rpc_press.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. 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,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gflags/gflags.h>
  18. #include <google/protobuf/dynamic_message.h>
  19. #include <google/protobuf/compiler/importer.h>
  20. #include <brpc/server.h>
  21. #include <butil/logging.h>
  22. #include <butil/string_splitter.h>
  23. #include <string.h>
  24. #include "rpc_press_impl.h"
  25. DEFINE_int32(dummy_port, 8888, "Port of dummy server");
  26. DEFINE_string(proto, "", " user's proto files with path");
  27. DEFINE_string(inc, "", "Include paths for proto, separated by semicolon(;)");
  28. DEFINE_string(method, "example.EchoService.Echo", "The full method name");
  29. DEFINE_string(server, "0.0.0.0:8002", "ip:port of the server when -load_balancer is empty, the naming service otherwise");
  30. DEFINE_string(input, "", "The file containing requests in json format");
  31. DEFINE_string(output, "", "The file containing responses in json format");
  32. DEFINE_string(lb_policy, "", "The load balancer algorithm: rr, random, la, c_murmurhash, c_md5");
  33. DEFINE_int32(thread_num, 0, "Number of threads to send requests. 0: automatically chosen according to -qps");
  34. DEFINE_string(protocol, "baidu_std", "baidu_std hulu_pbrpc sofa_pbrpc http public_pbrpc nova_pbrpc ubrpc_compack...");
  35. DEFINE_string(connection_type, "", "Type of connections: single, pooled, short");
  36. DEFINE_int32(timeout_ms, 1000, "RPC timeout in milliseconds");
  37. DEFINE_int32(connection_timeout_ms, 500, " connection timeout in milliseconds");
  38. DEFINE_int32(max_retry, 3, "Maximum retry times by RPC framework");
  39. DEFINE_int32(request_compress_type, 0, "Snappy:1 Gzip:2 Zlib:3 LZ4:4 None:0");
  40. DEFINE_int32(response_compress_type, 0, "Snappy:1 Gzip:2 Zlib:3 LZ4:4 None:0");
  41. DEFINE_int32(attachment_size, 0, "Carry so many byte attachment along with requests");
  42. DEFINE_int32(duration, 0, "how many seconds the press keep");
  43. DEFINE_int32(qps, 100 , "how many calls per seconds");
  44. DEFINE_bool(pretty, true, "output pretty jsons");
  45. bool set_press_options(pbrpcframework::PressOptions* options){
  46. size_t dot_pos = FLAGS_method.find_last_of('.');
  47. if (dot_pos == std::string::npos) {
  48. LOG(ERROR) << "-method must be in form of: package.service.method";
  49. return false;
  50. }
  51. options->service = FLAGS_method.substr(0, dot_pos);
  52. options->method = FLAGS_method.substr(dot_pos + 1);
  53. options->lb_policy = FLAGS_lb_policy;
  54. options->test_req_rate = FLAGS_qps;
  55. if (FLAGS_thread_num > 0) {
  56. options->test_thread_num = FLAGS_thread_num;
  57. } else {
  58. if (FLAGS_qps <= 0) { // unlimited qps
  59. options->test_thread_num = 50;
  60. } else {
  61. options->test_thread_num = FLAGS_qps / 10000;
  62. if (options->test_thread_num < 1) {
  63. options->test_thread_num = 1;
  64. }
  65. if (options->test_thread_num > 50) {
  66. options->test_thread_num = 50;
  67. }
  68. }
  69. }
  70. options->input = FLAGS_input;
  71. options->output = FLAGS_output;
  72. options->connection_type = FLAGS_connection_type;
  73. options->connect_timeout_ms = FLAGS_connection_timeout_ms;
  74. options->timeout_ms = FLAGS_timeout_ms;
  75. options->max_retry = FLAGS_max_retry;
  76. options->protocol = FLAGS_protocol;
  77. options->request_compress_type = FLAGS_request_compress_type;
  78. options->response_compress_type = FLAGS_response_compress_type;
  79. options->attachment_size = FLAGS_attachment_size;
  80. options->host = FLAGS_server;
  81. options->proto_file = FLAGS_proto;
  82. options->proto_includes = FLAGS_inc;
  83. return true;
  84. }
  85. int main(int argc, char* argv[]) {
  86. // Parse gflags. We recommend you to use gflags as well
  87. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  88. // set global log option
  89. if (FLAGS_dummy_port >= 0) {
  90. brpc::StartDummyServerAt(FLAGS_dummy_port);
  91. }
  92. pbrpcframework::PressOptions options;
  93. if (!set_press_options(&options)) {
  94. return -1;
  95. }
  96. pbrpcframework::RpcPress* rpc_press = new pbrpcframework::RpcPress;
  97. if (0 != rpc_press->init(&options)) {
  98. LOG(FATAL) << "Fail to init rpc_press";
  99. return -1;
  100. }
  101. rpc_press->start();
  102. if (FLAGS_duration <= 0) {
  103. while (!brpc::IsAskedToQuit()) {
  104. sleep(1);
  105. }
  106. } else {
  107. sleep(FLAGS_duration);
  108. }
  109. rpc_press->stop();
  110. // NOTE(gejun): Can't delete rpc_press on exit. It's probably
  111. // used by concurrently running done.
  112. return 0;
  113. }