main.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include <iostream>
  17. #include "util/tc_http.h"
  18. #include "util/tc_option.h"
  19. #include "util/tc_common.h"
  20. #include "util/tc_clientsocket.h"
  21. #include "util/tc_thread_pool.h"
  22. #include "util/tc_timeprovider.h"
  23. #include "servant/Application.h"
  24. using namespace std;
  25. using namespace tars;
  26. Communicator* _comm;
  27. static string http2Obj = "TestApp.Http2Server.http2Obj@tcp -h 127.0.0.1 -p 8082";
  28. struct Param
  29. {
  30. int count;
  31. string call;
  32. int thread;
  33. ServantPrx servant2Prx;
  34. };
  35. Param param;
  36. std::atomic<int> callback_count(0);
  37. void syncRpc2(int c)
  38. {
  39. int64_t t = TC_Common::now2us();
  40. std::map<std::string, std::string> header;
  41. header[":authority"] = "domain.com";
  42. header[":scheme"] = "http";
  43. std::map<std::string, std::string> rheader;
  44. //发起远程调用
  45. for (int i = 0; i < c; ++i)
  46. {
  47. string rbody;
  48. try
  49. {
  50. param.servant2Prx->http_call("GET", "/", header, "helloworld", rheader, rbody);
  51. }
  52. catch(exception& e)
  53. {
  54. cout << "exception:" << e.what() << endl;
  55. }
  56. ++callback_count;
  57. }
  58. int64_t cost = TC_Common::now2us() - t;
  59. cout << "syncRpc2 total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
  60. }
  61. void asyncRpc2(int c)
  62. {
  63. int64_t t = TC_Common::now2us();
  64. std::map<std::string, std::string> header;
  65. header[":path"] = "/";
  66. header[":method"] = "GET";
  67. header[":authority"] = "domain.com";
  68. header[":scheme"] = "http";
  69. //发起远程调用
  70. for (int i = 0; i < c; ++i)
  71. {
  72. HttpCallbackPtr p = new TestHttpCallback(t, i, c);
  73. try
  74. {
  75. param.servant2Prx->http_call_async(header, "helloworld", p);
  76. }
  77. catch(exception& e)
  78. {
  79. cout << "exception:" << e.what() << endl;
  80. }
  81. TC_Common::msleep(10);
  82. // while(i-callback_count > 0 )
  83. // {
  84. // TC_Common::msleep(100);
  85. // }
  86. }
  87. int64_t cost = TC_Common::now2us() - t;
  88. cout << "asyncRpc2 send:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. try
  93. {
  94. if (argc < 4)
  95. {
  96. cout << "Usage:" << argv[0] << "--count=1000 --call=[synchttp2|asynchttp2] --thread=1" << endl;
  97. return 0;
  98. }
  99. TC_Option option;
  100. option.decode(argc, argv);
  101. param.count = TC_Common::strto<int>(option.getValue("count"));
  102. if(param.count <= 0) param.count = 1000;
  103. param.call = option.getValue("call");
  104. if(param.call.empty()) param.call = "sync";
  105. param.thread = TC_Common::strto<int>(option.getValue("thread"));
  106. if(param.thread <= 0) param.thread = 1;
  107. _comm = new Communicator();
  108. // TarsRollLogger::getInstance()->logger()->setLogLevel(6);
  109. _comm->setProperty("sendqueuelimit", "1000000");
  110. _comm->setProperty("asyncqueuecap", "1000000");
  111. param.servant2Prx = _comm->stringToProxy<ServantPrx>(http2Obj);
  112. param.servant2Prx->tars_connect_timeout(5000);
  113. param.servant2Prx->tars_async_timeout(60*1000);
  114. ProxyProtocol proto;
  115. proto.requestFunc = ProxyProtocol::http2Request;
  116. proto.responseFunc = ProxyProtocol::http2Response;
  117. param.servant2Prx->tars_set_protocol(proto);
  118. int64_t start = TC_Common::now2us();
  119. std::function<void(int)> func;
  120. if (param.call == "synchttp2")
  121. {
  122. func = syncRpc2;
  123. }
  124. else if(param.call == "asynchttp2")
  125. {
  126. func = asyncRpc2;
  127. }
  128. else
  129. {
  130. cout << "no func, exits" << endl;
  131. exit(0);
  132. }
  133. vector<std::thread*> vt;
  134. for(int i = 0 ; i< param.thread; i++)
  135. {
  136. vt.push_back(new std::thread(func, param.count));
  137. }
  138. std::thread print([&]{while(callback_count != param.count * param.thread) {
  139. cout << param.call << ": ----------finish count:" << callback_count << endl;
  140. std::this_thread::sleep_for(std::chrono::seconds(1));
  141. };});
  142. for(size_t i = 0 ; i< vt.size(); i++)
  143. {
  144. vt[i]->join();
  145. delete vt[i];
  146. }
  147. cout << "(pid:" << std::this_thread::get_id() << ")"
  148. << "(count:" << param.count << ")"
  149. << "(use ms:" << (TC_Common::now2us() - start)/1000 << ")"
  150. << endl;
  151. while(callback_count != param.count * param.thread) {
  152. std::this_thread::sleep_for(std::chrono::seconds(1));
  153. }
  154. print.join();
  155. cout << "----------finish count:" << callback_count << endl;
  156. }
  157. catch(exception &ex)
  158. {
  159. cout << ex.what() << endl;
  160. }
  161. cout << "main return." << endl;
  162. return 0;
  163. }