main.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. //发起远程调用
  41. for (int i = 0; i < c; ++i)
  42. {
  43. shared_ptr<TC_HttpResponse> rsp;
  44. shared_ptr<TC_HttpRequest> req = std::make_shared<TC_HttpRequest>();
  45. req->setPostRequest("http://domain.com/hello", string("helloworld-") + TC_Common::tostr(i), true);
  46. try
  47. {
  48. param.servant2Prx->http_call("hello", req, rsp);
  49. }
  50. catch (exception & e)
  51. {
  52. cout << "exception:" << e.what() << endl;
  53. }
  54. assert(req->getContent() == rsp->getContent());
  55. assert(req.use_count() == 1);
  56. assert(rsp.use_count() == 1);
  57. ++callback_count;
  58. }
  59. int64_t cost = TC_Common::now2us() - t;
  60. cout << "syncRpc2 total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
  61. }
  62. struct TestHttpCallback : public HttpCallback
  63. {
  64. TestHttpCallback(const string &buff) : _buff(buff)
  65. {
  66. }
  67. virtual int onHttpResponse(const shared_ptr<TC_HttpResponse> &rsp)
  68. {
  69. callback_count++;
  70. assert(_buff == rsp->getContent());
  71. return 0;
  72. }
  73. virtual int onHttpResponseException(int expCode)
  74. {
  75. cout << "onHttpResponseException expCode:" << expCode << endl;
  76. callback_count++;
  77. return 0;
  78. }
  79. string _buff;
  80. };
  81. void asyncRpc2(int c)
  82. {
  83. int64_t t = TC_Common::now2us();
  84. //发起远程调用
  85. for (int i = 0; i < c; ++i)
  86. {
  87. shared_ptr<TC_HttpRequest> req = std::make_shared<TC_HttpRequest>();
  88. string buff = string("helloworld-") + TC_Common::tostr(i);
  89. req->setPostRequest("http://domain.com/hello", buff, true);
  90. HttpCallbackPtr p = new TestHttpCallback(buff);
  91. try
  92. {
  93. param.servant2Prx->http_call_async("hello", req, p);
  94. }
  95. catch(exception& e)
  96. {
  97. cout << "exception:" << e.what() << endl;
  98. }
  99. if(i % 500 == 0)
  100. {
  101. TC_Common::msleep(100);
  102. }
  103. }
  104. int64_t cost = TC_Common::now2us() - t;
  105. cout << "asyncRpc2 send:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
  106. }
  107. int main(int argc, char *argv[])
  108. {
  109. try
  110. {
  111. if (argc < 4)
  112. {
  113. cout << "Usage:" << argv[0] << "--count=1000 --call=[sync|async] --thread=1" << endl;
  114. return 0;
  115. }
  116. TC_Option option;
  117. option.decode(argc, argv);
  118. param.count = TC_Common::strto<int>(option.getValue("count"));
  119. if(param.count <= 0) param.count = 1000;
  120. param.call = option.getValue("call");
  121. if(param.call.empty()) param.call = "sync";
  122. param.thread = TC_Common::strto<int>(option.getValue("thread"));
  123. if(param.thread <= 0) param.thread = 1;
  124. _comm = new Communicator();
  125. // TarsRollLogger::getInstance()->logger()->setLogLevel(6);
  126. _comm->setProperty("sendqueuelimit", "1000000");
  127. _comm->setProperty("asyncqueuecap", "1000000");
  128. param.servant2Prx = _comm->stringToProxy<ServantPrx>(http2Obj);
  129. param.servant2Prx->tars_connect_timeout(5000);
  130. param.servant2Prx->tars_async_timeout(60*1000);
  131. param.servant2Prx->tars_set_protocol(ServantProxy::PROTOCOL_HTTP2);
  132. int64_t start = TC_Common::now2us();
  133. std::function<void(int)> func;
  134. if (param.call == "sync")
  135. {
  136. func = syncRpc2;
  137. }
  138. else if(param.call == "async")
  139. {
  140. func = asyncRpc2;
  141. }
  142. else
  143. {
  144. cout << "no func, exits" << endl;
  145. exit(0);
  146. }
  147. vector<std::thread*> vt;
  148. for(int i = 0 ; i< param.thread; i++)
  149. {
  150. vt.push_back(new std::thread(func, param.count));
  151. }
  152. std::thread print([&]{while(callback_count != param.count * param.thread) {
  153. cout << "Http2:" << param.call << ": ----------finish count:" << callback_count << endl;
  154. std::this_thread::sleep_for(std::chrono::seconds(1));
  155. };});
  156. for(size_t i = 0 ; i< vt.size(); i++)
  157. {
  158. vt[i]->join();
  159. delete vt[i];
  160. }
  161. cout << "(pid:" << std::this_thread::get_id() << ")"
  162. << "(count:" << param.count << ")"
  163. << "(use ms:" << (TC_Common::now2us() - start)/1000 << ")"
  164. << endl;
  165. while(callback_count != param.count * param.thread) {
  166. std::this_thread::sleep_for(std::chrono::seconds(1));
  167. }
  168. print.join();
  169. cout << "----------finish count:" << callback_count << endl;
  170. }
  171. catch(exception &ex)
  172. {
  173. cout << ex.what() << endl;
  174. }
  175. cout << "main return." << endl;
  176. return 0;
  177. }