client.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // A client sending requests to server by multiple threads.
  18. #include <gflags/gflags.h>
  19. #include <bthread/bthread.h>
  20. #include <butil/logging.h>
  21. #include <brpc/server.h>
  22. #include <brpc/channel.h>
  23. #include "echo.pb.h"
  24. #include <bvar/bvar.h>
  25. DEFINE_int32(thread_num, 50, "Number of threads to send requests");
  26. DEFINE_bool(use_bthread, false, "Use bthread to send requests");
  27. DEFINE_int32(attachment_size, 0, "Carry so many byte attachment along with requests");
  28. DEFINE_int32(request_size, 16, "Bytes of each request");
  29. DEFINE_string(server, "0.0.0.0:8002", "IP Address of server");
  30. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  31. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  32. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  33. DEFINE_bool(dont_fail, false, "Print fatal when some call failed");
  34. DEFINE_int32(dummy_port, -1, "Launch dummy server at this port");
  35. std::string g_request;
  36. std::string g_attachment;
  37. bvar::LatencyRecorder g_latency_recorder("client");
  38. bvar::Adder<int> g_error_count("client_error_count");
  39. static void* sender(void* arg) {
  40. // Normally, you should not call a Channel directly, but instead construct
  41. // a stub Service wrapping it. stub can be shared by all threads as well.
  42. example::EchoService_Stub stub(static_cast<google::protobuf::RpcChannel*>(arg));
  43. int log_id = 0;
  44. example::EchoRequest request;
  45. example::EchoResponse response;
  46. brpc::Controller cntl;
  47. while (!brpc::IsAskedToQuit()) {
  48. // We will receive response synchronously, safe to put variables
  49. // on stack.
  50. request.Clear();
  51. response.Clear();
  52. cntl.Reset();
  53. request.set_message(g_request);
  54. cntl.set_log_id(log_id++); // set by user
  55. if (!g_attachment.empty()) {
  56. // Set attachment which is wired to network directly instead of
  57. // being serialized into protobuf messages.
  58. cntl.request_attachment().append(g_attachment);
  59. }
  60. // Because `done'(last parameter) is NULL, this function waits until
  61. // the response comes back or error occurs(including timedout).
  62. stub.Echo(&cntl, &request, &response, NULL);
  63. if (!cntl.Failed()) {
  64. g_latency_recorder << cntl.latency_us();
  65. } else {
  66. g_error_count << 1;
  67. CHECK(brpc::IsAskedToQuit() || !FLAGS_dont_fail)
  68. << "error=" << cntl.ErrorText() << " latency=" << cntl.latency_us();
  69. // We can't connect to the server, sleep a while. Notice that this
  70. // is a specific sleeping to prevent this thread from spinning too
  71. // fast. You should continue the business logic in a production
  72. // server rather than sleeping.
  73. bthread_usleep(50000);
  74. }
  75. }
  76. return NULL;
  77. }
  78. int main(int argc, char* argv[]) {
  79. // Parse gflags. We recommend you to use gflags as well.
  80. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  81. // A Channel represents a communication line to a Server. Notice that
  82. // Channel is thread-safe and can be shared by all threads in your program.
  83. brpc::Channel channel;
  84. // Initialize the channel, NULL means using default options.
  85. brpc::ChannelOptions options;
  86. options.protocol = "nshead_mcpack";
  87. options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
  88. options.max_retry = FLAGS_max_retry;
  89. if (channel.Init(FLAGS_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
  90. LOG(ERROR) << "Fail to initialize channel";
  91. return -1;
  92. }
  93. if (FLAGS_attachment_size > 0) {
  94. g_attachment.resize(FLAGS_attachment_size, 'a');
  95. }
  96. if (FLAGS_request_size <= 0) {
  97. LOG(ERROR) << "Bad request_size=" << FLAGS_request_size;
  98. return -1;
  99. }
  100. g_request.resize(FLAGS_request_size, 'r');
  101. if (FLAGS_dummy_port >= 0) {
  102. brpc::StartDummyServerAt(FLAGS_dummy_port);
  103. }
  104. std::vector<bthread_t> bids;
  105. std::vector<pthread_t> pids;
  106. if (!FLAGS_use_bthread) {
  107. pids.resize(FLAGS_thread_num);
  108. for (int i = 0; i < FLAGS_thread_num; ++i) {
  109. if (pthread_create(&pids[i], NULL, sender, &channel) != 0) {
  110. LOG(ERROR) << "Fail to create pthread";
  111. return -1;
  112. }
  113. }
  114. } else {
  115. bids.resize(FLAGS_thread_num);
  116. for (int i = 0; i < FLAGS_thread_num; ++i) {
  117. if (bthread_start_background(
  118. &bids[i], NULL, sender, &channel) != 0) {
  119. LOG(ERROR) << "Fail to create bthread";
  120. return -1;
  121. }
  122. }
  123. }
  124. while (!brpc::IsAskedToQuit()) {
  125. sleep(1);
  126. LOG(INFO) << "Sending requests at qps=" << g_latency_recorder.qps(1)
  127. << " latency=" << g_latency_recorder.latency(1);
  128. }
  129. LOG(INFO) << "EchoClient is going to quit";
  130. for (int i = 0; i < FLAGS_thread_num; ++i) {
  131. if (!FLAGS_use_bthread) {
  132. pthread_join(pids[i], NULL);
  133. } else {
  134. bthread_join(bids[i], NULL);
  135. }
  136. }
  137. return 0;
  138. }