C# 协议缓冲区与 JSON 或 BSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2000933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:32:55  来源:igfitidea点击:

Protocol Buffers versus JSON or BSON

c#jsoncomparisonprotocol-buffersbson

提问by Jeff Meatball Yang

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

有没有人有关于协议缓冲区与 BSON(二进制 JSON)或 JSON 的性能特征的任何信息?

  • 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.

这些看起来像是通过 HTTP 使用的很好的二进制协议。我只是想知道从长远来看对于 C# 环境来说哪个更好。

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

这是我在BSONProtocol Buffers上阅读的一些信息。

采纳答案by Michael Greene

Thriftis another Protocol Buffers-like alternative as well.

Thrift也是另一种类似于 Protocol Buffers 的替代方案。

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

Java 社区有关于这些技术的序列化/反序列化和线路大小的良好基准: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 Avrois trying to solve, and it beats both in terms of performance.

一般来说,JSON 的线路尺寸稍大,DeSer 稍差,但在普遍性和无需源 IDL 的情况下轻松解释它的能力方面胜出。最后一点是Apache Avro试图解决的问题,它在性能方面胜过两者。

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

微软发布了一个 C# NuGet 包Microsoft.Hadoop.Avro

回答by Hassan Syed

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.
  1. 非常小的消息大小 - 一方面是非常有效的可变大小整数表示。
  2. 非常快速的解码 - 它是一个二进制协议。
  3. protobuf 生成用于编码和解码消息的超高效 C++——提示:如果您将所有 var-integers 或静态大小的项目编码到其中,它将以确定的速度进行编码和解码。
  4. 它提供了一个非常丰富的数据模型——有效地编码非常复杂的数据结构。

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.

JSON 只是文本,需要对其进行解析。提示:将“十亿” int 编码到其中需要相当多的字符:Billion = 12 个字符(长标),二进制它适合 uint32_t 现在尝试编码 double 怎么样?那会更糟糕。

回答by James Newton-King

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

这篇文章比较了 .NET 中的序列化速度和大小,包括 JSON、BSON 和 XML。

alt text

替代文字

alt text

替代文字

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

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

回答by mythz

Here are some recent benchmarksshowing the performance of popular .NET Serializers.

以下是一些最近的基准测试,显示了流行的 .NET 序列化程序的性能。

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

燃烧的和尚基准测试显示序列化一个简单的POCO的性能,同时全面罗斯文基准测试显示,微软的罗斯文数据集的每个表序列化行的综合结果。

enter image description here

在此处输入图片说明

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

基本上,协议缓冲区 ( protobuf-net)比 .NET 中最快的基类库 Serializer (XML DataContractSerializer) 快7 倍左右。它也比竞争产品小,因为它也是2.2倍比微软最紧凑的序列化格式(JsonDataContractSerializer)小。

ServiceStack's Text serializers are the closest to matching the performance of the binary protobuf-net where its Json Serializeris only 2.58xslower than protobuf-net.

ServiceStack 的文本序列化器最接近二进制 protobuf-net 的性能,其Json 序列化器仅比 protobuf-net 慢2.58 倍