473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

.NET Remoting/Serialization way too slow vs C++ binary/tcp

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(returningStrin g.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 ?

Nov 16 '05 #1
11 11894
Remoting is different from just shooting the contents of a class over
the wire. With remoting, you have method calls that are taking place, sinks
that are manipulating the call before it gets to the call site (on both
ends, etc, etc). This definitely contributes to the overall time it takes
to make a call over remoting.

Also, with serialization, you get that out of the box, with little or no
modification to your class (with the exception of the Serializable
attribute). In C++, I've seen some pretty contrived ways of serializing
information to send over the wire. Serialization really shines in .NET when
you have large object graphs that need to be serialized.

Your example class is somewhat simplistic. Granted, it could be all you
ever send, but in more complex situations, I think that the C++ solution
will become difficult to implement (I could be very wrong here, granted).

In the end though, you have to measure what your needs are. If
processing 500K methods in 1459 seconds (an average of 342 calls a second)
is not sufficient, then don't use it. However, if you only need to process
say, 40 operations a second, then this would be more than sufficient, and I
would take the .NET solution because it would be much easier to implement.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<aj*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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(returningStrin g.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 ?

Nov 16 '05 #2
This doesn't sound right. First, a terminology point.
This isn't remoting b/c you're not using the remoting
classes (RemotingConfiguration, ChannelServices, etc.).
But it IS serialization and I would expect it to be doing
this.

One item that jumps out at me is the memory allocation.
Your server is creating memory in every iteration. Of
course, if you're doing that in native C++, it's a fair
comparison.

Could you post the C++ as well?
-----Original Message-----
I was running some tests on my Win32 1GHZ processor to see how long itwould take to transmit objects numerous times via TCP/IP using C#..NET Remoting vs the C++ trustworthy method of binary streams. I ranthe test for 50K, 100K, 500K iterations, where each iteration consistsof sending an object from a client process to a server process, and theserver process sends back an ack.
Here are the results:

.NET Remoting C++ Binary TCP/IP -------------- ------- -----------50,000 Iterations: 128 seconds 3 seconds100,000 Iterations: 300 seconds 8 seconds500,000 Iterations: 1459 seconds 43 seconds
In the above tests the .NET remoting overhead was 42.6x, 37.5x, and33.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 noserialization involved - just send the byte stream of the class viaTCP/IP sockets.

If .NET Remoting / Serialization is so slow, why would anyone ever useit over C++ for performance critical applications that transmithundreds of thousands of messages per day ? Is the tradeoff reallyworth it ? What are people's thoughts ?

.

Nov 16 '05 #3
> 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 ?


If you're only doing hundreds of thousands per day, then the fact that it
takes 5 minutes to handle 100,000 iterations means that you could handle
nearly 29 million iterations every 24 hours.

But why not just do the same thing in C# that you're doing in C++ if
performance is critical? That's the tradeoff. If you want performance, open
a socket, pack the data in a byte array and send it out. The serialization
stuff in .NET is necessarily slow because it requires reflection. I can't
really speak for the remoting stuff as I haven't had a need to use it. If
performance is an issue, serialization isn't your friend.

Pete
Nov 16 '05 #4
Also, serialization provides for hardware independence. Presumably, with
C++, you're not doing anything to protect against one machine using
big-endian and another using little-endian. With .NET serialization, that's
taken care of for you.

I didn't say it before, and I may be in the minority in saying it, but I
don't think this is an unreasonable volume for transmission. The two
methods should not be this far apart. If they are, then I agree that it's
unacceptable. Remoting has many benefits, but ease-of-use isn't one of
them, IMO. (if you didn't care about performance, you'd be using SOAP,
because that's ridiculously easy).

I'm really interested to see how this turns out. If you can post the C++
code, it will be really helpful for a comparative analysis.

Dave
"Pete Davis" <pd******@NOSPAM.hotmail.com> wrote in message
news:BM********************@giganews.com...
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 ?

If you're only doing hundreds of thousands per day, then the fact that it
takes 5 minutes to handle 100,000 iterations means that you could handle
nearly 29 million iterations every 24 hours.

But why not just do the same thing in C# that you're doing in C++ if
performance is critical? That's the tradeoff. If you want performance,

open a socket, pack the data in a byte array and send it out. The serialization
stuff in .NET is necessarily slow because it requires reflection. I can't
really speak for the remoting stuff as I haven't had a need to use it. If
performance is an issue, serialization isn't your friend.

Pete

Nov 16 '05 #5
Another thought:
Try running this through the JetBrains and CLR profilers to see where the
CPU cycles and memory allocations look like.

Dave

<aj*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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(returningStrin g.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 ?

Nov 16 '05 #6
Here is the C++ code - It uses the Ace Toolkit for the Network layer
socket encapsulatation.

Client
----------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////
// BINARY TEST C++
//////////////////////////////////////////////////////////////////////////////////
int
binaryStructTest()
{
struct MsgStruct
{
int msgType_;
int seqNum_;
char symbol_[5];
int quoteId_;
int responseLevel_;
int eqiRole_;
float bidPrice_;
float offerPrice_;
int bidSize_;
int offerSize_;
float liquidityBidPrice_;
float liquidityOfferPrice_;
int liquidityBidSize_;
int liquidityOfferSize_;
char text_[512];
int checkSum_;

MsgStruct()
{
memset(this, NULL, sizeof(MsgStruct));
msgType_ = 92;
seqNum_ = 0;
strcpy(symbol_, "IBM");
bidPrice_ = 100.12; offerPrice_ = 101.21;
}
};
ACE_SOCK_Stream clientStream;
ACE_INET_Addr remoteAddr(remotePort, "machine1");
ACE_SOCK_Connector connector;

if (connector.connect(clientStream, remoteAddr) == -1)
{
cout << "Failed to connect" << endl;
exit(-1);
}

for (int i=0; i<N; ++i)
{
MsgStruct msg;
msg.msgType_ = 101;
msg.bidPrice_ = 123.32;
msg.bidSize_ = 100;
msg.eqiRole_ = 1;
msg.liquidityBidPrice_ = 122.12;
msg.liquidityOfferPrice_ = 192.32;
msg.offerPrice_ = 154.25;
msg.quoteId_ = i;
msg.responseLevel_ = 10;
strcpy(msg.symbol_, "IBM");
int n=0;
if ((n = clientStream.send(&msg, sizeof(msg))) == -1)
{
cout << "Failed to send " << endl;
exit(-1);
}

// wait for response now
int response;
clientStream.recv(&response, sizeof(response));

}

clientStream.close();

return 0;
}

Server
----------------------------------------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////////////
// BINARY TEST
///////////////////////////////////////////////////////////////////////////////////////
int binaryStructTest()
{
struct MsgStruct
{
int msgType_;
int seqNum_;
char symbol_[5];
int quoteId_;
int responseLevel_;
int eqiRole_;
float bidPrice_;
float offerPrice_;
int bidSize_;
int offerSize_;
float liquidityBidPrice_;
float liquidityOfferPrice_;
int liquidityBidSize_;
int liquidityOfferSize_;
char text_[512];
int checkSum_;
};

ACE_INET_Addr serverAddr(5666), clientAddr;
ACE_SOCK_Acceptor peerAcceptor(serverAddr);

if (peerAcceptor.get_local_addr(serverAddr) == -1)
{
cout << "Failed in get_local_addr()" << endl;
exit(-1);
}

ACE_SOCK_Stream newStream;
if (peerAcceptor.accept(newStream, &clientAddr) == -1)
{
cout << "Failed in accept()" << endl;
}

time_t t;
int numMsgs = 0;
while (true)
{
MsgStruct msg;
int bytes = newStream.recv_n(&msg, sizeof(msg));
++numMsgs;
if (bytes == 0)
break;

// send back response
int response = msg.seqNum_;
newStream.send(&response, sizeof(response));

if (numMsgs == 1)
t = time(NULL);
else if (numMsgs >= N)
{
break;
}
}
time_t t2 = time(NULL);
cout << "Msgs received: " << numMsgs << endl;
cout << "TIME: " << t2 - t << endl;
return 0;
}


So the code as you can see is straight forward . And my belief is that
adding to the size of the class object (more fields/attributes) will
furthermore add extra overhead to the .NET serialization process via
reflection - making the difference even more so greater.

Nov 16 '05 #7
I see a big difference in memory allocation. For both client and server,
you declare the MsgStruct inside the loop, but in C++ that causes one
allocation (during the first iteration). You are repeatedly using the same
memory on the stack instead of allocating memory from the heap for each
iteration. In the .NET code, you're creating a new object every iteration,
which has to allocate memory from the heap. The C++ is reusing the same
memory over and over, but the .NET code has to allocate (and later clean up)
memory for each iteration.

I suggest performing a new/delete every iteration in the C++ code for the
sake of symmetry. Alternatively, you could change the .NET code to reuse
the same object every time.

Dave

<aj*******@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Here is the C++ code - It uses the Ace Toolkit for the Network layer
socket encapsulatation.

Client
-------------------------------------------------------------------------- -- ////////////////////////////////////////////////////////////////////////////
///// // BINARY TEST C++
////////////////////////////////////////////////////////////////////////////
////// int
binaryStructTest()
{
struct MsgStruct
{
int msgType_;
int seqNum_;
char symbol_[5];
int quoteId_;
int responseLevel_;
int eqiRole_;
float bidPrice_;
float offerPrice_;
int bidSize_;
int offerSize_;
float liquidityBidPrice_;
float liquidityOfferPrice_;
int liquidityBidSize_;
int liquidityOfferSize_;
char text_[512];
int checkSum_;

MsgStruct()
{
memset(this, NULL, sizeof(MsgStruct));
msgType_ = 92;
seqNum_ = 0;
strcpy(symbol_, "IBM");
bidPrice_ = 100.12; offerPrice_ = 101.21;
}
};
ACE_SOCK_Stream clientStream;
ACE_INET_Addr remoteAddr(remotePort, "machine1");
ACE_SOCK_Connector connector;

if (connector.connect(clientStream, remoteAddr) == -1)
{
cout << "Failed to connect" << endl;
exit(-1);
}

for (int i=0; i<N; ++i)
{
MsgStruct msg;
msg.msgType_ = 101;
msg.bidPrice_ = 123.32;
msg.bidSize_ = 100;
msg.eqiRole_ = 1;
msg.liquidityBidPrice_ = 122.12;
msg.liquidityOfferPrice_ = 192.32;
msg.offerPrice_ = 154.25;
msg.quoteId_ = i;
msg.responseLevel_ = 10;
strcpy(msg.symbol_, "IBM");
int n=0;
if ((n = clientStream.send(&msg, sizeof(msg))) == -1)
{
cout << "Failed to send " << endl;
exit(-1);
}

// wait for response now
int response;
clientStream.recv(&response, sizeof(response));

}

clientStream.close();

return 0;
}

Server
-------------------------------------------------------------------------- -------------------------------- ////////////////////////////////////////////////////////////////////////////
///////// // BINARY TEST
////////////////////////////////////////////////////////////////////////////
/////////// int binaryStructTest()
{
struct MsgStruct
{
int msgType_;
int seqNum_;
char symbol_[5];
int quoteId_;
int responseLevel_;
int eqiRole_;
float bidPrice_;
float offerPrice_;
int bidSize_;
int offerSize_;
float liquidityBidPrice_;
float liquidityOfferPrice_;
int liquidityBidSize_;
int liquidityOfferSize_;
char text_[512];
int checkSum_;
};

ACE_INET_Addr serverAddr(5666), clientAddr;
ACE_SOCK_Acceptor peerAcceptor(serverAddr);

if (peerAcceptor.get_local_addr(serverAddr) == -1)
{
cout << "Failed in get_local_addr()" << endl;
exit(-1);
}

ACE_SOCK_Stream newStream;
if (peerAcceptor.accept(newStream, &clientAddr) == -1)
{
cout << "Failed in accept()" << endl;
}

time_t t;
int numMsgs = 0;
while (true)
{
MsgStruct msg;
int bytes = newStream.recv_n(&msg, sizeof(msg));
++numMsgs;
if (bytes == 0)
break;

// send back response
int response = msg.seqNum_;
newStream.send(&response, sizeof(response));

if (numMsgs == 1)
t = time(NULL);
else if (numMsgs >= N)
{
break;
}
}
time_t t2 = time(NULL);
cout << "Msgs received: " << numMsgs << endl;
cout << "TIME: " << t2 - t << endl;
return 0;
}


So the code as you can see is straight forward . And my belief is that
adding to the size of the class object (more fields/attributes) will
furthermore add extra overhead to the .NET serialization process via
reflection - making the difference even more so greater.

Nov 16 '05 #8
In C++, that MsgStruct object as you said is allocated on the stack,
BUT it happens for every iteration. If it were allocated as a static
variable, then it would only be allocated once. But in this case it is
being allocated/dellocated on the stack (not the same memory location).
Just to go along with your idea, though - I changed the allocation
inside the loop to be on the heap via explicit "new MsgStruct()" and
"delete msg" inside the loop, to see how much extra penalty would be
incurred in the C++ version. And the result is very minimal :
For 500,000 iterations it took 45 seconds now (43 seconds before), but
still way better than the 1459 seconds for the .NET version.

Perhaps there is a way to optimize the .NET version via the
compiler.... Otherwise, the penalty of an interpreted environment,
reflection overhead for serialization, and overhead of garbage
collection is just too much, and cannot be recommended for real-time
systems.

Nov 16 '05 #9
Another interesting test. I added Java Serialization into the mix,
basically I ported the C# code to Java - same logic, but the results
are much in better, in favor of Java.

The Java Serialization results:
For 50,000 Iterations: 19 seconds
For 100,000 Iterations: 46 seconds
For 500,000 Iterations: 250 seconds

That averages to about 6x slower than binary C++ version, but __MUCH__
better than the .NET version which averaged about 35x slower.

Why is .NET's serialization/processing so much worse than Java's ?
Here is the Java Code:
Server
-------------------------------------------------------------------------------------------
class tcpServer
{
public static void main(String[] args)
{
tcpServer server = new tcpServer();
server.run(100000);
}

void run(int N)
{
try
{
ServerSocket listener = new ServerSocket(9991);
Socket socket = listener.accept();
DataInputStream dis = new
DataInputStream(socket.getInputStream());
DataOutputStream dos = new
DataOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(dis);
ObjectOutputStream oos = new ObjectOutputStream(dos);

int i = 0;
while (i < N)
{
++i;
Msg obj = (Msg)ois.readObject();

// reply
Integer ii = new Integer(i);
oos.writeObject(ii);

}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

Client
--------------------------------------------------------------------------------------------
class tcpClient
{
public static void main(String[] args)
{
tcpClient client = new tcpClient();
client.run(100000);
}

void run(int N)
{
try
{

InetAddress ia = InetAddress.getByName("machine1");
Socket socket = new Socket(ia, 9991);
ObjectOutputStream oos = new
ObjectOutputStream(socket.getOutputStream());
DataInputStream dis = new
DataInputStream(socket.getInputStream());
ObjectInputStream ois = new ObjectInputStream(dis);

int i = 0;
Calendar beginTime = null;
while (i < N)
{
if (i == 0)
{
beginTime = Calendar.getInstance();
}

++i;

Msg object = new Msg();
object.msgType_ = 101;
object.seqNum_ = i;
object.symbol_ = "IBM";
object.quoteId_ = i;
object.responseLevel_ = 1;
object.eqiRole_ = 1;
object.bidPrice_ = 100.21f;
object.offerPrice_ = 102.31f;
object.bidSize_ = 100; object.offerSize_ = 200;
oos.writeObject(object);

// read ack
Integer ii = (Integer)ois.readObject();
}
Calendar endTime = Calendar.getInstance();
System.out.println("Time: " +
(endTime.getTimeInMillis() - beginTime.getTimeInMillis()) );

}
catch (Exception e)
{
e.printStackTrace();
}
}

}
Message Object
-------------------------------------------------------------------------
public class Msg implements Serializable
{
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 char[] text_ = new char[512];
public int checkSum_;

Nov 16 '05 #10
Another interesting test. I added Java Serialization into the mix,
basically I ported the C# code to Java - same logic, but the results
are much in better, in favor of Java.

The Java Serialization results:
For 50,000 Iterations: 19 seconds
For 100,000 Iterations: 46 seconds
For 500,000 Iterations: 250 seconds

That averages to about 6x slower than binary C++ version, but __MUCH__
better than the .NET version which averaged about 35x slower.

Why is .NET's serialization/processing so much worse than Java's ?
Here is the Java Code:
Server
-------------------------------------------------------------------------------------------
class tcpServer
{
public static void main(String[] args)
{
tcpServer server = new tcpServer();
server.run(100000);
}

void run(int N)
{
try
{
ServerSocket listener = new ServerSocket(9991);
Socket socket = listener.accept();
DataInputStream dis = new
DataInputStream(socket.getInputStream());
DataOutputStream dos = new
DataOutputStream(socket.getOutputStream());
ObjectInputStream ois = new ObjectInputStream(dis);
ObjectOutputStream oos = new ObjectOutputStream(dos);

int i = 0;
while (i < N)
{
++i;
Msg obj = (Msg)ois.readObject();

// reply
Integer ii = new Integer(i);
oos.writeObject(ii);

}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

Client
--------------------------------------------------------------------------------------------
class tcpClient
{
public static void main(String[] args)
{
tcpClient client = new tcpClient();
client.run(100000);
}

void run(int N)
{
try
{

InetAddress ia = InetAddress.getByName("machine1");
Socket socket = new Socket(ia, 9991);
ObjectOutputStream oos = new
ObjectOutputStream(socket.getOutputStream());
DataInputStream dis = new
DataInputStream(socket.getInputStream());
ObjectInputStream ois = new ObjectInputStream(dis);

int i = 0;
Calendar beginTime = null;
while (i < N)
{
if (i == 0)
{
beginTime = Calendar.getInstance();
}

++i;

Msg object = new Msg();
object.msgType_ = 101;
object.seqNum_ = i;
object.symbol_ = "IBM";
object.quoteId_ = i;
object.responseLevel_ = 1;
object.eqiRole_ = 1;
object.bidPrice_ = 100.21f;
object.offerPrice_ = 102.31f;
object.bidSize_ = 100; object.offerSize_ = 200;
oos.writeObject(object);

// read ack
Integer ii = (Integer)ois.readObject();
}
Calendar endTime = Calendar.getInstance();
System.out.println("Time: " +
(endTime.getTimeInMillis() - beginTime.getTimeInMillis()) );

}
catch (Exception e)
{
e.printStackTrace();
}
}

}
Message Object
-------------------------------------------------------------------------
public class Msg implements Serializable
{
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 char[] text_ = new char[512];
public int checkSum_;

Nov 16 '05 #11

<aj*******@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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(returningStrin g.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.


Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mountain Bikn' Guy | last post by:
We have an app that computes a lot of numeric data. We would like to save to disk about 1-2 gigabytes of computed data that includes ints, doubles, strings and some complex objects that contain...
3
by: Sunny | last post by:
Hi all, I'm creating client/server app in C# (VS. 2003). So, I need the client to call the server with some auth info (user and pass). If the auth is OK, server will do some work and will prepare...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Steve | last post by:
I've been following a couple remoting tutorials on the web, they are all pretty much the same. I've got my different applications built(client, server and remote object (dll)) The client is...
13
by: Ron L | last post by:
I am working on an application that is a front-end for a SQL database. While it is not an immediate requirement, the application will probably be required to be able to connect via the internet at...
1
by: Remy De Almeida | last post by:
Hi, I have a method on RemoteServer that returns a ArrayList which contains objects. The Remote server has been throwing this error whihc i am not been able to understand .. please can someone...
2
by: TonyJ | last post by:
Hello! I read on a page in the NET this text "What makes Remoting slow is not so much the communication protocol but the serialization." Can somebody explain if I use remoting for sending...
1
by: Dan Holmes | last post by:
I traced this all the to the domain boundary. I also but a breakpoint in the server. That was never hit. It happens after it leaves the client but before it calls into the "add" method in the...
1
by: sandeepbhutani304 | last post by:
have 2 projects communicating each other with .NET remoting. But when I am trying to call these functions I am getting the error: The input stream is not a valid binary format. The starting...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.