client.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2014 Baidu, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // A client sending requests to server which will send the request to itself
  15. // again according to the field `depth'
  16. #include <gflags/gflags.h>
  17. #include <butil/logging.h>
  18. #include <butil/time.h>
  19. #include <bthread/bthread.h>
  20. #include <brpc/channel.h>
  21. #include <brpc/server.h>
  22. #include "echo.pb.h"
  23. #include <bvar/bvar.h>
  24. DEFINE_int32(thread_num, 4, "Number of threads to send requests");
  25. DEFINE_bool(use_bthread, false, "Use bthread to send requests");
  26. DEFINE_string(attachment, "foo", "Carry this along with requests");
  27. DEFINE_string(connection_type, "", "Connection type. Available values: single, pooled, short");
  28. DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
  29. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  30. DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
  31. DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
  32. DEFINE_string(protocol, "baidu_std", "Protocol type. Defined in protocol/brpc/options.proto");
  33. DEFINE_int32(depth, 0, "number of loop calls");
  34. // Don't send too frequently in this example
  35. DEFINE_int32(sleep_ms, 100, "milliseconds to sleep after each RPC");
  36. DEFINE_int32(dummy_port, 0, "Launch dummy server at this port");
  37. bvar::LatencyRecorder g_latency_recorder("client");
  38. void* sender(void* arg) {
  39. brpc::Channel* chan = (brpc::Channel*)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(chan);
  43. // Send a request and wait for the response every 1 second.
  44. int log_id = 0;
  45. while (!brpc::IsAskedToQuit()) {
  46. // We will receive response synchronously, safe to put variables
  47. // on stack.
  48. example::EchoRequest request;
  49. example::EchoResponse response;
  50. brpc::Controller cntl;
  51. request.set_message("hello world");
  52. if (FLAGS_depth > 0) {
  53. request.set_depth(FLAGS_depth);
  54. }
  55. cntl.set_log_id(log_id ++); // set by user
  56. if (FLAGS_protocol != "http" && FLAGS_protocol != "h2c") {
  57. // Set attachment which is wired to network directly instead of
  58. // being serialized into protobuf messages.
  59. cntl.request_attachment().append(FLAGS_attachment);
  60. }
  61. // Because `done'(last parameter) is NULL, this function waits until
  62. // the response comes back or error occurs(including timedout).
  63. stub.Echo(&cntl, &request, &response, NULL);
  64. if (cntl.Failed()) {
  65. //LOG_EVERY_SECOND(WARNING) << "Fail to send EchoRequest, " << cntl.ErrorText();
  66. } else {
  67. g_latency_recorder << cntl.latency_us();
  68. }
  69. if (FLAGS_sleep_ms != 0) {
  70. bthread_usleep(FLAGS_sleep_ms * 1000L);
  71. }
  72. }
  73. return NULL;
  74. }
  75. int main(int argc, char* argv[]) {
  76. // Parse gflags. We recommend you to use gflags as well.
  77. google::SetUsageMessage("Send EchoRequest to server every second");
  78. google::ParseCommandLineFlags(&argc, &argv, true);
  79. // A Channel represents a communication line to a Server. Notice that
  80. // Channel is thread-safe and can be shared by all threads in your program.
  81. brpc::Channel channel;
  82. brpc::ChannelOptions options;
  83. options.protocol = FLAGS_protocol;
  84. options.connection_type = FLAGS_connection_type;
  85. options.timeout_ms = FLAGS_timeout_ms/*milliseconds*/;
  86. options.max_retry = FLAGS_max_retry;
  87. // Initialize the channel, NULL means using default options.
  88. // options, see `brpc/channel.h'.
  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. std::vector<bthread_t> tids;
  94. tids.resize(FLAGS_thread_num);
  95. if (!FLAGS_use_bthread) {
  96. for (int i = 0; i < FLAGS_thread_num; ++i) {
  97. if (pthread_create(&tids[i], NULL, sender, &channel) != 0) {
  98. LOG(ERROR) << "Fail to create pthread";
  99. return -1;
  100. }
  101. }
  102. } else {
  103. for (int i = 0; i < FLAGS_thread_num; ++i) {
  104. if (bthread_start_background(
  105. &tids[i], NULL, sender, &channel) != 0) {
  106. LOG(ERROR) << "Fail to create bthread";
  107. return -1;
  108. }
  109. }
  110. }
  111. if (FLAGS_dummy_port > 0) {
  112. brpc::StartDummyServerAt(FLAGS_dummy_port);
  113. }
  114. while (!brpc::IsAskedToQuit()) {
  115. sleep(1);
  116. LOG(INFO) << "Sending EchoRequest at qps=" << g_latency_recorder.qps(1)
  117. << " latency=" << g_latency_recorder.latency(1);
  118. }
  119. LOG(INFO) << "EchoClient is going to quit";
  120. for (int i = 0; i < FLAGS_thread_num; ++i) {
  121. if (!FLAGS_use_bthread) {
  122. pthread_join(tids[i], NULL);
  123. } else {
  124. bthread_join(tids[i], NULL);
  125. }
  126. }
  127. return 0;
  128. }