main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 "BServant.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. BServantPrx _prx;
  32. };
  33. Test1::Test1(const string &sStr)
  34. {
  35. _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 10.208.139.242 -p 17890 -t 10000");
  36. _comm.setProperty("stat", "tars.tarsstat.StatObj");
  37. _comm.stringToProxy(sStr, _prx);
  38. }
  39. Test1::~Test1()
  40. {
  41. }
  42. void Test1::queryResult(int iFlag, int iExecuteNum)
  43. {
  44. string sIn(10,'a');
  45. string sOut("");
  46. tars::Int32 count = 0;
  47. unsigned long sum = 0;
  48. time_t _iTime=TC_TimeProvider::getInstance()->getNowMs();
  49. for(int i=0; i<iExecuteNum; i++)
  50. {
  51. sOut = "";
  52. try
  53. {
  54. int ret = -1;
  55. if(iFlag == 0)
  56. {
  57. ret = _prx->testCoroSerial(sIn, sOut);
  58. }
  59. else
  60. {
  61. ret = _prx->testCoroParallel(sIn, sOut);
  62. }
  63. if(ret == 0)
  64. {
  65. ++sum;
  66. ++count;
  67. if(count == iExecuteNum)
  68. {
  69. cout << "pthread id: " << pthread_self() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl;
  70. _iTime=TC_TimeProvider::getInstance()->getNowMs();
  71. count = 0;
  72. }
  73. }
  74. }
  75. catch(TC_Exception &e)
  76. {
  77. cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl;
  78. }
  79. catch(...)
  80. {
  81. cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl;
  82. }
  83. }
  84. cout << "succ:" << sum << endl;
  85. cout << "sOut:" << sOut << endl;
  86. }
  87. int main(int argc,char ** argv)
  88. {
  89. if(argc != 5)
  90. {
  91. cout << "usage: " << argv[0] << " sObj ThreadNum CallTimes CallMode" << endl;
  92. return -1;
  93. }
  94. string s = string(argv[1]);
  95. Test1 test1(s);
  96. try
  97. {
  98. tars::Int32 threads = TC_Common::strto<tars::Int32>(string(argv[2]));
  99. TC_ThreadPool tp;
  100. tp.init(threads);
  101. tp.start();
  102. tars::Int32 times = TC_Common::strto<tars::Int32>(string(argv[3]));
  103. tars::Int32 callMode = TC_Common::strto<tars::Int32>(string(argv[4]));
  104. for(int i = 0; i<threads; i++)
  105. {
  106. auto fw = std::bind(&Test1::queryResult, &test1, callMode, times);
  107. tp.exec(fw);
  108. cout << "********************" <<endl;
  109. }
  110. tp.wait();
  111. }
  112. catch(exception &e)
  113. {
  114. cout<<e.what()<<endl;
  115. }
  116. catch(...)
  117. {
  118. }
  119. return 0;
  120. }