|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Sockets; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using LiteNetLib; |
| 9 | +using LiteNetLib.Utils; |
| 10 | + |
| 11 | +namespace LibSample |
| 12 | +{ |
| 13 | + public class ThroughputTest : IExample |
| 14 | + { |
| 15 | + public const long TOTAL_BYTES = 1024L * 1024L * 16; // 256 MB |
| 16 | + public const int SEND_CHUNK_SIZE = 1024 * 1024; // 1 MB |
| 17 | + public const int SEND_CHUNK_COUNT = (int)(TOTAL_BYTES / SEND_CHUNK_SIZE); |
| 18 | + |
| 19 | + public class Server : INetEventListener, IDisposable |
| 20 | + { |
| 21 | + public int ReliableReceived { get; private set; } |
| 22 | + public bool HasCompleted { get; private set; } |
| 23 | + |
| 24 | + readonly NetManager _server; |
| 25 | + |
| 26 | + public TimeSpan TransferTime => _lastChunkReceiveTime - _peerConnectTime; |
| 27 | + |
| 28 | + DateTime _peerConnectTime; |
| 29 | + DateTime _lastChunkReceiveTime; |
| 30 | + |
| 31 | + public NetStatistics Stats => _server.Statistics; |
| 32 | + |
| 33 | + public Server(int latency, int jitter, int packetLoss) |
| 34 | + { |
| 35 | + _server = new NetManager(this) |
| 36 | + { |
| 37 | + AutoRecycle = true, |
| 38 | + UpdateTime = 1, |
| 39 | + SimulatePacketLoss = true, |
| 40 | + SimulationPacketLossChance = packetLoss, |
| 41 | + SimulateLatency = true, |
| 42 | + SimulationMinLatency = latency, |
| 43 | + SimulationMaxLatency = latency + jitter, |
| 44 | + EnableStatistics = true, |
| 45 | + UnsyncedEvents = true |
| 46 | + }; |
| 47 | + _server.Start(9050); |
| 48 | + } |
| 49 | + |
| 50 | + void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode) |
| 51 | + { |
| 52 | + Console.WriteLine($"Server: error: {socketErrorCode}"); |
| 53 | + } |
| 54 | + |
| 55 | + void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + public void OnConnectionRequest(ConnectionRequest request) |
| 60 | + { |
| 61 | + request.AcceptIfKey("ConnKey"); |
| 62 | + } |
| 63 | + |
| 64 | + void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod) |
| 65 | + { |
| 66 | + if (++ReliableReceived == SEND_CHUNK_COUNT) |
| 67 | + { |
| 68 | + _lastChunkReceiveTime = DateTime.UtcNow; |
| 69 | + HasCompleted = true; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, |
| 74 | + UnconnectedMessageType messageType) |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + void INetEventListener.OnPeerConnected(NetPeer peer) |
| 79 | + { |
| 80 | + Console.WriteLine($"Server: client connected: {peer}"); |
| 81 | + |
| 82 | + _peerConnectTime = DateTime.UtcNow; |
| 83 | + } |
| 84 | + |
| 85 | + void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) |
| 86 | + { |
| 87 | + Console.WriteLine($"Server: client disconnected: {disconnectInfo.Reason}"); |
| 88 | + } |
| 89 | + |
| 90 | + public void Dispose() |
| 91 | + { |
| 92 | + _server.Stop(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public class Client : INetEventListener, IDisposable |
| 97 | + { |
| 98 | + public int ReliableSent; |
| 99 | + |
| 100 | + public bool HasCompleted { get; private set; } |
| 101 | + public bool IsRunning => _peer.ConnectionState == ConnectionState.Connected; |
| 102 | + |
| 103 | + readonly NetManager _client; |
| 104 | + NetPeer _peer; |
| 105 | + |
| 106 | + public NetStatistics Stats => _client.Statistics; |
| 107 | + |
| 108 | + public Client(int latency, int jitter, int packetLoss) |
| 109 | + { |
| 110 | + _client = new NetManager(this) |
| 111 | + { |
| 112 | + UnsyncedEvents = true, |
| 113 | + AutoRecycle = true, |
| 114 | + SimulatePacketLoss = true, |
| 115 | + SimulationPacketLossChance = packetLoss, |
| 116 | + SimulateLatency = true, |
| 117 | + SimulationMinLatency = latency, |
| 118 | + SimulationMaxLatency = latency + jitter, |
| 119 | + EnableStatistics = true |
| 120 | + }; |
| 121 | + _client.Start(); |
| 122 | + } |
| 123 | + |
| 124 | + public void SendReliable(byte[] data) |
| 125 | + { |
| 126 | + _peer.Send(data, DeliveryMethod.ReliableOrdered); |
| 127 | + ReliableSent++; |
| 128 | + } |
| 129 | + |
| 130 | + public void Connect() |
| 131 | + { |
| 132 | + _peer = _client.Connect("localhost", 9050, "ConnKey"); |
| 133 | + } |
| 134 | + |
| 135 | + void INetEventListener.OnNetworkError(IPEndPoint endPoint, SocketError socketErrorCode) |
| 136 | + { |
| 137 | + Console.WriteLine($"Client: error: {socketErrorCode}"); |
| 138 | + } |
| 139 | + |
| 140 | + void INetEventListener.OnNetworkLatencyUpdate(NetPeer peer, int latency) |
| 141 | + { |
| 142 | + } |
| 143 | + |
| 144 | + public void OnConnectionRequest(ConnectionRequest request) |
| 145 | + { |
| 146 | + request.RejectForce(); |
| 147 | + } |
| 148 | + |
| 149 | + void INetEventListener.OnNetworkReceive(NetPeer peer, NetPacketReader reader, byte channelNumber, DeliveryMethod deliveryMethod) |
| 150 | + { |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + void INetEventListener.OnNetworkReceiveUnconnected(IPEndPoint remoteEndPoint, NetPacketReader reader, |
| 155 | + UnconnectedMessageType messageType) |
| 156 | + { |
| 157 | + } |
| 158 | + |
| 159 | + void INetEventListener.OnPeerConnected(NetPeer peer) |
| 160 | + { |
| 161 | + Task.Run(() => |
| 162 | + { |
| 163 | + var data = new byte[SEND_CHUNK_SIZE]; |
| 164 | + var r = new Random(); |
| 165 | + |
| 166 | + for (int i = 0; i < SEND_CHUNK_COUNT; i++) |
| 167 | + { |
| 168 | + r.NextBytes(data); |
| 169 | + SendReliable(data); |
| 170 | + } |
| 171 | + |
| 172 | + HasCompleted = true; |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + void INetEventListener.OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo) |
| 177 | + { |
| 178 | + Console.WriteLine($"Client: Disconnected {disconnectInfo.Reason}"); |
| 179 | + } |
| 180 | + |
| 181 | + public void Dispose() |
| 182 | + { |
| 183 | + _client.Stop(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public void Run() |
| 188 | + { |
| 189 | + Console.WriteLine("Testing Throughput..."); |
| 190 | + |
| 191 | + int[] packetLossValues = new int[] { 0, 1, 2, 5, 10 }; |
| 192 | + int[] latencies = new int[] { 5, 10, 20, 40, 60, 80, 100, 150, 200, 300 }; |
| 193 | + |
| 194 | + Dictionary<int, List<TimeSpan>> resultGroups = new Dictionary<int, List<TimeSpan>>(); |
| 195 | + |
| 196 | + foreach (var packetLoss in packetLossValues) |
| 197 | + { |
| 198 | + var results = new List<TimeSpan>(); |
| 199 | + |
| 200 | + resultGroups.Add(packetLoss, results); |
| 201 | + |
| 202 | + foreach (var latency in latencies) |
| 203 | + { |
| 204 | + var jitter = Math.Max(1, (int)Math.Round(latency / 5f)); |
| 205 | + |
| 206 | + var serverThread = new Thread(() => StartServer(latency, jitter, packetLoss, results)); |
| 207 | + serverThread.Start(); |
| 208 | + |
| 209 | + var clientThread = new Thread(() => StartClient(latency, jitter, packetLoss)); |
| 210 | + clientThread.Start(); |
| 211 | + |
| 212 | + Console.WriteLine($"Processing, latency: {latency} ms, jitter: {jitter}, packet loss: {packetLoss} %..."); |
| 213 | + |
| 214 | + serverThread.Join(); |
| 215 | + clientThread.Join(); |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + foreach (var resultGroup in resultGroups) |
| 220 | + { |
| 221 | + Console.WriteLine($"Results for packet loss: {resultGroup.Key} %"); |
| 222 | + |
| 223 | + for (int i = 0; i < latencies.Length; i++) |
| 224 | + { |
| 225 | + var bytesPerSec = TOTAL_BYTES / resultGroup.Value[i].TotalSeconds; |
| 226 | + Console.WriteLine($"\t{latencies[i]} ms -> {resultGroup.Value[i]}\t{bytesPerSec / 1024:F2} kB/s"); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + static void StartServer(int latency, int jitter, int packetLoss, List<TimeSpan> results) |
| 232 | + { |
| 233 | + using (Server s = new Server(latency, jitter, packetLoss)) |
| 234 | + { |
| 235 | + while (!s.HasCompleted) |
| 236 | + Thread.Sleep(100); |
| 237 | + |
| 238 | + Console.WriteLine("SERVER RECEIVED -> Reliable: " + s.ReliableReceived + " in " + s.TransferTime); |
| 239 | + Console.WriteLine("SERVER STATS:\n" + s.Stats); |
| 240 | + |
| 241 | + results.Add(s.TransferTime); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + static void StartClient(int latency, int jitter, int packetLoss) |
| 246 | + { |
| 247 | + using (Client c = new Client(latency, jitter, packetLoss)) |
| 248 | + { |
| 249 | + c.Connect(); |
| 250 | + |
| 251 | + while (!c.HasCompleted || c.IsRunning) |
| 252 | + Thread.Sleep(100); |
| 253 | + |
| 254 | + Console.WriteLine("CLIENT SENT -> Reliable: " + c.ReliableSent); |
| 255 | + Console.WriteLine("CLIENT STATS:\n" + c.Stats); |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | +} |
0 commit comments