NetworkStatistics.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using UnityEngine;
  3. namespace Mirror
  4. {
  5. /// <summary>
  6. /// Shows Network messages and bytes sent & received per second.
  7. /// <para>Add this component to the same object as Network Manager.</para>
  8. /// </summary>
  9. [AddComponentMenu("Network/Network Statistics")]
  10. [DisallowMultipleComponent]
  11. [HelpURL("https://mirror-networking.gitbook.io/docs/components/network-statistics")]
  12. public class NetworkStatistics : MonoBehaviour
  13. {
  14. // update interval
  15. double intervalStartTime;
  16. // ---------------------------------------------------------------------
  17. // CLIENT
  18. // long bytes to support >2GB
  19. int clientIntervalReceivedPackets;
  20. long clientIntervalReceivedBytes;
  21. int clientIntervalSentPackets;
  22. long clientIntervalSentBytes;
  23. // results from last interval
  24. // long bytes to support >2GB
  25. int clientReceivedPacketsPerSecond;
  26. long clientReceivedBytesPerSecond;
  27. int clientSentPacketsPerSecond;
  28. long clientSentBytesPerSecond;
  29. // ---------------------------------------------------------------------
  30. // SERVER
  31. // capture interval
  32. // long bytes to support >2GB
  33. int serverIntervalReceivedPackets;
  34. long serverIntervalReceivedBytes;
  35. int serverIntervalSentPackets;
  36. long serverIntervalSentBytes;
  37. // results from last interval
  38. // long bytes to support >2GB
  39. int serverReceivedPacketsPerSecond;
  40. long serverReceivedBytesPerSecond;
  41. int serverSentPacketsPerSecond;
  42. long serverSentBytesPerSecond;
  43. // NetworkManager sets Transport.activeTransport in Awake().
  44. // so let's hook into it in Start().
  45. void Start()
  46. {
  47. // find available transport
  48. Transport transport = Transport.activeTransport;
  49. if (transport != null)
  50. {
  51. transport.OnClientDataReceived += OnClientReceive;
  52. transport.OnClientDataSent += OnClientSend;
  53. transport.OnServerDataReceived += OnServerReceive;
  54. transport.OnServerDataSent += OnServerSend;
  55. }
  56. else Debug.LogError($"NetworkStatistics: no available or active Transport found on this platform: {Application.platform}");
  57. }
  58. void OnDestroy()
  59. {
  60. // remove transport hooks
  61. Transport transport = Transport.activeTransport;
  62. if (transport != null)
  63. {
  64. transport.OnClientDataReceived -= OnClientReceive;
  65. transport.OnClientDataSent -= OnClientSend;
  66. transport.OnServerDataReceived -= OnServerReceive;
  67. transport.OnServerDataSent -= OnServerSend;
  68. }
  69. }
  70. void OnClientReceive(ArraySegment<byte> data, int channelId)
  71. {
  72. ++clientIntervalReceivedPackets;
  73. clientIntervalReceivedBytes += data.Count;
  74. }
  75. void OnClientSend(ArraySegment<byte> data, int channelId)
  76. {
  77. ++clientIntervalSentPackets;
  78. clientIntervalSentBytes += data.Count;
  79. }
  80. void OnServerReceive(int connectionId, ArraySegment<byte> data, int channelId)
  81. {
  82. ++serverIntervalReceivedPackets;
  83. serverIntervalReceivedBytes += data.Count;
  84. }
  85. void OnServerSend(int connectionId, ArraySegment<byte> data, int channelId)
  86. {
  87. ++serverIntervalSentPackets;
  88. serverIntervalSentBytes += data.Count;
  89. }
  90. void Update()
  91. {
  92. // calculate results every second
  93. if (NetworkTime.localTime >= intervalStartTime + 1)
  94. {
  95. if (NetworkClient.active) UpdateClient();
  96. if (NetworkServer.active) UpdateServer();
  97. intervalStartTime = NetworkTime.localTime;
  98. }
  99. }
  100. void UpdateClient()
  101. {
  102. clientReceivedPacketsPerSecond = clientIntervalReceivedPackets;
  103. clientReceivedBytesPerSecond = clientIntervalReceivedBytes;
  104. clientSentPacketsPerSecond = clientIntervalSentPackets;
  105. clientSentBytesPerSecond = clientIntervalSentBytes;
  106. clientIntervalReceivedPackets = 0;
  107. clientIntervalReceivedBytes = 0;
  108. clientIntervalSentPackets = 0;
  109. clientIntervalSentBytes = 0;
  110. }
  111. void UpdateServer()
  112. {
  113. serverReceivedPacketsPerSecond = serverIntervalReceivedPackets;
  114. serverReceivedBytesPerSecond = serverIntervalReceivedBytes;
  115. serverSentPacketsPerSecond = serverIntervalSentPackets;
  116. serverSentBytesPerSecond = serverIntervalSentBytes;
  117. serverIntervalReceivedPackets = 0;
  118. serverIntervalReceivedBytes = 0;
  119. serverIntervalSentPackets = 0;
  120. serverIntervalSentBytes = 0;
  121. }
  122. void OnGUI()
  123. {
  124. // only show if either server or client active
  125. if (NetworkClient.active || NetworkServer.active)
  126. {
  127. // create main GUI area
  128. // 105 is below NetworkManager HUD in all cases.
  129. GUILayout.BeginArea(new Rect(10, 105, 215, 300));
  130. // show client / server stats if active
  131. if (NetworkClient.active) OnClientGUI();
  132. if (NetworkServer.active) OnServerGUI();
  133. // end of GUI area
  134. GUILayout.EndArea();
  135. }
  136. }
  137. void OnClientGUI()
  138. {
  139. // background
  140. GUILayout.BeginVertical("Box");
  141. GUILayout.Label("<b>Client Statistics</b>");
  142. // sending ("msgs" instead of "packets" to fit larger numbers)
  143. GUILayout.Label($"Send: {clientSentPacketsPerSecond} msgs @ {Utils.PrettyBytes(clientSentBytesPerSecond)}/s");
  144. // receiving ("msgs" instead of "packets" to fit larger numbers)
  145. GUILayout.Label($"Recv: {clientReceivedPacketsPerSecond} msgs @ {Utils.PrettyBytes(clientReceivedBytesPerSecond)}/s");
  146. // end background
  147. GUILayout.EndVertical();
  148. }
  149. void OnServerGUI()
  150. {
  151. // background
  152. GUILayout.BeginVertical("Box");
  153. GUILayout.Label("<b>Server Statistics</b>");
  154. // sending ("msgs" instead of "packets" to fit larger numbers)
  155. GUILayout.Label($"Send: {serverSentPacketsPerSecond} msgs @ {Utils.PrettyBytes(serverSentBytesPerSecond)}/s");
  156. // receiving ("msgs" instead of "packets" to fit larger numbers)
  157. GUILayout.Label($"Recv: {serverReceivedPacketsPerSecond} msgs @ {Utils.PrettyBytes(serverReceivedBytesPerSecond)}/s");
  158. // end background
  159. GUILayout.EndVertical();
  160. }
  161. }
  162. }