.NET Core protobuf-net、MessagePack、Json.NET序列化/反序列化性能测试

时间:2022-10-15 08:59:08

测试代码Zonciu/SerializationTest.cs
源自neuecc/ZeroFormatterBenchmark.cs

NuGet包及其版本

mgravell/protobuf-net 2.3.2(非官方实现,官方无.NET实现)
msgpack/msgpack-cli 1.0.0-beta2(官方实现)
neuecc/MessagePack 1.7.2(非官方实现)
JamesNK/Newtonsoft.Json 10.0.3

兼容性

protobuf-net

  • 不支持无Contract Attribute的模型
  • 自带Attribute
  • 支持System.Runtime.Serialization Attribute

MsgPack-Cli

  • 支持无Contract Attribute的模型
  • 自带Attribute
  • 支持System.Runtime.Serialization Attribute

MessagePack

  • 支持无Contract Attribute的模型
  • 自带Attribute
  • 支持System.Runtime.Serialization Attribute

Newtonsoft.Json

  • 支持无Contract Attribute的模型
  • 自带Attribute
  • 支持System.Runtime.Serialization Attribute

测试结果

mgravell/protobuf-net
      Serialize   4.195 ms
    Deserialize   10.0423 ms
    ReSerialize   4.1817 ms
 Size of Binary 36.00 B
mgravell/protobuf-net
      Serialize   4.172 ms
    Deserialize   10.0139 ms
    ReSerialize   4.1776 ms
 Size of Binary 36.00 B
mgravell/protobuf-net
      Serialize   4.4121 ms
    Deserialize   10.2684 ms
    ReSerialize   4.2439 ms
 Size of Binary 36.00 B

Official MsgPack-Cli
      Serialize   6.9675 ms
    Deserialize   14.4727 ms
    ReSerialize   6.7491 ms
 Size of Binary 36.00 B
Official MsgPack-Cli
      Serialize   6.7087 ms
    Deserialize   14.4588 ms
    ReSerialize   6.9524 ms
 Size of Binary 36.00 B
Official MsgPack-Cli
      Serialize   6.7676 ms
    Deserialize   14.5932 ms
    ReSerialize   6.9552 ms
 Size of Binary 36.00 B

neuecc/MessagePack-CSharp
      Serialize   3.703 ms
    Deserialize   4.3366 ms
    ReSerialize   3.6741 ms
 Size of Binary 32.00 B
neuecc/MessagePack-CSharp
      Serialize   3.7056 ms
    Deserialize   4.6376 ms
    ReSerialize   3.6672 ms
 Size of Binary 32.00 B
neuecc/MessagePack-CSharp
      Serialize   3.6871 ms
    Deserialize   4.2972 ms
    ReSerialize   3.6756 ms
 Size of Binary 32.00 B

Newtonsoft.Json
      Serialize   17.5037 ms
    Deserialize   33.8181 ms
    ReSerialize   21.2306 ms
 Size of Binary 90.00 B
Newtonsoft.Json
      Serialize   18.4885 ms
    Deserialize   35.5003 ms
    ReSerialize   17.4223 ms
 Size of Binary 90.00 B
Newtonsoft.Json
      Serialize   18.1058 ms
    Deserialize   32.0834 ms
    ReSerialize   23.5259 ms
 Size of Binary 90.00 B

mgravell/protobuf-net
      Serialize   6546.5884 ms
    Deserialize   11488.7313 ms
    ReSerialize   6709.2795 ms
 Size of Binary 37.11 KB
mgravell/protobuf-net
      Serialize   6564.3259 ms
    Deserialize   11431.9974 ms
    ReSerialize   6679.0116 ms
 Size of Binary 37.11 KB
mgravell/protobuf-net
      Serialize   6561.8096 ms
    Deserialize   11476.1318 ms
    ReSerialize   6668.9106 ms
 Size of Binary 37.11 KB

Official MsgPack-Cli
      Serialize   5617.6389 ms
    Deserialize   14581.9512 ms
    ReSerialize   5578.6621 ms
 Size of Binary 38.82 KB
Official MsgPack-Cli
      Serialize   5590.4476 ms
    Deserialize   14763.014 ms
    ReSerialize   5533.6327 ms
 Size of Binary 38.82 KB
Official MsgPack-Cli
      Serialize   5607.3836 ms
    Deserialize   14615.2356 ms
    ReSerialize   5585.7651 ms
 Size of Binary 38.82 KB

neuecc/MessagePack-CSharp
      Serialize   3065.3312 ms
    Deserialize   4371.5859 ms
    ReSerialize   3062.9087 ms
 Size of Binary 32.83 KB
neuecc/MessagePack-CSharp
      Serialize   3116.9423 ms
    Deserialize   4392.1279 ms
    ReSerialize   3061.0422 ms
 Size of Binary 32.83 KB
neuecc/MessagePack-CSharp
      Serialize   3080.5855 ms
    Deserialize   4385.2026 ms
    ReSerialize   3054.3972 ms
 Size of Binary 32.83 KB

Newtonsoft.Json
      Serialize   13303.1545 ms
    Deserialize   24219.7497 ms
    ReSerialize   13346.126 ms
 Size of Binary 86.81 KB
Newtonsoft.Json
      Serialize   13362.7296 ms
    Deserialize   24238.1881 ms
    ReSerialize   13332.8785 ms
 Size of Binary 86.81 KB
Newtonsoft.Json
      Serialize   13399.7856 ms
    Deserialize   24197.9922 ms
    ReSerialize   13325.7399 ms
 Size of Binary 86.81 KB

neuecc/MessagePack-CSharp without Attribute
      Serialize   3.9342 ms
    Deserialize   4.9817 ms
    ReSerialize   3.9434 ms
 Size of Binary 65.00 B
neuecc/MessagePack-CSharp without Attribute
      Serialize   3.9854 ms
    Deserialize   4.9978 ms
    ReSerialize   3.9498 ms
 Size of Binary 65.00 B
neuecc/MessagePack-CSharp without Attribute
      Serialize   3.9393 ms
    Deserialize   5.1384 ms
    ReSerialize   3.9293 ms
 Size of Binary 65.00 B

neuecc/MessagePack-CSharp without Attribute
      Serialize   3968.4564 ms
    Deserialize   4912.953 ms
    ReSerialize   4082.8229 ms
 Size of Binary 65.06 KB
neuecc/MessagePack-CSharp without Attribute
      Serialize   3967.5422 ms
    Deserialize   4912.2183 ms
    ReSerialize   4059.2614 ms
 Size of Binary 65.06 KB
neuecc/MessagePack-CSharp without Attribute
      Serialize   3952.6747 ms
    Deserialize   4974.3738 ms
    ReSerialize   4066.7528 ms
 Size of Binary 65.06 KB

单对象序列化、反序列化、再序列化时间
.NET Core protobuf-net、MessagePack、Json.NET序列化/反序列化性能测试

多对象数组序列化、反序列化、再序列化时间
.NET Core protobuf-net、MessagePack、Json.NET序列化/反序列化性能测试

单对象序列化后数据大小
.NET Core protobuf-net、MessagePack、Json.NET序列化/反序列化性能测试

多对象数组序列化后数据大小
.NET Core protobuf-net、MessagePack、Json.NET序列化/反序列化性能测试

结论

neuecc/MessagePack表现最优,官方MsgPack-Cli表现比较差,Protobuf不支持无Contract Attribute模型,意味着需要侵入模型。

使用MessagePack,模型添加Contract Attribute并添加int型Order,比不添加Attribute或添加string型key快一点点,序列化后数据量明显减少,原因是不使用Attribute或使用string型Attribute key时,是以成员名称或key string作为索引,反之使用int型索引。