473,756 Members | 5,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.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 liquidityBidPri ce_;
public float liquidityOfferP rice_;
public int liquidityBidSiz e_;
public int liquidityOfferS ize_;
public int checkSum_;
}

The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{

using System;
using System.Net;
using System.Net.Sock ets;
using System.Runtime. Serialization.F ormatters.Binar y;
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(por t);
tcpListener.Sta rt();
Socket soTcp = tcpListener.Acc eptSocket();
Console.WriteLi ne("SampleClien t is connected through TCP.");
NetworkStream stream = new NetworkStream(s oTcp,
FileAccess.Read Write, true);
BinaryReader bReader = new BinaryReader(st ream);

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)b F.Deserialize(s tream);
String returningString = Convert.ToStrin g(99);
Byte[] returningByte =
System.Text.Enc oding.ASCII.Get Bytes(returning String.ToCharAr ray());

//Returning a confirmation back to the client.
soTcp.Send(retu rningByte, returningByte.L ength, 0);
}
DateTime endTime = System.DateTime .Now;
Console.WriteLi ne(endTime - beginTime);
}
}
}

The Client Process
---------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sock ets;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;
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("mach ine1", port);
NetworkStream tcpStream = tcpClient.GetSt ream();
BinaryWriter bWriter = new BinaryWriter(tc pStream);
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.liquidityBidP rice_ = 122.12f;
m.liquidityOffe rPrice_ = 192.32f;
m.offerPrice_ = 154.25f;
m.quoteId_ = i;
m.responseLevel _ = 10;
m.symbol_ = "IBM";
bF.Serialize(tc pStream, 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
11 11960
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(1000 00);
}

void run(int N)
{
try
{
ServerSocket listener = new ServerSocket(99 91);
Socket socket = listener.accept ();
DataInputStream dis = new
DataInputStream (socket.getInpu tStream());
DataOutputStrea m dos = new
DataOutputStrea m(socket.getOut putStream());
ObjectInputStre am ois = new ObjectInputStre am(dis);
ObjectOutputStr eam oos = new ObjectOutputStr eam(dos);

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

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

}
}
catch (Exception e)
{
e.printStackTra ce();
}
}

}

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

void run(int N)
{
try
{

InetAddress ia = InetAddress.get ByName("machine 1");
Socket socket = new Socket(ia, 9991);
ObjectOutputStr eam oos = new
ObjectOutputStr eam(socket.getO utputStream());
DataInputStream dis = new
DataInputStream (socket.getInpu tStream());
ObjectInputStre am ois = new ObjectInputStre am(dis);

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

++i;

Msg object = new Msg();
object.msgType_ = 101;
object.seqNum_ = i;
object.symbol_ = "IBM";
object.quoteId_ = i;
object.response Level_ = 1;
object.eqiRole_ = 1;
object.bidPrice _ = 100.21f;
object.offerPri ce_ = 102.31f;
object.bidSize_ = 100; object.offerSiz e_ = 200;
oos.writeObject (object);

// read ack
Integer ii = (Integer)ois.re adObject();
}
Calendar endTime = Calendar.getIns tance();
System.out.prin tln("Time: " +
(endTime.getTim eInMillis() - beginTime.getTi meInMillis()) );

}
catch (Exception e)
{
e.printStackTra ce();
}
}

}
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 liquidityBidPri ce_;
public float liquidityOfferP rice_;
public int liquidityBidSiz e_;
public int liquidityOfferS ize_;
public char[] text_ = new char[512];
public int checkSum_;

Nov 16 '05 #11

<aj*******@yaho o.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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 liquidityBidPri ce_;
public float liquidityOfferP rice_;
public int liquidityBidSiz e_;
public int liquidityOfferS ize_;
public int checkSum_;
}

The Server Process:
------------------------------------------------------
using System;
using Messages;
namespace tcpServer
{

using System;
using System.Net;
using System.Net.Sock ets;
using System.Runtime. Serialization.F ormatters.Binar y;
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(por t);
tcpListener.Sta rt();
Socket soTcp = tcpListener.Acc eptSocket();
Console.WriteLi ne("SampleClien t is connected through TCP.");
NetworkStream stream = new NetworkStream(s oTcp,
FileAccess.Read Write, true);
BinaryReader bReader = new BinaryReader(st ream);

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)b F.Deserialize(s tream);
String returningString = Convert.ToStrin g(99);
Byte[] returningByte =
System.Text.Enc oding.ASCII.Get Bytes(returning String.ToCharAr ray());

//Returning a confirmation back to the client.
soTcp.Send(retu rningByte, returningByte.L ength, 0);
}
DateTime endTime = System.DateTime .Now;
Console.WriteLi ne(endTime - beginTime);
}
}
}

The Client Process
---------------------------------------------------------------------
using System;
using System.Net;
using System.Net.Sock ets;
using System.IO;
using System.Runtime. Serialization.F ormatters.Binar y;
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("mach ine1", port);
NetworkStream tcpStream = tcpClient.GetSt ream();
BinaryWriter bWriter = new BinaryWriter(tc pStream);
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.liquidityBidP rice_ = 122.12f;
m.liquidityOffe rPrice_ = 192.32f;
m.offerPrice_ = 154.25f;
m.quoteId_ = i;
m.responseLevel _ = 10;
m.symbol_ = "IBM";
bF.Serialize(tc pStream, 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(objec t o){
byte[] buffer;
using(MemoryStr eam ms = new MemoryStream())
{
BinaryFormatter b = new BinaryFormatter ();
b.Serialize(ms, o);
if (ms.Length > int.MaxValue)
throw new ArgumentExcepti on("Serialized object is larger than
can fit into byte array");
buffer = ms.GetBuffer();
}
return buffer;
}

public object DeSerialize(byt e[] bytes){
object o;
using(MemoryStr eam ms = new MemoryStream(by tes))
{
BinaryFormatter b = new BinaryFormatter ();
o = b.Deserialize(m s);
}
return o;
}
}

Usage:
//Client....
....
Serializer se = new Serializer();
while(...)
...
sb = se.Serialize(m) ;
bWriter.Write(s b.Length); // Write size in bytes of serialized
array
bWriter.Write(s b);

}

Serializer se = new Serializer();
while (i < N)
{
++i;
int length = bReader.ReadInt 32();
byte[] data = bReader.ReadByt es(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
4254
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 hashtables. We would like to read this data back into the app and reuse those values and state to compute more new data. Up to this point we just write out comma separated ASCII values for everything. And we read/parse the ascii back in. The ASCII...
3
2274
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 a zip-file and some other text and numeric data and will notify the client that everything is ready. And the client will fetch the data. So if it was all console/desktop application, I know what to do (in
6
2716
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 rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
3
2876
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 able to get a reference to the remote object and that works fine. When I try to make a call to a remote object's method I get an exception: System.Runtime.Serialization.SerializationException: Cannot find the assembly ProcessTest,...
13
1923
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 a later date, so we are implementing the data connections via remoting. The remoting is implemented via the HTTP channel, with the binary formatter. We have noticed, however, that the remoting calls seemed to be much slower that the OLEDB calls,...
1
4702
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 let me know what could be happeining. Error The internal array cannot expand to greater than Int32.MaxValue elements.: Server stack trace:
2
1285
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 message will it be slow if I use remoting then? //Tony
1
1365
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 server. I don't know how to debug this any further. exact error message and code follows this is what fails. it fails on the add. TestContactInfo tci = new TestContactInfo(); tci.Address1 = "address1";
1
5400
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 contents (in bytes) are: 53-79-73-74-65-6D-2E-54-79-70-65-4C-6F-61-64-45-78 ...
0
9455
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8709
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7242
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2665
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.