Comparing serializations, or why Flash Remoting is not as efficient as some would imply
[Updated: Added reference to which classes were being used. The DataContractJsonSerializer is part of the .net 3.5 release.]
There's been a few discussions recently about the cost of using JSON against using Flash Remoting. Its a fact of life to have to discuss those issues when you work with a lot of Adobe people, especially when you're the Microsoft guys.
One of the discussions is around the performance of JSON and Flash Remoting. There is the BlazeBench application that would imply AMF wins hands down in every configuration. BlazeDS is a Java server that Adobe is apparently open-sourcing, but me being an asp.net guy, I wanted to see for myself what all this meant.
You see, in terms of architecture, you will always have a service layer, be it in the shape of a REST api, SOAP endpoints or a flash remoting object that pulls DTOs for display by the application. The difference between each of those solutions is going to be in the serialization mechanism you use. So I wrote a small application that compares the time it takes to serialize objects using the following:
- Asp.net JSON using System.Web.Script.Serialization.JavaScriptSerializer
- WCF JSON using System.Runtime.Serialization.Json.DataContractJsonSerializer [part of Orcas]
- AMF FlashMX using FlashGateway.IO.AMFWriter
- .net binary using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
- Xml using System.Runtime.Serialization.DataContractSerializer
- Xml using System.Xml.Serialization.XmlSerializer
I've not done anything special to optimise any scenario and the object graph is the same. I serialize 1,000, 10,000 and 100,000 graphs with each of the serializers, everything is built first before running the test, everything is serialized to a MemoryStream.
This is what i get.
The big surprise for me was the binary serialization using BinaryFormatter, which is nearly as slow as the XmlSerializer.
More important for this study, the serialization cost of the WCF JSON serializer is only 300 milliseconds, for 100,000 objects.
With those results in hand, I will argue that there is no real performance gaps between WCF JSON, Flash AMF and XML DataContracts. Even with the asp.net AJAX extensions JSON serializer, the cost of serializing your graph is probably going to be a fraction of the cost of retrieving such an object graph from a database.
If anyone wants the source code, I'll be more than happy to publish it, provided my client allows it.