Skip to content

Instantly share code, notes, and snippets.

@ruslander
Created October 29, 2015 22:54
Show Gist options
  • Select an option

  • Save ruslander/9c0514338d362095676d to your computer and use it in GitHub Desktop.

Select an option

Save ruslander/9c0514338d362095676d to your computer and use it in GitHub Desktop.
Baseline Request Replay over sockets
[TestFixture]
public class ReqRepSocketBaselineTests
{
[Test]
public void Should_be_fast_10k()
{
var duration = Tc(10 * 1000);
Assert.Less(duration, 825);
}
[Test]
public void Should_be_fast_100k()
{
var duration = Tc(100 * 1000);
Assert.Less(duration, 2700);
}
[Test]
public void Should_be_fast_1mln()
{
var duration = Tc(1000 * 1000);
Assert.Less(duration, 22000);
}
public long Tc(int numberOfMessages)
{
var server = new TcpSocketServer();
var watch = Stopwatch.StartNew();
using (var cl = new TcpSocketClient())
{
for (var i = 0; i < numberOfMessages; i++)
cl.Send();
}
watch.Stop();
Console.WriteLine();
Console.WriteLine("Messages : " + numberOfMessages);
Console.WriteLine("Duraton : " + TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds));
Console.WriteLine("Roundtrip(millisec) : " + (((float)watch.ElapsedMilliseconds / numberOfMessages)));
return watch.ElapsedMilliseconds;
}
[Test]
public void Should_connect_disconnect_fast()
{
var server = new TcpSocketServer();
const int messages = 10 * 1000;
var watch = Stopwatch.StartNew();
for (var i = 0; i < messages; i++)
using (var cl = new TcpSocketClient())
cl.Send();
watch.Stop();
Console.WriteLine();
Console.WriteLine("Messages : " + messages);
Console.WriteLine("Duraton : " + TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds));
Console.WriteLine("Roundtrip(millisec) : " + (((float)watch.ElapsedMilliseconds / messages)));
Assert.Less(watch.ElapsedMilliseconds, 15034);
}
}
public class TcpSocketClient : IDisposable
{
readonly Socket _sender;
public TcpSocketClient()
{
try
{
var ipHostInfo = Dns.Resolve(Dns.GetHostName());
var ipAddress = ipHostInfo.AddressList[0];
var remoteEp = new IPEndPoint(ipAddress, 11000);
_sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_sender.Connect(remoteEp);
Console.WriteLine("Socket connected to {0}", _sender.RemoteEndPoint);
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e);
}
}
public string Send()
{
var bytes = new byte[1024];
try
{
var bytesSent = _sender.Send(Encoding.ASCII.GetBytes("This is a test<EOF>"));
var bytesRec = _sender.Receive(bytes);
return Encoding.ASCII.GetString(bytes, 0, bytesRec);
//Console.Write(",");
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e);
}
return "";
}
public void Dispose()
{
_sender.Shutdown(SocketShutdown.Both);
_sender.Close();
}
}
public class TcpSocketServer
{
public TcpSocketServer()
{
Task.Factory.StartNew(StartListening, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}
static bool SocketConnected(Socket s)
{
bool part1 = s.Poll(1000, SelectMode.SelectRead);
bool part2 = (s.Available == 0);
if (part1 && part2)
return false;
else
return true;
}
public static void StartListening()
{
var ipHostInfo = Dns.Resolve(Dns.GetHostName());
var ipAddress = ipHostInfo.AddressList[0];
var localEndPoint = new IPEndPoint(ipAddress, 11000);
var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
Console.WriteLine("Waiting for a connection...");
var handler = listener.Accept();
var data = string.Empty;
var bytes = new Byte[1024];
while (true)
{
var bytesRec = handler.Receive(bytes);
data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
if (data.IndexOf("<EOF>") > -1)
{
//Console.Write(".");
var msg = Encoding.ASCII.GetBytes(data);
handler.Send(msg);
bytes = new byte[1024];
data = string.Empty;
continue;
}
if (!SocketConnected(handler))
{
handler.Shutdown(SocketShutdown.Both);
handler.Close();
break;
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment