main.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "servant/Communicator.h"
  18. #include "Hello.h"
  19. #include "util/tc_option.h"
  20. using namespace std;
  21. using namespace tars;
  22. using namespace TestApp;
  23. Communicator* _comm;
  24. static string helloObj = "TestApp.UdpServer.UdpObj@udp -h 127.0.0.1 -p 9016 -e 1";
  25. static string ipv6Obj = "TestApp.UdpServer.Ipv6Obj@udp -h ::1 -p 25460";
  26. struct Param
  27. {
  28. int count;
  29. string call;
  30. int thread;
  31. int buffersize;
  32. int netthread;
  33. HelloPrx pPrx;
  34. };
  35. Param param;
  36. std::atomic<int> request_count(0);
  37. std::atomic<int> callback_count(0);
  38. struct HelloCallback : public HelloPrxCallback
  39. {
  40. HelloCallback(int64_t t, int i, int c) : start(t), cur(i), count(c)
  41. {
  42. }
  43. //call back
  44. virtual void callback_testHello(int ret, const string &r)
  45. {
  46. assert(ret == 0);
  47. callback_count++;
  48. if(cur == count-1)
  49. {
  50. int64_t cost = TC_Common::now2us() - start;
  51. cout << "callback_testHello count:" << count << ", " << cost << " us, avg:" << 1.*cost/count << "us" << endl;
  52. }
  53. }
  54. virtual void callback_testHello_exception(tars::Int32 ret)
  55. {
  56. cout << "callback exception:" << ret << endl;
  57. }
  58. int64_t start;
  59. int cur;
  60. int count;
  61. };
  62. void syncCall(int c)
  63. {
  64. string buffer(param.buffersize, 'a');
  65. int64_t t = TC_Common::now2us();
  66. //发起远程调用
  67. for (int i = 0; i < c; ++i)
  68. {
  69. string r;
  70. try
  71. {
  72. param.pPrx->testHello(buffer, r);
  73. }
  74. catch(exception& e)
  75. {
  76. cout << "exception:" << e.what() << endl;
  77. }
  78. ++callback_count;
  79. }
  80. int64_t cost = TC_Common::now2us() - t;
  81. cout << "syncCall total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
  82. }
  83. void asyncCall(int c)
  84. {
  85. int64_t t = TC_Common::now2us();
  86. string buffer(param.buffersize, 'a');
  87. //发起远程调用
  88. for (int i = 0; i < c;)
  89. {
  90. if(request_count - callback_count < 100) {
  91. i++;
  92. request_count++;
  93. HelloPrxCallbackPtr p = new HelloCallback(t, i, c);
  94. try {
  95. param.pPrx->async_testHello(p, buffer);
  96. }
  97. catch (exception & e) {
  98. cout << "exception:" << e.what() << endl;
  99. }
  100. }
  101. else
  102. {
  103. TC_Common::msleep(10);
  104. }
  105. }
  106. int64_t cost = TC_Common::now2us() - t;
  107. cout << "asyncCall send:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
  108. }
  109. int main(int argc, char *argv[])
  110. {
  111. try
  112. {
  113. if (argc < 6)
  114. {
  115. cout << "Usage:" << argv[0] << "--config=conf --count=1000 --call=[sync|async|syncv6|asyncv6] --thread=1 --buffersize=1000 --netthread=1" << endl;
  116. return 0;
  117. }
  118. TC_Option option;
  119. option.decode(argc, argv);
  120. param.count = TC_Common::strto<int>(option.getValue("count"));
  121. if(param.count <= 0) param.count = 1000;
  122. param.buffersize = TC_Common::strto<int>(option.getValue("buffersize"));
  123. if(param.buffersize <= 0) param.buffersize = 1000;
  124. param.call = option.getValue("call");
  125. if(param.call.empty()) param.call = "sync";
  126. param.thread = TC_Common::strto<int>(option.getValue("thread"));
  127. if(param.thread <= 0) param.thread = 1;
  128. param.netthread = TC_Common::strto<int>(option.getValue("netthread"));
  129. if(param.netthread <= 0) param.netthread = 1;
  130. _comm = new Communicator();
  131. TC_Config conf;
  132. conf.parseFile(option.getValue("config"));
  133. _comm->setProperty(conf);
  134. // TarsRollLogger::getInstance()->logger()->setLogLevel(6);
  135. _comm->setProperty("sendqueuelimit", "1000000");
  136. _comm->setProperty("asyncqueuecap", "1000000");
  137. _comm->setProperty("netthread", TC_Common::tostr(param.netthread));
  138. int64_t start = TC_Common::now2us();
  139. std::function<void(int)> func;
  140. if (param.call == "sync")
  141. {
  142. func = syncCall;
  143. param.pPrx = _comm->stringToProxy<HelloPrx>(helloObj);
  144. }
  145. else if (param.call == "async")
  146. {
  147. func = asyncCall;
  148. param.pPrx = _comm->stringToProxy<HelloPrx>(helloObj);
  149. }
  150. else if (param.call == "syncv6")
  151. {
  152. func = syncCall;
  153. param.pPrx = _comm->stringToProxy<HelloPrx>(ipv6Obj);
  154. }
  155. else if (param.call == "asyncv6")
  156. {
  157. func = asyncCall;
  158. param.pPrx = _comm->stringToProxy<HelloPrx>(ipv6Obj);
  159. }
  160. else
  161. {
  162. cout << "no func, exits" << endl;
  163. exit(0);
  164. }
  165. param.pPrx->tars_connect_timeout(5000);
  166. param.pPrx->tars_async_timeout(60*1000);
  167. param.pPrx->tars_ping();
  168. vector<std::thread*> vt;
  169. for(int i = 0 ; i< param.thread; i++)
  170. {
  171. vt.push_back(new std::thread(func, param.count));
  172. }
  173. std::thread print([&]{while(callback_count != param.count * param.thread) {
  174. cout << "Auth:" << param.call << " : ----------finish count:" << callback_count << endl;
  175. std::this_thread::sleep_for(std::chrono::seconds(1));
  176. };});
  177. for(size_t i = 0 ; i< vt.size(); i++)
  178. {
  179. vt[i]->join();
  180. delete vt[i];
  181. }
  182. cout << "(pid:" << std::this_thread::get_id() << ")"
  183. << "(count:" << param.count << ")"
  184. << "(use ms:" << (TC_Common::now2us() - start)/1000 << ")"
  185. << endl;
  186. while(callback_count != param.count * param.thread) {
  187. std::this_thread::sleep_for(std::chrono::seconds(1));
  188. }
  189. print.join();
  190. cout << "Auth:" << param.call << " ----------finish count:" << callback_count << endl;
  191. }
  192. catch(exception &ex)
  193. {
  194. cout << ex.what() << endl;
  195. }
  196. cout << "main return." << endl;
  197. return 0;
  198. }