LoopbackPerformanceTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "RakPeerInterface.h"
  11. #include "MessageIdentifiers.h" // Enumerations
  12. #include "GetTime.h"
  13. #include "RakNetStatistics.h"
  14. #include <cstdio>
  15. #include <stdlib.h>
  16. #include "Gets.h"
  17. #ifdef _WIN32
  18. #include "WindowsIncludes.h" // Sleep
  19. #define SLEEP(arg) ( Sleep( (arg) ) )
  20. #else
  21. #include <unistd.h> // usleep
  22. #define SLEEP(arg) ( usleep( (arg) *1000 ) )
  23. #endif
  24. static const int DESTINATION_SYSTEM_PORT=60000;
  25. static const int RELAY_SYSTEM_PORT=60001;
  26. static const int SOURCE_SYSTEM_PORT=60002;
  27. int main(void)
  28. {
  29. RakNet::RakPeerInterface *localSystem;
  30. RakNet::Packet *p;
  31. int systemType;
  32. unsigned char byteBlock[4096];
  33. RakNet::TimeMS time, quitTime, nextStatsTime;
  34. unsigned int packetsPerSecond, bytesPerPacket, num,index, bytesInPackets;
  35. RakNet::TimeMS lastSendTime;
  36. int sendMode;
  37. int verbosityLevel;
  38. unsigned int showStatsInterval;
  39. bool connectionCompleted, incomingConnectionCompleted;
  40. RakNet::RakNetStatistics *rss;
  41. printf("Loopback performance test.\n");
  42. printf("This test measures the effective transfer rate of RakNet.\n\n");
  43. printf("Instructions:\nStart 3 instances of this program.\n");
  44. printf("Press\n1. for the first instance (destination)\n2. for the second instance (relay)\n3. for the third instance (source).\n");
  45. printf("When the third instance is started the test will start.\n\n");
  46. printf("Difficulty: Intermediate\n\n");
  47. printf("Which instance is this? Enter 1, 2, or 3: ");
  48. Gets((char*)byteBlock, sizeof(byteBlock));
  49. systemType=byteBlock[0]-'0'-1;
  50. if (systemType < 0 || systemType > 2)
  51. {
  52. printf("Error, you must enter 1, 2, or 3.\nQuitting.\n");
  53. return 1;
  54. }
  55. localSystem=RakNet::RakPeerInterface::GetInstance();
  56. printf("How many seconds do you want to run the test for?\n");
  57. Gets((char*)byteBlock, sizeof(byteBlock));
  58. if (byteBlock[0]==0)
  59. {
  60. printf("Defaulting to 90 seconds\n");
  61. quitTime=90;
  62. }
  63. else
  64. quitTime=atoi((char*)byteBlock);
  65. printf("Enter statistics verbosity level, 0=lowest, 2=highest\n");
  66. Gets((char*)byteBlock, sizeof(byteBlock));
  67. if (byteBlock[0]==0)
  68. {
  69. printf("Defaulting to verbosity level 1\n");
  70. verbosityLevel=1;
  71. }
  72. else
  73. verbosityLevel=atoi((char*)byteBlock);
  74. printf("How frequently to show statistics, in seconds?\n");
  75. Gets((char*)byteBlock, sizeof(byteBlock));
  76. if (byteBlock[0]==0)
  77. {
  78. printf("Defaulting to 5 seconds\n");
  79. showStatsInterval=5*1000;
  80. }
  81. else
  82. showStatsInterval=atoi((char*)byteBlock)*1000;
  83. if (systemType==0)
  84. {
  85. printf("Initializing Raknet...\n");
  86. // Destination. Accept one connection and wait for further instructions.
  87. RakNet::SocketDescriptor socketDescriptor(DESTINATION_SYSTEM_PORT,0);
  88. if (localSystem->Startup(1, &socketDescriptor, 1)!=RakNet::RAKNET_STARTED)
  89. {
  90. printf("Failed to initialize RakNet!.\nQuitting\n");
  91. return 1;
  92. }
  93. localSystem->SetMaximumIncomingConnections(1);
  94. printf("Initialization complete. Destination system started and waiting...\n");
  95. }
  96. else if (systemType==1)
  97. {
  98. printf("What send mode to use for relays?\n");
  99. printf("(0). UNRELIABLE\n");
  100. printf("(1). UNRELIABLE_SEQUENCED\n");
  101. printf("(2). RELIABLE\n");
  102. printf("(3). RELIABLE_ORDERED\n");
  103. printf("(4). RELIABLE_SEQUENCED\n");
  104. Gets((char*)byteBlock, sizeof(byteBlock));
  105. if (byteBlock[0]==0)
  106. {
  107. printf("Defaulting to RELIABLE\n");
  108. sendMode=2;
  109. }
  110. else
  111. {
  112. sendMode=atoi((char*)byteBlock);
  113. if (sendMode < 0 || sendMode > 4)
  114. {
  115. printf("Invalid send mode. Using UNRELIABLE\n");
  116. sendMode=0;
  117. }
  118. }
  119. printf("Initializing Raknet...\n");
  120. // Relay. Accept one connection, initiate outgoing connection, wait for further instructions.
  121. RakNet::SocketDescriptor socketDescriptor(RELAY_SYSTEM_PORT,0);
  122. if (localSystem->Startup(2, &socketDescriptor, 1)!=RakNet::RAKNET_STARTED)
  123. {
  124. printf("Failed to initialize RakNet!.\nQuitting\n");
  125. return 1;
  126. }
  127. localSystem->SetMaximumIncomingConnections(1);
  128. socketDescriptor.port=DESTINATION_SYSTEM_PORT;
  129. if (localSystem->Connect("127.0.0.1", DESTINATION_SYSTEM_PORT, 0, 0)!=RakNet::CONNECTION_ATTEMPT_STARTED)
  130. {
  131. printf("Connect call failed!.\nQuitting\n");
  132. return 1;
  133. }
  134. printf("Initialization complete. Relay system started.\nConnecting to destination and waiting for sender...\n");
  135. }
  136. else
  137. {
  138. printf("How many packets do you wish to send per second?\n");
  139. Gets((char*)byteBlock, sizeof(byteBlock));
  140. if (byteBlock[0]==0)
  141. {
  142. #ifdef _DEBUG
  143. printf("Defaulting to 1000\n");
  144. packetsPerSecond=1000;
  145. #else
  146. printf("Defaulting to 10000\n");
  147. packetsPerSecond=10000;
  148. #endif
  149. }
  150. else
  151. packetsPerSecond=atoi((char*)byteBlock);
  152. printf("How many bytes per packet?\n");
  153. Gets((char*)byteBlock, sizeof(byteBlock));
  154. if (byteBlock[0]==0)
  155. {
  156. printf("Defaulting to 400\n");
  157. bytesPerPacket=400;
  158. }
  159. else
  160. {
  161. bytesPerPacket=atoi((char*)byteBlock);
  162. if (bytesPerPacket > 4096)
  163. {
  164. printf("Increase the array size of byteBlock to send more than 4096 bytes.\n");
  165. bytesPerPacket=4096;
  166. }
  167. }
  168. printf("What send mode?\n");
  169. printf("(0). UNRELIABLE\n");
  170. printf("(1). UNRELIABLE_SEQUENCED\n");
  171. printf("(2). RELIABLE\n");
  172. printf("(3). RELIABLE_ORDERED\n");
  173. printf("(4). RELIABLE_SEQUENCED\n");
  174. Gets((char*)byteBlock, sizeof(byteBlock));
  175. if (byteBlock[0]==0)
  176. {
  177. printf("Defaulting to RELIABLE\n");
  178. sendMode=2;
  179. }
  180. else
  181. {
  182. sendMode=atoi((char*)byteBlock);
  183. if (sendMode < 0 || sendMode > 4)
  184. {
  185. printf("Invalid send mode. Using UNRELIABLE\n");
  186. sendMode=0;
  187. }
  188. }
  189. printf("Initializing RakNet...\n");
  190. // Sender. Initiate outgoing connection to relay.
  191. RakNet::SocketDescriptor socketDescriptor(SOURCE_SYSTEM_PORT,0);
  192. if (localSystem->Startup(1, &socketDescriptor, 1)!=RakNet::RAKNET_STARTED)
  193. {
  194. printf("Failed to initialize RakNet!.\nQuitting\n");
  195. return 1;
  196. }
  197. if (localSystem->Connect("127.0.0.1", RELAY_SYSTEM_PORT, 0, 0)!=RakNet::CONNECTION_ATTEMPT_STARTED)
  198. {
  199. printf("Connect call failed!.\nQuitting\n");
  200. return 1;
  201. }
  202. printf("Initialization complete. Sender system started. Connecting to relay...\n");
  203. }
  204. connectionCompleted=false;
  205. incomingConnectionCompleted=false;
  206. time = RakNet::GetTimeMS();
  207. lastSendTime=time;
  208. nextStatsTime=time+2000; // First stat shows up in 2 seconds
  209. bytesInPackets=0;
  210. while (time < quitTime || (connectionCompleted==false && incomingConnectionCompleted==false))
  211. {
  212. time = RakNet::GetTimeMS();
  213. // Parse messages
  214. while (1)
  215. {
  216. p = localSystem->Receive();
  217. if (p)
  218. {
  219. bytesInPackets+=p->length;
  220. switch (p->data[0])
  221. {
  222. case ID_CONNECTION_REQUEST_ACCEPTED:
  223. printf("ID_CONNECTION_REQUEST_ACCEPTED.\n");
  224. connectionCompleted=true;
  225. // Timer starts when a connection has completed
  226. if (systemType==1 || systemType==2)
  227. quitTime=quitTime*1000 + time;
  228. break;
  229. case ID_DISCONNECTION_NOTIFICATION:
  230. // Connection lost normally
  231. printf("ID_DISCONNECTION_NOTIFICATION.\n");
  232. // connectionCompleted=false;
  233. break;
  234. case ID_NEW_INCOMING_CONNECTION:
  235. // Somebody connected. We have their IP now
  236. printf("ID_NEW_INCOMING_CONNECTION.\n");
  237. incomingConnectionCompleted=true;
  238. // Timer starts when a new connection has come in
  239. if (systemType==0)
  240. quitTime=quitTime*1000 + time;
  241. if (systemType==1 && connectionCompleted==false)
  242. printf("Warning, relay connection to destination has not completed yet.\n");
  243. break;
  244. case ID_CONNECTION_LOST:
  245. // Couldn't deliver a reliable packet - i.e. the other system was abnormally
  246. // terminated
  247. printf("ID_CONNECTION_LOST.\n");
  248. // connectionCompleted=false;
  249. break;
  250. case ID_NO_FREE_INCOMING_CONNECTIONS:
  251. printf("ID_NO_FREE_INCOMING_CONNECTIONS.\n");
  252. break;
  253. default:
  254. // The relay system will relay all data with 255 as the first byte
  255. if (systemType==1)
  256. {
  257. if (p->data[0]==255)
  258. {
  259. if (localSystem->Send((char*)p->data, p->length, HIGH_PRIORITY, (PacketReliability)sendMode, 0, p->systemAddress, true)==false)
  260. {
  261. printf("Relay failed!\n");
  262. }
  263. }
  264. else
  265. printf("Got packet with ID %u\n", p->data[0]);
  266. }
  267. break;
  268. }
  269. }
  270. else
  271. break;
  272. localSystem->DeallocatePacket(p);
  273. }
  274. // Show stats.
  275. if (time > nextStatsTime && (connectionCompleted || incomingConnectionCompleted))
  276. {
  277. printf("\n* First connected system statistics:\n");
  278. rss=localSystem->GetStatistics(localSystem->GetSystemAddressFromIndex(0));
  279. StatisticsToString(rss, (char*)byteBlock, verbosityLevel);
  280. printf("%s", byteBlock);
  281. if (systemType==1)
  282. {
  283. rss=localSystem->GetStatistics(localSystem->GetSystemAddressFromIndex(1));
  284. if (rss)
  285. {
  286. printf("* Second connected system statistics:\n");
  287. StatisticsToString(rss, (char*)byteBlock, verbosityLevel);
  288. printf("%s", byteBlock);
  289. }
  290. }
  291. nextStatsTime = time + showStatsInterval;
  292. }
  293. // As the destination, we don't care if the connection is completed. Do nothing
  294. // As the relay, we relay packets if the connection is completed.
  295. // That is done when the packet arrives.
  296. // As the source, we start sending packets when the connection is completed.
  297. if (systemType==2 && connectionCompleted)
  298. {
  299. // Number of packets to send is (float)(packetsPerSecond * (time - lastSendTime)) / 1000.0f;
  300. num=(packetsPerSecond * (unsigned int) (time - lastSendTime)) / 1000;
  301. byteBlock[0]=255; // Relay all data with an identifier of 255
  302. for (index=0; index < num; index++)
  303. {
  304. localSystem->Send((char*)byteBlock, bytesPerPacket, HIGH_PRIORITY, (PacketReliability)sendMode, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
  305. }
  306. lastSendTime+= (1000 * num) / packetsPerSecond;
  307. }
  308. SLEEP(100);
  309. }
  310. printf("Test duration elapsed. Final Stats:\n");
  311. printf("\n* First connected system statistics:\n");
  312. rss=localSystem->GetStatistics(localSystem->GetSystemAddressFromIndex(0));
  313. StatisticsToString(rss, (char*)byteBlock, 2);
  314. printf("%s", byteBlock);
  315. if (systemType==1)
  316. {
  317. rss=localSystem->GetStatistics(localSystem->GetSystemAddressFromIndex(1));
  318. if (rss)
  319. {
  320. printf("* Second connected system statistics:\n");
  321. StatisticsToString(rss, (char*)byteBlock, 2);
  322. printf("%s", byteBlock);
  323. }
  324. }
  325. printf("Hit enter to continue.\n");
  326. char buff[100];
  327. Gets(buff,sizeof(buff));
  328. RakNet::RakPeerInterface::DestroyInstance(localSystem);
  329. return 0;
  330. }
粤ICP备19079148号