test_stat.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 
  2. #include "hello_test.h"
  3. #include "server/FrameworkServer.h"
  4. int getStatCount(const vector<map<tars::StatMicMsgHead, tars::StatMicMsgBody>> &data)
  5. {
  6. LOG_CONSOLE_DEBUG << "client stat:" << data.size() << endl;
  7. int sum = 0;
  8. for_each(data.begin(), data.end(), [&](const map<tars::StatMicMsgHead, tars::StatMicMsgBody> &r){
  9. for(auto e : r)
  10. {
  11. // LOG_CONSOLE_DEBUG << e.first.writeToJsonString() << ", " << e.second.writeToJsonString() << endl;
  12. sum += e.second.count ;
  13. }
  14. });
  15. return sum;
  16. }
  17. TEST_F(HelloTest, statReport)
  18. {
  19. FrameworkServer fs;
  20. startServer(fs, FRAMEWORK_CONFIG());
  21. HelloServer hs;
  22. startServer(hs, CONFIG());
  23. _clientStatData.clear();
  24. shared_ptr<Communicator> c = getCommunicator();
  25. int totalReport = 0;
  26. int totalRealReport = 0;
  27. int count = 3;
  28. while(count-->0)
  29. {
  30. int report = rand() % 100;
  31. if(report == 0)
  32. {
  33. continue;
  34. }
  35. checkStat(c.get(), report);
  36. TC_Common::sleep(1);
  37. totalReport += report;
  38. totalRealReport = getStatCount(_clientStatData);
  39. LOG_CONSOLE_DEBUG << "report:" << report << ", totalReport:" << totalReport << ", totalRealReport:" << totalRealReport << ", " << _clientStatData.size()<< endl;
  40. ASSERT_TRUE(totalReport >= totalRealReport);
  41. ASSERT_TRUE(totalReport - totalRealReport <= 20);
  42. }
  43. // LOG_CONSOLE_DEBUG << "client stat:" << _clientStatData.size() << endl;
  44. // LOG_CONSOLE_DEBUG << "server stat:" << _serverStatData.size() << endl;
  45. stopServer(hs);
  46. stopServer(fs);
  47. }
  48. TEST_F(HelloTest, statReportInCoroutine)
  49. {
  50. FrameworkServer fs;
  51. startServer(fs, FRAMEWORK_CONFIG());
  52. HelloServer hs;
  53. startServer(hs, CONFIG());
  54. _clientStatData.clear();
  55. shared_ptr<Communicator> c = getCommunicator();
  56. funcInCoroutine([=]()
  57. {
  58. int totalReport = 0;
  59. int totalRealReport = 0;
  60. int count = 3;
  61. while (count-- > 0)
  62. {
  63. int report = rand() % 100;
  64. if (report == 0)
  65. {
  66. continue;
  67. }
  68. checkStat(c.get(), report);
  69. TC_Common::sleep(1);
  70. totalReport += report;
  71. totalRealReport = getStatCount(_clientStatData);
  72. LOG_CONSOLE_DEBUG << "report:" << report << ", totalReport:" << totalReport << ", totalRealReport:"
  73. << totalRealReport << ", data size:" << _clientStatData.size() << endl;
  74. ASSERT_TRUE(totalReport >= totalRealReport);
  75. ASSERT_TRUE(totalReport - totalRealReport <= 20);
  76. }
  77. }, true);
  78. stopServer(hs);
  79. stopServer(fs);
  80. }