main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "Stress.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. ~Test1();
  29. void dohandle(int excut_num,int size);
  30. private:
  31. Communicator _comm;
  32. StressPrx prx;
  33. };
  34. Test1::Test1(const string &sStr)
  35. {
  36. // _comm.setProperty("locator","tars.tarsregistry.QueryObj@tcp -h 172.27.194.147 -p 17890 -t 50000");
  37. // _comm.setProperty("locator","tars.tarsregistry.QueryObj@tcp -h 10.120.129.226 -p 17890 -t 10000");
  38. _comm.setProperty("stat", "tars.tarsstat.StatObj");
  39. _comm.stringToProxy(sStr, prx);
  40. }
  41. Test1::~Test1()
  42. {
  43. }
  44. void Test1::dohandle(int excut_num,int size)
  45. {
  46. string s(size,'a');
  47. tars::Int32 count = 0;
  48. unsigned long sum = 0;
  49. time_t _iTime=TC_TimeProvider::getInstance()->getNowMs();
  50. for(int i=0; i<excut_num; i++)
  51. {
  52. try
  53. {
  54. string ret="";
  55. prx->tars_set_timeout(15000)->testStr(s,ret);
  56. if(ret.size() == s.size())
  57. {
  58. ++sum;
  59. ++count;
  60. }
  61. if(count == excut_num)
  62. {
  63. cout << "pthread id: " << std::this_thread::get_id() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl;
  64. count = 0;
  65. }
  66. }
  67. catch(TC_Exception &e)
  68. {
  69. cout << "pthread id: " << std::this_thread::get_id() << "id: " << i << "exception: " << e.what() << endl;
  70. }
  71. catch(...)
  72. {
  73. cout << "pthread id: " << std::this_thread::get_id() << "id: " << i << "unknown exception." << endl;
  74. }
  75. }
  76. cout << "succ:" << sum <<endl;
  77. }
  78. int main(int argc,char ** argv)
  79. {
  80. if(argc != 5)
  81. {
  82. cout << "usage: " << argv[0] << " ThreadNum CallTimes sObj size" << endl;
  83. return -1;
  84. }
  85. // string s="Test.TarsStressServer.StressObj";
  86. string s=string(argv[3]);
  87. Test1 tm(s);
  88. try
  89. {
  90. tars::Int32 threads = TC_Common::strto<tars::Int32>(string(argv[1]));
  91. TC_ThreadPool tp;
  92. tp.init(threads);
  93. tp.start();
  94. cout << "init tp succ" << endl;
  95. tars::Int32 times = TC_Common::strto<tars::Int32>(string(argv[2]));
  96. tars::Int32 size = TC_Common::strto<tars::Int32>(string(argv[4]));
  97. cout << "times:" << times << endl;
  98. for(int i = 0; i<threads; i++)
  99. {
  100. auto fw = std::bind(&Test1::dohandle, &tm, times, size);
  101. tp.exec(fw);
  102. cout << "********************" <<endl;
  103. }
  104. tp.waitForAllDone();
  105. }
  106. catch(exception &e)
  107. {
  108. cout<<e.what()<<endl;
  109. }
  110. catch(...)
  111. {
  112. }
  113. return 0;
  114. }