main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "AServant.h"
  17. #include "servant/Communicator.h"
  18. #include "util/tc_thread_pool.h"
  19. #include <iostream>
  20. using namespace std;
  21. using namespace Test;
  22. using namespace tars;
  23. class Test1
  24. {
  25. public:
  26. Test1(const string &sStr);
  27. ~Test1();
  28. void queryResult(int iFlag, int iExecuteNum);
  29. private:
  30. Communicator _comm;
  31. AServantPrx prx;
  32. };
  33. Test1::Test1(const string &sStr)
  34. {
  35. _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 172.22.32.115 -t 60000 -p 17890");
  36. _comm.stringToProxy(sStr, prx);
  37. }
  38. Test1::~Test1()
  39. {
  40. }
  41. void Test1::queryResult(int iFlag, int iExecuteNum)
  42. {
  43. string sIn(10,'a');
  44. string sOut("");
  45. tars::Int32 count = 0;
  46. unsigned long sum = 0;
  47. time_t _iTime=TC_TimeProvider::getInstance()->getNowMs();
  48. for(int i=0; i<iExecuteNum; i++)
  49. {
  50. sOut = "";
  51. try
  52. {
  53. int ret = -1;
  54. if(iFlag == 0)
  55. {
  56. ret = prx->queryResultSerial(sIn, sOut);
  57. }
  58. else
  59. {
  60. ret = prx->queryResultParallel(sIn, sOut);
  61. }
  62. if(ret == 0)
  63. {
  64. ++sum;
  65. ++count;
  66. if(count == iExecuteNum)
  67. {
  68. cout << "pthread id: " << pthread_self() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl;
  69. _iTime=TC_TimeProvider::getInstance()->getNowMs();
  70. count = 0;
  71. }
  72. }
  73. }
  74. catch(TC_Exception &e)
  75. {
  76. cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl;
  77. }
  78. catch(...)
  79. {
  80. cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl;
  81. }
  82. }
  83. cout << "succ:" << sum << endl;
  84. cout << "sOut:" << sOut << endl;
  85. }
  86. int main(int argc,char ** argv)
  87. {
  88. if(argc != 5)
  89. {
  90. cout << "usage: " << argv[0] << " sObj ThreadNum CallTimes CallMode" << endl;
  91. return -1;
  92. }
  93. string s = string(argv[1]);
  94. Test1 test1(s);
  95. try
  96. {
  97. tars::Int32 threads = TC_Common::strto<tars::Int32>(string(argv[2]));
  98. TC_ThreadPool tp;
  99. tp.init(threads);
  100. tp.start();
  101. tars::Int32 times = TC_Common::strto<tars::Int32>(string(argv[3]));
  102. tars::Int32 callMode = TC_Common::strto<tars::Int32>(string(argv[4]));
  103. for(int i = 0; i<threads; i++)
  104. {
  105. auto fw = std::bind(&Test1::queryResult, &test1, callMode, times);
  106. tp.exec(fw);
  107. cout << "********************" <<endl;
  108. }
  109. tp.waitForAllDone();
  110. }catch(exception &e)
  111. {
  112. cout<<e.what()<<endl;
  113. }
  114. catch(...)
  115. {
  116. }
  117. return 0;
  118. }