Re: .NET Remoting/Serialization way too slow vs C++ binary/tcp
From: Willy Denoyette [MVP] (willy.denoyette_at_telenet.be)
Date: 03/17/05
- Next message: David Levine: "Re: Exception management question..."
- Previous message: David Levine: "Re: Exception management question..."
- In reply to: ajou_king_at_yahoo.com: ".NET Remoting/Serialization way too slow vs C++ binary/tcp"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 18 Mar 2005 00:00:53 +0100
<ajou_king@yahoo.com> wrote in message
news:1110898272.233605.189020@o13g2000cwo.googlegroups.com...
>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 ?
>
You should never use the BinaryFormatter to serialize an object over a
NetworkStream.
Or you should use the Remoting infrastructure, or use a MemoryStream to
serialize the object to a byte array and send the client the length of the
array (as an int) followed by the byte array. The receiving side has to
deserialize the byte array back into an object and cast it to the desired
object instance.
Following class illustrates the process:
public class Serializer {
public Serializer(){}
public byte[] Serialize(object o){
byte[] buffer;
using(MemoryStream ms = new MemoryStream())
{
BinaryFormatter b = new BinaryFormatter();
b.Serialize(ms, o);
if (ms.Length > int.MaxValue)
throw new ArgumentException("Serialized object is larger than
can fit into byte array");
buffer = ms.GetBuffer();
}
return buffer;
}
public object DeSerialize(byte[] bytes){
object o;
using(MemoryStream ms = new MemoryStream(bytes))
{
BinaryFormatter b = new BinaryFormatter();
o = b.Deserialize(ms);
}
return o;
}
}
Usage:
//Client....
...
Serializer se = new Serializer();
while(...)
...
sb = se.Serialize(m);
bWriter.Write(sb.Length); // Write size in bytes of serialized
array
bWriter.Write(sb);
}
Serializer se = new Serializer();
while (i < N)
{
++i;
int length = bReader.ReadInt32();
byte[] data = bReader.ReadBytes(length);
Msg o = se.DeSerialize(data) as Msg;
...
}
Using this method it should be possible to achieve results approaching these
obtained with C++.
Willy.
- Next message: David Levine: "Re: Exception management question..."
- Previous message: David Levine: "Re: Exception management question..."
- In reply to: ajou_king_at_yahoo.com: ".NET Remoting/Serialization way too slow vs C++ binary/tcp"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|