udp_test.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "stdafx.h"
  2. #include "udp_test.h"
  3. #include "udp_client_server.h"
  4. #include "udp_http.h"
  5. #include "cpu_affinity.h"
  6. #include <util/system/hp_timer.h>
  7. #include <util/datetime/cputimer.h>
  8. #include <util/random/random.h>
  9. #include <util/random/fast.h>
  10. namespace NNetliba {
  11. //static void PacketLevelTest(bool client)
  12. //{
  13. // int port = client ? 0 : 13013;
  14. // TIntrusivePtr<IUdpHost> host = CreateUdpHost(&port);
  15. //
  16. // if(host == 0) {
  17. // exit(-1);
  18. // }
  19. // TUdpAddress serverAddr = CreateAddress("localhost", 13013);
  20. // vector<char> dummyPacket;
  21. // dummyPacket.resize(10000);
  22. // srand(GetCycleCount());
  23. //
  24. // for (int i = 0; i < dummyPacket.size(); ++i)
  25. // dummyPacket[i] = rand();
  26. // bool cont = true, hasReply = true;
  27. // int reqCount = 1;
  28. // for (int i = 0; cont; ++i) {
  29. // host->Step();
  30. // if (client) {
  31. // //while (host->HasPendingData(serverAddr))
  32. // // Sleep(0);
  33. // if (hasReply) {
  34. // printf("request %d\n", reqCount);
  35. // *(int*)&dummyPacket[0] = reqCount;
  36. // host->Send(serverAddr, dummyPacket, 0, PP_NORMAL);
  37. // hasReply = false;
  38. // ++reqCount;
  39. // }else
  40. // sleep(0);
  41. //
  42. // TRequest *req;
  43. // while (req = host->GetRequest()) {
  44. // int n = *(int*)&req->Data[0];
  45. // printf("received response %d\n", n);
  46. // Y_ASSERT(memcmp(&req->Data[4], &dummyPacket[4], dummyPacket.size() - 4) == 0);
  47. // delete req;
  48. // hasReply = true;
  49. // }
  50. // TSendResult sr;
  51. // while (host->GetSendResult(&sr)) {
  52. // if (!sr.Success) {
  53. // printf("Send failed!\n");
  54. // //Sleep(INFINITE);
  55. // hasReply = true;
  56. // }
  57. // }
  58. // } else {
  59. // while (TRequest *req = host->GetRequest()) {
  60. // int n = *(int*)&req->Data[0];
  61. // printf("responding %d\n", n);
  62. // host->Send(req->Address, req->Data, 0, PP_NORMAL);
  63. // delete req;
  64. // }
  65. // TSendResult sr;
  66. // while (host->GetSendResult(&sr)) {
  67. // if (!sr.Success) {
  68. // printf("Send failed!\n");
  69. // sleep(0);
  70. // }
  71. // }
  72. // sleep(0);
  73. // }
  74. // }
  75. //}
  76. static void SessionLevelTest(bool client, const char* serverName, int packetSize, int packetsInFly, int srcPort) {
  77. BindToSocket(0);
  78. TIntrusivePtr<IRequester> reqHost;
  79. // reqHost = CreateHttpUdpRequester(13013);
  80. reqHost = CreateHttpUdpRequester(client ? srcPort : 13013);
  81. TUdpAddress serverAddr = CreateAddress(serverName, 13013);
  82. TVector<char> dummyPacket;
  83. dummyPacket.resize(packetSize);
  84. TReallyFastRng32 rr((unsigned int)GetCycleCount());
  85. for (size_t i = 0; i < dummyPacket.size(); ++i)
  86. dummyPacket[i] = (char)rr.Uniform(256);
  87. bool cont = true;
  88. NHPTimer::STime t;
  89. NHPTimer::GetTime(&t);
  90. THashMap<TGUID, bool, TGUIDHash> seenReqs;
  91. if (client) {
  92. THashMap<TGUID, bool, TGUIDHash> reqList;
  93. int packetsSentCount = 0;
  94. TUdpHttpRequest* udpReq;
  95. for (int i = 1; cont; ++i) {
  96. for (;;) {
  97. udpReq = reqHost->GetRequest();
  98. if (udpReq == nullptr)
  99. break;
  100. udpReq->Data.resize(10);
  101. reqHost->SendResponse(udpReq->ReqId, &udpReq->Data);
  102. delete udpReq;
  103. }
  104. while (TUdpHttpResponse* res = reqHost->GetResponse()) {
  105. THashMap<TGUID, bool, TGUIDHash>::iterator z = reqList.find(res->ReqId);
  106. if (z == reqList.end()) {
  107. printf("Unexpected response\n");
  108. abort();
  109. }
  110. reqList.erase(z);
  111. if (res->Ok) {
  112. ++packetsSentCount;
  113. //Y_ASSERT(res->Data == dummyPacket);
  114. NHPTimer::STime tChk = t;
  115. if (NHPTimer::GetTimePassed(&tChk) > 1) {
  116. printf("packet size = %d\n", dummyPacket.ysize());
  117. double passedTime = NHPTimer::GetTimePassed(&t);
  118. double rate = packetsSentCount / passedTime;
  119. printf("packet rate %g, transfer %gmb\n", rate, rate * dummyPacket.size() / 1000000);
  120. packetsSentCount = 0;
  121. }
  122. } else {
  123. printf("Failed request!\n");
  124. //Sleep(INFINITE);
  125. }
  126. delete res;
  127. }
  128. while (reqList.ysize() < packetsInFly) {
  129. *(int*)&dummyPacket[0] = i;
  130. TVector<char> fakePacket = dummyPacket;
  131. TGUID req2 = reqHost->SendRequest(serverAddr, "blaxuz", &fakePacket);
  132. reqList[req2];
  133. }
  134. reqHost->GetAsyncEvent().Wait();
  135. }
  136. } else {
  137. TUdpHttpRequest* req;
  138. for (;;) {
  139. req = reqHost->GetRequest();
  140. if (req) {
  141. if (seenReqs.find(req->ReqId) != seenReqs.end()) {
  142. printf("Request %s recieved twice!\n", GetGuidAsString(req->ReqId).c_str());
  143. }
  144. seenReqs[req->ReqId];
  145. req->Data.resize(10);
  146. reqHost->SendResponse(req->ReqId, &req->Data);
  147. delete req;
  148. } else {
  149. reqHost->GetAsyncEvent().Wait();
  150. }
  151. }
  152. }
  153. }
  154. void RunUdpTest(bool client, const char* serverName, int packetSize, int packetsInFly, int srcPort) {
  155. //PacketLevelTest(client);
  156. SessionLevelTest(client, serverName, packetSize, packetsInFly, srcPort);
  157. }
  158. }