.NET Forum / .NET Framework / .NET SDK / March 2005
.NET Remoting/Serialization way too slow vs C++ binary/tcp
|
|
Thread rating:  |
ajou_king@yahoo.com - 15 Mar 2005 15:51 GMT 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 ?
Nicholas Paldino [.NET/C# MVP] - 15 Mar 2005 16:25 GMT 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.
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.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# [quoted text clipped - 167 lines] > hundreds of thousands of messages per day ? Is the tradeoff really > worth it ? What are people's thoughts ? Pete Davis - 15 Mar 2005 16:46 GMT > 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
Dave P. - 15 Mar 2005 17:02 GMT 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
> > If .NET Remoting / Serialization is so slow, why would anyone ever use > > it over C++ for performance critical applications that transmit [quoted text clipped - 13 lines] > > Pete ajou_king@yahoo.com - 15 Mar 2005 17:25 GMT 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.
Dave P. - 15 Mar 2005 22:09 GMT 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
> Here is the C++ code - It uses the Ace Toolkit for the Network layer > socket encapsulatation. > > Client > ---------------------------------------------------------------------------- //////////////////////////////////////////////////////////////////////////// /////
> // BINARY TEST C++ //////////////////////////////////////////////////////////////////////////// //////
> int > binaryStructTest() [quoted text clipped - 72 lines] > Server > -------------------------------------------------------------------------- --------------------------------
//////////////////////////////////////////////////////////////////////////// /////////
> // BINARY TEST //////////////////////////////////////////////////////////////////////////// ///////////
> int binaryStructTest() > { [quoted text clipped - 65 lines] > furthermore add extra overhead to the .NET serialization process via > reflection - making the difference even more so greater. ajou_king@yahoo.com - 15 Mar 2005 22:55 GMT 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.
Dave P. - 15 Mar 2005 17:05 GMT Another thought: Try running this through the JetBrains and CLR profilers to see where the CPU cycles and memory allocations look like.
Dave
> 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# [quoted text clipped - 167 lines] > hundreds of thousands of messages per day ? Is the tradeoff really > worth it ? What are people's thoughts ? ajou_king@yahoo.com - 16 Mar 2005 16:38 GMT 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_;
ajou_king@yahoo.com - 16 Mar 2005 17:01 GMT 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_;
Willy Denoyette [MVP] - 18 Mar 2005 00:00 GMT >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# [quoted text clipped - 167 lines] > 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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|