public void CheckAllMessagesInOrder() { _currentNumber = 1; var port = NetworkUtils.GetRandomUnusedPort(); var ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); var transport = new SendingTransport(); var sender = new TcpTransportPipeMultiThread(100000, HighWaterMarkBehavior.Block, ipEndPoint, transport); var receiver = new TcpReceiver(); var waitHandle = new AutoResetEvent(false); var batchSize = 2000000; receiver.RegisterCallback(ipEndPoint, s => OnCheckErrorMessageReceived(s, waitHandle, batchSize)); var beforebindDataCount = 100; for (int i = 1; i < beforebindDataCount; i++) { sender.Send(new ArraySegment<byte>(BitConverter.GetBytes(i))); } Thread.Sleep(800); receiver.ListenToEndpoint(ipEndPoint); SpinWait.SpinUntil(() => _currentNumber == beforebindDataCount); Console.WriteLine("received first messages"); var watch = new Stopwatch(); watch.Start(); for (int i = beforebindDataCount; i < batchSize / 2; i++) { sender.Send(new ArraySegment<byte>(BitConverter.GetBytes(i))); } Console.WriteLine("stoppping reception on endpoint"); SpinWait.SpinUntil(() => _currentNumber >= batchSize / 2); receiver.StopListeningTo(ipEndPoint); Thread.Sleep(100); Console.WriteLine("re-establishing reception on endpoint"); sender.Send(new ArraySegment<byte>(BitConverter.GetBytes(batchSize / 2))); sender.Send(new ArraySegment<byte>(BitConverter.GetBytes(batchSize / 2 + 1))); Thread.Sleep(100); receiver.ListenToEndpoint(ipEndPoint); Thread.Sleep(500); for (int i = batchSize / 2 +2; i < batchSize; i++) { // sender.Send(ipEndPoint, BitConverter.GetBytes(i)); sender.Send(new ArraySegment<byte>(BitConverter.GetBytes(i))); } Console.WriteLine("sent last messages"); waitHandle.WaitOne(); if(_currentNumber != batchSize) Assert.Fail(); watch.Stop(); var fps = batchSize / (watch.ElapsedMilliseconds / 1000m); Console.WriteLine(string.Format("FPS = : {0} mess/sec, elapsed : {1} ms, messages {2}", fps.ToString("N2"), watch.ElapsedMilliseconds, batchSize)); sender.Dispose(); transport.Dispose(); receiver.Dispose(); Thread.Sleep(500); }
public void tcp() { var waitForMessage1 = new ManualResetEvent(false); var ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2000); var transport = new SendingTransport(1); var sender = new TcpTransportPipeMultiThread(600000, HighWaterMarkBehavior.Block, ipEndPoint, transport); var receiver = new TcpReceiver(); Thread.Sleep(1000); receiver.RegisterCallback(ipEndPoint, s => OnIpEndpointMessageReceived(s,waitForMessage1,ipEndPoint)); receiver.ListenToEndpoint(ipEndPoint); Thread senderThread = new Thread(() => { _sentBuffer = Encoding.ASCII.GetBytes(String.Join("", Enumerable.Range(0, 35).Select(x => x.ToString()))); // sender.Send(ipEndPoint, Encoding.ASCII.GetBytes("stop")); sender.Send(new ArraySegment<byte>(Encoding.ASCII.GetBytes("stop"))); _messSentNumber++; Console.WriteLine("after first sends"); waitForMessage1.WaitOne(); waitForMessage1.Reset(); Thread.Sleep(1000); for (int j = 0; j < 20; j++) { GC.Collect(2,GCCollectionMode.Forced,true); Console.WriteLine("Entering loop"); var watch = new Stopwatch(); watch.Start(); var batchSize = 1000000; for (int i = 0; i < batchSize; i++) { _messSentNumber++; sender.Send(new ArraySegment<byte>(_sentBuffer)); // Thread.SpinWait(200); Thread.Sleep(0); Thread.Sleep(0); Thread.Sleep(0); } sender.Send(new ArraySegment<byte>(Encoding.ASCII.GetBytes("stop"))); _messSentNumber++; // sender.Send(ipEndPoint, Encoding.ASCII.GetBytes("stop")); waitForMessage1.WaitOne(); waitForMessage1.Reset(); watch.Stop(); Assert.AreEqual(_messSentNumber, _messNumber); var fps = batchSize / (watch.ElapsedMilliseconds / 1000m); Console.WriteLine(string.Format("FPS = : {0} mess/sec, elapsed : {1} ms, messages {2}", fps.ToString("N2"), watch.ElapsedMilliseconds, batchSize)); } }); senderThread.Start(); senderThread.Join(); sender.Dispose(); receiver.Dispose(); Thread.Sleep(1000); }