proxy.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include "benchmark_pb.srpc.h"
  4. #include "benchmark_thrift.srpc.h"
  5. #include "workflow/WFFacilities.h"
  6. using namespace srpc;
  7. std::atomic<int> query_count(0); // per_second
  8. std::atomic<long long> last_timestamp(0L);
  9. //volatile bool stop_flag = false;
  10. int max_qps = 0;
  11. long long total_count = 0;
  12. std::string remote_host;
  13. unsigned short remote_port;
  14. WFFacilities::WaitGroup wait_group(1);
  15. inline void collect_qps()
  16. {
  17. int64_t ms_timestamp = GET_CURRENT_MS();
  18. ++query_count;
  19. if (ms_timestamp / 1000 > last_timestamp)
  20. {
  21. last_timestamp = ms_timestamp / 1000;
  22. int count = query_count;
  23. query_count = 0;
  24. total_count += count;
  25. if (count > max_qps)
  26. max_qps = count;
  27. long long ts = ms_timestamp;
  28. fprintf(stdout, "TIMESTAMP(ms) = %llu QPS = %d\n", ts, count);
  29. }
  30. }
  31. template<class CLIENT>
  32. class BenchmarkPBServiceImpl : public BenchmarkPB::Service
  33. {
  34. public:
  35. void echo_pb(FixLengthPBMsg *request, EmptyPBMsg *response,
  36. RPCContext *ctx) override
  37. {
  38. auto *task = this->client->create_echo_pb_task(
  39. [](EmptyPBMsg *remote_resp, srpc::RPCContext *remote_ctx) {
  40. collect_qps();
  41. });
  42. task->user_data = response;
  43. task->serialize_input(request);
  44. ctx->get_series()->push_back(task);
  45. }
  46. void slow_pb(FixLengthPBMsg *request, EmptyPBMsg *response,
  47. RPCContext *ctx) override
  48. {
  49. auto *task = WFTaskFactory::create_timer_task(15000, nullptr);
  50. ctx->get_series()->push_back(task);
  51. }
  52. CLIENT *client;
  53. };
  54. template<class CLIENT>
  55. class BenchmarkThriftServiceImpl : public BenchmarkThrift::Service
  56. {
  57. public:
  58. void echo_thrift(BenchmarkThrift::echo_thriftRequest *request,
  59. BenchmarkThrift::echo_thriftResponse *response,
  60. RPCContext *ctx) override
  61. {
  62. auto *task = this->client->create_echo_thrift_task(
  63. [](BenchmarkThrift::echo_thriftResponse *remote_resp,
  64. srpc::RPCContext *remote_ctx) {
  65. collect_qps();
  66. });
  67. task->user_data = response;
  68. task->serialize_input(request);
  69. ctx->get_series()->push_back(task);
  70. }
  71. void slow_thrift(BenchmarkThrift::slow_thriftRequest *request,
  72. BenchmarkThrift::slow_thriftResponse *response,
  73. RPCContext *ctx) override
  74. {
  75. auto *task = WFTaskFactory::create_timer_task(15000, nullptr);
  76. ctx->get_series()->push_back(task);
  77. }
  78. CLIENT *client;
  79. };
  80. static void sig_handler(int signo)
  81. {
  82. wait_group.done();
  83. }
  84. template<template<class> class SERVICE, class CLIENT>
  85. static void init_proxy_client(SERVICE<CLIENT>& service_impl)
  86. {
  87. RPCClientParams client_params = RPC_CLIENT_PARAMS_DEFAULT;
  88. client_params.task_params.keep_alive_timeout = -1;
  89. client_params.host = remote_host;
  90. client_params.port = remote_port;
  91. service_impl.client = new CLIENT(&client_params);
  92. }
  93. static void run_srpc_proxy(unsigned short port)
  94. {
  95. RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
  96. params.max_connections = 2048;
  97. SRPCServer proxy_server(&params);
  98. BenchmarkPBServiceImpl<BenchmarkPB::SRPCClient> pb_impl;
  99. BenchmarkThriftServiceImpl<BenchmarkThrift::SRPCClient> thrift_impl;
  100. init_proxy_client<BenchmarkPBServiceImpl,
  101. BenchmarkPB::SRPCClient>(pb_impl);
  102. init_proxy_client<BenchmarkThriftServiceImpl,
  103. BenchmarkThrift::SRPCClient>(thrift_impl);
  104. proxy_server.add_service(&pb_impl);
  105. proxy_server.add_service(&thrift_impl);
  106. if (proxy_server.start(port) == 0)
  107. {
  108. wait_group.wait();
  109. proxy_server.stop();
  110. }
  111. else
  112. perror("server start");
  113. }
  114. template<class SERVER, class CLIENT>
  115. static void run_pb_proxy(unsigned short port)
  116. {
  117. RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
  118. params.max_connections = 2048;
  119. SERVER server(&params);
  120. BenchmarkPBServiceImpl<CLIENT> pb_impl;
  121. init_proxy_client<BenchmarkPBServiceImpl, CLIENT>(pb_impl);
  122. server.add_service(&pb_impl);
  123. if (server.start(port) == 0)
  124. {
  125. wait_group.wait();
  126. server.stop();
  127. }
  128. else
  129. perror("server start");
  130. }
  131. template<class SERVER, class CLIENT>
  132. static void run_thrift_proxy(unsigned short port)
  133. {
  134. RPCServerParams params = RPC_SERVER_PARAMS_DEFAULT;
  135. params.max_connections = 2048;
  136. SERVER server(&params);
  137. BenchmarkThriftServiceImpl<CLIENT> thrift_impl;
  138. init_proxy_client<BenchmarkThriftServiceImpl, CLIENT>(thrift_impl);
  139. server.add_service(&thrift_impl);
  140. if (server.start(port) == 0)
  141. {
  142. wait_group.wait();
  143. server.stop();
  144. }
  145. else
  146. perror("server start");
  147. }
  148. int main(int argc, char* argv[])
  149. {
  150. GOOGLE_PROTOBUF_VERIFY_VERSION;
  151. if (argc != 5)
  152. {
  153. fprintf(stderr, "Usage: %s <PORT> <REMOTE_IP> <REMOTE_PORT>"
  154. " <srpc|brpc|thrift>\n", argv[0]);
  155. abort();
  156. }
  157. signal(SIGINT, sig_handler);
  158. signal(SIGTERM, sig_handler);
  159. WFGlobalSettings my = GLOBAL_SETTINGS_DEFAULT;
  160. my.poller_threads = 16;
  161. my.handler_threads = 16;
  162. WORKFLOW_library_init(&my);
  163. unsigned short port = atoi(argv[1]);
  164. remote_host = argv[2];
  165. remote_port = atoi(argv[3]);
  166. std::string server_type = argv[4];
  167. if (server_type == "srpc")
  168. run_srpc_proxy(port);
  169. else if (server_type == "brpc")
  170. run_pb_proxy<BRPCServer, BenchmarkPB::BRPCClient>(port);
  171. else if (server_type == "thrift")
  172. run_thrift_proxy<ThriftServer, BenchmarkThrift::ThriftClient>(port);
  173. else if (server_type == "srpc_http")
  174. run_pb_proxy<SRPCHttpServer, BenchmarkPB::SRPCHttpClient>(port);
  175. else if (server_type == "thrift_http")
  176. run_thrift_proxy<ThriftHttpServer, BenchmarkThrift::ThriftHttpClient>(port);
  177. else
  178. abort();
  179. fprintf(stdout, "\nTotal query: %llu max QPS: %d\n", total_count, max_qps);
  180. google::protobuf::ShutdownProtobufLibrary();
  181. return 0;
  182. }