A
ajou_king
I was running some tests on my Win32 1GHZ processor to see how long it
would take to transmit objects numerous times via TCP/IP using C#
..NET Remoting vs the C++ trustworthy method of binary streams. I ran
the test for 50K, 100K, 500K iterations, where each iteration consists
of sending an object from a client process to a server process, and the
server process sends back an ack.
Here are the results:
.NET Remoting C++ Binary TCP/IP
-------------- ------------------
50,000 Iterations: 128 seconds 3 seconds
100,000 Iterations: 300 seconds 8 seconds
500,000 Iterations: 1459 seconds 43 seconds
In the above tests the .NET remoting overhead was 42.6x, 37.5x, and
33.9x slower than the c++ version.
Here is the object that was used:
---------------------------------------------------------------
[Serializable]
public class Msg
{
public int msgType_;
public int seqNum_;
public String symbol_;
public int quoteId_;
public int responseLevel_;
public int eqiRole_;
public float bidPrice_;
public float offerPrice_;
public int bidSize_;
public int offerSize_;
public float liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}
The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int port = 7627;
int N = 50000;
int i = 0;
BinaryFormatter bF = new BinaryFormatter();
TcpListener tcpListener = new TcpListener(port);
tcpListener.Start();
Socket soTcp = tcpListener.AcceptSocket();
Console.WriteLine("SampleClient is connected through TCP.");
NetworkStream stream = new NetworkStream(soTcp,
FileAccess.ReadWrite, true);
BinaryReader bReader = new BinaryReader(stream);
DateTime beginTime = new DateTime();
while (i < N)
{
if (i == 0)
{
beginTime = System.DateTime.Now;
}
++i;
Byte[] received = new Byte[1024];
Messages.Msg msg = new Messages.Msg();
msg = (Messages.Msg)bF.Deserialize(stream);
String returningString = Convert.ToString(99);
Byte[] returningByte =
System.Text.Encoding.ASCII.GetBytes(returningString.ToCharArray());
//Returning a confirmation back to the client.
soTcp.Send(returningByte, returningByte.Length, 0);
}
DateTime endTime = System.DateTime.Now;
Console.WriteLine(endTime - beginTime);
}
}
}
The Client Process
---------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Messages;
namespace tcpClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int port = 7627;
int N = 100000;
int i = 0;
TcpClient tcpClient = new TcpClient("machine1", port);
NetworkStream tcpStream = tcpClient.GetStream();
BinaryWriter bWriter = new BinaryWriter(tcpStream);
BinaryFormatter bF = new BinaryFormatter();
while (i < N)
{
++i;
Messages.Msg m = new Messages.Msg();
m.msgType_ = 101;
m.bidPrice_ = 123.32f;
m.bidSize_ = 100;
m.eqiRole_ = 1;
m.liquidityBidPrice_ = 122.12f;
m.liquidityOfferPrice_ = 192.32f;
m.offerPrice_ = 154.25f;
m.quoteId_ = i;
m.responseLevel_ = 10;
m.symbol_ = "IBM";
bF.Serialize(tcpStream, m);
// Read back the Ack
Byte[] received = new Byte[1024];
tcpStream.Read(received, 0, received.Length);
}
}
}
}
The C++ binary streams method was to simply simulate the above but no
serialization involved - just send the byte stream of the class via
TCP/IP sockets.
If .NET Remoting / Serialization is so slow, why would anyone ever use
it over C++ for performance critical applications that transmit
hundreds of thousands of messages per day ? Is the tradeoff really
worth it ? What are people's thoughts ?
would take to transmit objects numerous times via TCP/IP using C#
..NET Remoting vs the C++ trustworthy method of binary streams. I ran
the test for 50K, 100K, 500K iterations, where each iteration consists
of sending an object from a client process to a server process, and the
server process sends back an ack.
Here are the results:
.NET Remoting C++ Binary TCP/IP
-------------- ------------------
50,000 Iterations: 128 seconds 3 seconds
100,000 Iterations: 300 seconds 8 seconds
500,000 Iterations: 1459 seconds 43 seconds
In the above tests the .NET remoting overhead was 42.6x, 37.5x, and
33.9x slower than the c++ version.
Here is the object that was used:
---------------------------------------------------------------
[Serializable]
public class Msg
{
public int msgType_;
public int seqNum_;
public String symbol_;
public int quoteId_;
public int responseLevel_;
public int eqiRole_;
public float bidPrice_;
public float offerPrice_;
public int bidSize_;
public int offerSize_;
public float liquidityBidPrice_;
public float liquidityOfferPrice_;
public int liquidityBidSize_;
public int liquidityOfferSize_;
public int checkSum_;
}
The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int port = 7627;
int N = 50000;
int i = 0;
BinaryFormatter bF = new BinaryFormatter();
TcpListener tcpListener = new TcpListener(port);
tcpListener.Start();
Socket soTcp = tcpListener.AcceptSocket();
Console.WriteLine("SampleClient is connected through TCP.");
NetworkStream stream = new NetworkStream(soTcp,
FileAccess.ReadWrite, true);
BinaryReader bReader = new BinaryReader(stream);
DateTime beginTime = new DateTime();
while (i < N)
{
if (i == 0)
{
beginTime = System.DateTime.Now;
}
++i;
Byte[] received = new Byte[1024];
Messages.Msg msg = new Messages.Msg();
msg = (Messages.Msg)bF.Deserialize(stream);
String returningString = Convert.ToString(99);
Byte[] returningByte =
System.Text.Encoding.ASCII.GetBytes(returningString.ToCharArray());
//Returning a confirmation back to the client.
soTcp.Send(returningByte, returningByte.Length, 0);
}
DateTime endTime = System.DateTime.Now;
Console.WriteLine(endTime - beginTime);
}
}
}
The Client Process
---------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Messages;
namespace tcpClient
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
int port = 7627;
int N = 100000;
int i = 0;
TcpClient tcpClient = new TcpClient("machine1", port);
NetworkStream tcpStream = tcpClient.GetStream();
BinaryWriter bWriter = new BinaryWriter(tcpStream);
BinaryFormatter bF = new BinaryFormatter();
while (i < N)
{
++i;
Messages.Msg m = new Messages.Msg();
m.msgType_ = 101;
m.bidPrice_ = 123.32f;
m.bidSize_ = 100;
m.eqiRole_ = 1;
m.liquidityBidPrice_ = 122.12f;
m.liquidityOfferPrice_ = 192.32f;
m.offerPrice_ = 154.25f;
m.quoteId_ = i;
m.responseLevel_ = 10;
m.symbol_ = "IBM";
bF.Serialize(tcpStream, m);
// Read back the Ack
Byte[] received = new Byte[1024];
tcpStream.Read(received, 0, received.Length);
}
}
}
}
The C++ binary streams method was to simply simulate the above but no
serialization involved - just send the byte stream of the class via
TCP/IP sockets.
If .NET Remoting / Serialization is so slow, why would anyone ever use
it over C++ for performance critical applications that transmit
hundreds of thousands of messages per day ? Is the tradeoff really
worth it ? What are people's thoughts ?