Serializers in .NET. v.2

[v.2.9.0 2015-08-19]

I've decided to keep live the first version of this post. It is interesting how tests evolve and how results change. V.1.0.10 see here.

The v.2 was inspired by OniBait, thanks a lot! He added code for Avro and Bond serializers, added 99st% to measurements, made other improvements which significantly impacted results.

That’s the force and beauty of the open source.

Small improvements in the project code change results all the time. So I just mention the last improvement date.

The project code is on GitHub.

Any distributed system requires serializing to transfer data between systems and applications. The serializers used to be hidden in adapters and proxies, where developers did not deal with the serialization process explicitly. The WCF serialization is an example, when all we need to know is where to place the [Serializable] attributes. Contemporary tendencies bring serializers to the surface. In Windows .NET development, it might have started when James Newton-King created the Json.Net serializer and even Microsoft officially declared it the recommended serializer for .NET.

There are many kinds of serializers; they produce very compact data very fast. There are serializers for messaging, for data stores, for marshaling objects.

What is the best serializer in .NET?

Nope… this project is not about the best serializer. Here I show in several lines of code, how to use different .NET serializers. Want to serialize object and look for the sample code? You are on the right place, just copy-past this code in your project. The goal is to help developers with samples. Samples should be simple enough to copy-past code without effort. Also samples should provide effective code to be good enough for most messaging scenarios. I want to show serializer in the simplest way, but it is good to know that it would not hit your code performance. That is why I added some measurements, so you can do right decisions.

Please, do not take these measurements too seriously. I have some numbers, but this project is not the right place to get decisions about serializer performance. I did not spent time to get the best results. If you have the expertise, please, feel free to modify code to get numbers that are more reliable.

Note: I have not tested the serializers that require IDL for serialization: Thrift, , Cap'n Proto, FlatBuffers, Simple Binary Encoding. Those sophisticated beasts are not easy in work; they needed for something more special than straightforward serialization for messaging. These serializers are on my Todo list. ProtoBuf for .NET implementation was upgraded to use attributes instead of IDL, kudos to Marc Gravell. Single exception is the new Microsoft Bond (thank OniBait again for coding Bond part).

Installation

Most of serializers installed with NuGet package. Look to the “packages.config” file to get a name of the package. I have included comments in the code about it.

Tests

The test data created by Randomizer. It fills in fields of the Person object with randomly generated data. This object used for a single test cycle with all serializers, and then it is regenerated for the next cycle.

If you want to test serializers for different object size or for different primitive types, change the Person object.

The measured time is for the combined serialization and deserialization operations of the same object. When serializer called the first time, it runs the longest time. This longest time span is important and it is measured. It is the Max time. If we need only single serialization/deserialization step, this is the most significant value for us. If we repeat serialization / deserialization many times, the most significant values for us are Average time and Min time.

There are two average measurements:

  • The Avg-100% : all measured times are used in calculation. 
  • The Avg-90% : 10% slowest measurements ignored.

Some serializers serialize to strings, others – to the byte arrays. I used a string as a denominator. The base64 format is used to convert byte arrays to strings. I know, it is not fair, because in many cases we use only a byte array to serialize, not a string. UTF-8 also could be more compact format.

Test Results

Again, do not take test results too seriously. I have some numbers, but this project is not the right place to get conclusions about serializer performance. You have to take this code and run it on your specific data on your specific workflows.

Here is a test result for the 100 repetitions. This number of repetitions shows stable results.

There is no such thing as the “best serializer”. If you invest time in optimizing code, the loser will be winner. If you change the test data, the winner would not be winner anymore. So I will mark serializer as a “winner” if it shows significant leadership in numbers, not just 1-3%.

There are several winners in each category.

Compression

This category important if you need smallest data size (on wire, in store…). Look at the Size: Avg measurement. All winners in this category use proprietary unreadable formats.

Winners are:

  • Solar.Bois
  • Avro
  • Solar.Bois
  • MsgPack
  • MessageSharkSerializer
  • NetSerializer
  • Bond
  • ProtoBuf

Notes:

  • All Json serializers create strings with almost the same size.
  • You can see the output serialized strings. They written to the trace output, so use DebugViw to see them.
  • Many Json serializers do not work well with DateTime format out-of-box. Only NetSerializer and Json.Net take care of DateTime format without additional customization.

Speed on Single Run

This category important, if you only need make a single serialization/deserialization. Look at the Max measurement.

Winners are:

  • Microsoft NetDataContract (surprise)
  • Json.Net
  • Microsoft Binary

Notes:

  • AvroBond, and NetSerializer also show good results.
  • Jil shows the worst result but, again, it means nothing.
  • Serializers show biggest incoherence in this category.

Speed on the Large Scale

This category important, if you need many serialization/deserialization acts on the same objects. Look at the Min and Avg:-90% measurements.

Winners are:

  • NetJSON
  • Bond
  • MessageSharkSerializer
  • NetSerializer
  • Jil
  • Avro
  • ProtoBuf
  • Solar.Bois

Notes:

  • MsgPack and BondJson also show good results.

Speed on Several Cycles

Winners are:

  • Avro
  • Bond
  • NetSerializer
  • Json.Net
This category important, if you need several serialization/deserialization acts on the same objects. Look at the Avg:-100% measurement. It is a mix of slowest and fastest times. As you can see, MessageShark, Jil and ProtoBuf not in leaders anymore because of the suboptimal initialization. But Json.Net now is a leader because of good initialization time on our test data.

Notes:

  • The classic Json.Net serializer used in two Json.Net (Helper) and Json.Net (Stream) tests. Tests show the difference between using the streams and the serializer helper classes. Helper classes could seriously decrease performance. Therefore, streams are the good way to keep up to the fast speed without too much hassle.
  • The first call to serializer initializes the serializer, that is why it might take thousand times faster to the following calls.
  • For Microsoft Avro my simple serialization interface is patched. For some reason it is not possible to pass generic T type into it. Type was hardcoded. Sad smile Because of this hack, Avro makes tests faster.
  • Json and Binary formats bring not too much difference in compacting the serialized strings.
  • Serialization of the classes with explicit constructors usually has negative impact on the serialization speed. That’s why test class (Person) creation is implemented not by constructor but by a method.
  • Many serializers do not work well with Json DateTime format out-of-box. Only NetSerializer and Json.Net take care of DateTime format without special treatment.
  • Test prints the test results on the console. It also traces the errors and the serialized strings, which can be seen in DebugView for example.
  • The Apolyton.Json and HaveBoxJSON serializers failed in tests, that why we see zeroes in several measurements.
Print | posted on Wednesday, May 6, 2015 10:08 PM

Feedback

# re: Serializers in .NET. v.2

left by Dmitriy at 6/8/2015 5:19 PM Gravatar
It would be interesting to see the https://github.com/aumcode/nfx/blob/master/Source/NFX/Serialization/Slim/SlimSerializer.cs in the mix

# re: Serializers in .NET. v.2

left by Leonid Ganeline at 6/8/2015 7:47 PM Gravatar
Slim Serializer added :)

# re: Serializers in .NET. v.2

left by TJ at 6/10/2015 8:26 PM Gravatar
Can you add NetJSON to the list? https://github.com/rpgmaker/NetJSON

# re: Serializers in .NET. v.2

left by Leonid Ganeline at 6/10/2015 9:18 PM Gravatar
NetJSON added. It is a leader now in "Speed in the Large Scale".

# re: Serializers in .NET. v.2

left by bluefoxreg at 7/15/2015 8:10 AM Gravatar
There's also a NetSerializer2 based on NetSerializer, though it doesn't seem to be updated for a few years. However, it does have better support for more native serializable classes.

https://github.com/smalinin/netserializer2

# re: Serializers in .NET. v.2

left by Salar at 8/19/2015 1:01 AM Gravatar
Can you add BOIS to the list?
https://github.com/salarcode/Bois

# re: Serializers in .NET. v.2

left by Christian Henrik Reich at 9/6/2015 12:23 PM Gravatar
Cool, your are measuring HaveBoxJSON :-)

I think, I'll give it a service check, now that it fails in your tests.

# re: Serializers in .NET. v.2

left by Chris at 9/25/2015 2:45 PM Gravatar
I know the article states that you ran on small number counts, but have you tested any larger collections? Compared the performance and functionality of hundreds of thousands of objects that need to be serialized?

I recently ran into issues with the .NET binary serializer and the length of arrays. I ended up writing my own method as well as implementing ProtoBuf.

I'm going to use your article and perform some additional testing but wondered if you had done so already.

# re: Serializers in .NET. v.2

left by Chris.son at 1/31/2016 1:48 PM Gravatar
Looks like you are testing BASE64 encoding not sers speed

# re: Serializers in .NET. v.2

left by Thomas Fuchs at 4/18/2016 5:04 AM Gravatar
This could be an interesting competitor as well: https://github.com/Toxantron/CGbR#serialization
Post A Comment
Title:
Name:
Email:
Comment:
Verification: