main.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "servant/CoroutineScheduler.h"
  19. #include <iostream>
  20. using namespace std;
  21. using namespace Test;
  22. using namespace tars;
  23. //继承框架的协程类
  24. class TestCoroutine : public Coroutine
  25. {
  26. public:
  27. TestCoroutine(int iNum, const string &sObj);
  28. ~TestCoroutine() {}
  29. void handle();
  30. private:
  31. int _num;
  32. string _sObj;
  33. Communicator _comm;
  34. BServantPrx _prx;
  35. };
  36. TestCoroutine::TestCoroutine(int iNum, const string &sObj)
  37. : _num(iNum)
  38. , _sObj(sObj)
  39. {
  40. _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 10.208.139.242 -p 17890 -t 10000");
  41. _comm.stringToProxy(_sObj, _prx);
  42. }
  43. void TestCoroutine::handle()
  44. {
  45. string sIn(32,'a');
  46. string sOut("");
  47. unsigned long sum = 0;
  48. for(int i = 0; i < _num; i++)
  49. {
  50. try
  51. {
  52. int iRet = _prx->testCoroSerial(sIn, sOut);
  53. if(iRet == 0)
  54. {
  55. ++sum;
  56. }
  57. sOut = "";
  58. iRet = _prx->testCoroParallel(sIn, sOut);
  59. if(iRet == 0)
  60. {
  61. ++sum;
  62. }
  63. }
  64. catch(TC_Exception &e)
  65. {
  66. cout << "i: " << i << "exception: " << e.what() << endl;
  67. }
  68. catch(...)
  69. {
  70. cout << "i: " << i << "unknown exception." << endl;
  71. }
  72. }
  73. cout << "succ:" << sum <<endl;
  74. }
  75. int main(int argc,char ** argv)
  76. {
  77. if(argc != 3)
  78. {
  79. cout << "usage: " << argv[0] << " CallTimes sObj" << endl;
  80. return -1;
  81. }
  82. tars::Int32 iNum = TC_Common::strto<tars::Int32>(string(argv[1]));
  83. string sObj = string(argv[2]);
  84. TestCoroutine testCoro(iNum, sObj);
  85. testCoro.setCoroInfo(10, 128, 128*1024);
  86. testCoro.start();
  87. testCoro.getThreadControl().join();
  88. return 0;
  89. }