Protocol Buffers versus JSON or BSON

2019-01-29 16:33发布

问题:

Does anyone have any information on the performance characteristics of Protocol Buffers versus BSON (binary JSON) or versus JSON in general?

  • Wire size
  • Serialization speed
  • Deserialization speed

These seem like good binary protocols for use over HTTP. I'm just wondering which would be better in the long run for a C# environment.

Here's some info that I was reading on BSON and Protocol Buffers.

回答1:

Thrift is another Protocol Buffers-like alternative as well.

There are good benchmarks from the Java community on serialization/deserialization and wire size of these technologies: https://github.com/eishay/jvm-serializers/wiki

In general, JSON has slightly larger wire size and slightly worse DeSer, but wins in ubiquity and the ability to interpret it easily without the source IDL. The last point is something that Apache Avro is trying to solve, and it beats both in terms of performance.

Microsoft has released a C# NuGet package Microsoft.Hadoop.Avro.



回答2:

This post compares serialization speeds and sizes in .NET, including JSON, BSON and XML.

alt text http://james.newtonking.com/images/eb2.NETSerializationPerformanceCompariso_DB29/json356otherperformance.png

alt text http://james.newtonking.com/images/eb2.NETSerializationPerformanceCompariso_DB29/json356size.png

http://james.newtonking.com/archive/2010/01/01/net-serialization-performance-comparison.aspx



回答3:

Here are some recent benchmarks showing the performance of popular .NET Serializers.

The Burning Monks benchmarks show the performance of serializing a simple POCO whilst the comprehensive Northwind benchmarks show the combined results of serializing a row in every table of Microsoft's Northwind dataset.

Basically protocol buffers (protobuf-net) is around 7x quicker than the fastest Base class library Serializer in .NET (XML DataContractSerializer). Its also smaller than the competition as it is also 2.2x smaller than Microsofts most compact serialization format (JsonDataContractSerializer).

ServiceStack's Text serializers are the closest to matching the performance of the binary protobuf-net where its Json Serializer is only 2.58x slower than protobuf-net.



回答4:

protocol buffers is designed for the wire:

  1. very small message size - one aspect is very efficient variable sized integer representation.
  2. Very fast decoding - it is a binary protocol.
  3. protobuf generates super efficient C++ for encoding and decoding the messages -- hint: if you encode all var-integers or static sized items into it it will encode and decode at deterministic speed.
  4. It offers a VERY rich data model -- efficiently encoding very complex data structures.

JSON is just text and it needs to be parsed. hint: encoding a "billion" int into it would take quite a lot of characters: Billion = 12 char's (long scale), in binary it fits in a uint32_t Now what about trying to encode a double ? that would be FAR FAR worse.