main.cpp 5.6 KB

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