main.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <iostream>
  2. #include "servant/Application.h"
  3. #include "util/tc_option.h"
  4. using namespace tars;
  5. Communicator *_comm;
  6. string matchObj = "TestApp.HelloServer.HelloObj@tcp -h 127.0.0.1 -p 8999";
  7. struct Param
  8. {
  9. int count;
  10. int thread;
  11. ServantPrx pPrx;
  12. };
  13. Param param;
  14. std::atomic<int> callback(0);
  15. void ping(int c)
  16. {
  17. for(int i = 0 ; i < c; i++)
  18. {
  19. param.pPrx->tars_ping();
  20. ++callback;
  21. }
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. try
  26. {
  27. if (argc < 2)
  28. {
  29. cout << "Usage:" << argv[0] << " --count=1000 --thread=1" << endl;
  30. return 0;
  31. }
  32. TC_Option option;
  33. option.decode(argc, argv);
  34. param.count = TC_Common::strto<int>(option.getValue("count"));
  35. if(param.count <= 0) param.count = 1000;
  36. param.thread = TC_Common::strto<int>(option.getValue("thread"));
  37. if(param.thread <= 0) param.thread = 1;
  38. _comm = new Communicator();
  39. // LocalRollLogger::getInstance()->logger()->setLogLevel(6);
  40. _comm->setProperty("sendqueuelimit", "1000000");
  41. _comm->setProperty("asyncqueuecap", "1000000");
  42. param.pPrx = _comm->stringToProxy<ServantPrx>(matchObj);
  43. param.pPrx->tars_connect_timeout(50000);
  44. param.pPrx->tars_set_timeout(60 * 1000);
  45. param.pPrx->tars_async_timeout(60*1000);
  46. int64_t start = TC_Common::now2us();
  47. std::function<void(int)> func;
  48. func = ping;
  49. vector<std::thread*> vt;
  50. for(int i = 0 ; i< param.thread; i++)
  51. {
  52. vt.push_back(new std::thread(func, param.count));
  53. }
  54. std::thread print([&]{while(callback != param.count * param.thread) {
  55. cout << "Call:" << " : ----------finish count:" << callback << endl;
  56. std::this_thread::sleep_for(std::chrono::seconds(1));
  57. };});
  58. for(size_t i = 0 ; i< vt.size(); i++)
  59. {
  60. vt[i]->join();
  61. delete vt[i];
  62. }
  63. cout << "(pid:" << std::this_thread::get_id() << ")"
  64. << "(count:" << param.count << ")"
  65. << "(use ms:" << (TC_Common::now2us() - start)/1000 << ")"
  66. << endl;
  67. while(callback != param.count * param.thread) {
  68. std::this_thread::sleep_for(std::chrono::seconds(1));
  69. }
  70. print.join();
  71. cout << " ----------finish count:" << callback << endl;
  72. }
  73. catch(exception &ex)
  74. {
  75. cout << ex.what() << endl;
  76. }
  77. cout << "main return." << endl;
  78. return 0;
  79. }