Brief introduction
This blog mainly compares the serialization and deserialization performance of Xml, Json, Hessian, and Protocol Buffers, leaving aside the basic concepts of Xml and Json. Hessian: Hessian is a lightweight remoting onhttp tool that provides RMI functionality using the binary RPC protocol and built-in serialization capabilities. Protocol Buffers: A format for data exchange from google, which is language-independent, and because it is a binary format, it is much faster than using xml for data exchange, and can be used for data communication between distributed applications or data exchange in heterogeneous environments. As an efficient and compatible binary data transmission format, it can be used in many fields such as network transmission, configuration files, data storage, etc. Google provides Java, C++, Python implementations, and now there are implementations of languages such as C# on the Internet.
Serialization and deserialization
XML: Use the XmlSerializer that comes with .Net. Json: It uses ServiceStack.Text, which is more performant than Newtonsoft.Json, but the fastest should be fastJSON.net. Hessian: Using the HessianCSharp library, downloaded from nuget. Protocol Buffers: Using protobuf-net, downloaded from nuget. The following are the entities used in the test.
The i7HQ 2.6HZ processor used in the test machine. Here are the results of the test Serialization
deserialization
Byte length after serialization
Let's talk about serialization first, here it is tested with 100 times, 1000 times, 10000 times and 100000 times respectively, the ordinate is the completion time, the unit is milliseconds, you can see that when testing within 10000 times, the time consumption of 4 kinds of serialization is very small, all within 200 milliseconds, after 10000 times, to 100000 times, they all begin to grow, the worst is Xml, the best is Protocol Buffers, but when it is within 10000 times, Hessian is better than Protocol Buffers. There is not much difference within 10,000 deserializations, but at 10,000 times, we can already see that Hessian is more time-consuming, and at 100,000 times, Hessian directly exceeds Xml, which makes me always think that there is a problem with my code, and the best performance is still Protocol Buffers. The serialized byte length is understandably the longest in Xml, because the file contains a lot of ending tags (),</Name> and Protocol Buffers is still the best.
Based on the graph above, we can almost quickly conclude that Protocol Buffers is the best, but I think we still need to evaluate it comprehensively from the following aspects: 1. Readability: Xml and Json are both text after serialization, and the readability is very good, for example, if there is a mistake in the middle, we can easily view the exchanged data, and even simulate the data to test; Both Hessian and Protocol Buffers are binary, and the content is unreadable after serialization, which will have a certain impact on the troubleshooting of the system. 2. Versatility: XML and JSON are already old data exchange formats, and these are the two formats for data exchange between general systems. Both Hessian and Protocol Buffers are relatively unpopular and are used less. 3. Convenience: Hessian is actually a complete RPC framework, define the interface on the server side and implement the interface, copy the interface to the client After a small amount of coding, we can call the server side like calling a local method, which is not available in the other three tools, and the performance is not bad.
|