main.cpp 6.8 KB

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