main.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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);
  28. ~TestCoroutine() {}
  29. void handle();
  30. private:
  31. int _num;
  32. Communicator _comm;
  33. BServantPrx _prx;
  34. };
  35. TestCoroutine::TestCoroutine(int iNum)
  36. : _num(iNum)
  37. {
  38. // _comm.setProperty("locator", "tars.tarsregistry.QueryObj@tcp -h 10.208.139.242 -p 17890 -t 10000");
  39. _prx = _comm.stringToProxy<BServantPrx>("TestApp.BServer.BServantObj@tcp -h 127.0.0.1 -p 9100");
  40. // _comm.stringToProxy(_sObj, _prx);
  41. }
  42. void TestCoroutine::handle()
  43. {
  44. string sIn(32,'a');
  45. string sOut("");
  46. unsigned long sum = 0;
  47. for(int i = 0; i < _num; i++)
  48. {
  49. try
  50. {
  51. int iRet = _prx->testCoroSerial(sIn, sOut);
  52. if(iRet == 0)
  53. {
  54. ++sum;
  55. }
  56. sOut = "";
  57. iRet = _prx->testCoroParallel(sIn, sOut);
  58. if(iRet == 0)
  59. {
  60. ++sum;
  61. }
  62. }
  63. catch(TC_Exception &e)
  64. {
  65. cout << "i: " << i << "exception: " << e.what() << endl;
  66. }
  67. catch(...)
  68. {
  69. cout << "i: " << i << "unknown exception." << endl;
  70. }
  71. }
  72. cout << "succ:" << sum <<endl;
  73. }
  74. int main(int argc,char ** argv)
  75. {
  76. if(argc != 2)
  77. {
  78. cout << "usage: " << argv[0] << " CallTimes " << endl;
  79. return -1;
  80. }
  81. tars::Int32 iNum = TC_Common::strto<tars::Int32>(string(argv[1]));
  82. TestCoroutine testCoro(iNum);
  83. //start 10 co
  84. testCoro.setCoroInfo(10, 128, 128*1024);
  85. testCoro.start();
  86. testCoro.getThreadControl().join();
  87. return 0;
  88. }