本博客将测试MessagePack 和System.Text.Json 序列号 反序列化性能项目文件:
![MessagePack 和System.Text.Json 序列号 反序列化对比](http://img.zhejianglong.com/231020/00093CD9-0.png)
文章插图
Program.cs代码:
using BenchmarkDotNet.Running;using Demo;var summary = BenchmarkRunner.Run<SerializeTest>();
SerializeTest.cs代码:using BenchmarkDotNet.Attributes;using MessagePack;using System.Text.Json;namespace Demo{[MemoryDiagnoser, RankColumn, MaxColumn,MinColumn]public class SerializeTest{public List<TestModule> TestDatas = new();public byte[] Pack;public byte[] Json;public SerializeTest(){for (int i = 0; i < 3000; i++){var d = new TestModule(Guid.NewGuid(), Guid.NewGuid().ToString("N") + i);d.i = i;TestDatas.Add(d);}Pack = MessagePackSerializer.Serialize(TestDatas, MessagePack.Resolvers.ContractlessStandardResolver.Options);Json = JsonSerializer.SerializeToUtf8Bytes(TestDatas);}[Benchmark]public byte[] GetMessagePackByte(){return MessagePackSerializer.Serialize(TestDatas, MessagePack.Resolvers.ContractlessStandardResolver.Options);}[Benchmark]public byte[] TextJsonByte(){return JsonSerializer.SerializeToUtf8Bytes(TestDatas);}[Benchmark]public List<TestModule> GetMessagePack(){return MessagePackSerializer.Deserialize<List<TestModule>>(Pack, MessagePack.Resolvers.ContractlessStandardResolver.Options);}[Benchmark]public List<TestModule>? TextJson(){return JsonSerializer.Deserialize<List<TestModule>>(Json);}public class TestModule{public TestModule(Guid id, string? value){Id = id;Value = https://www.huyubaike.com/biancheng/value;}public Guid Id { get; set; }public int i { get; set; }public string? Value { get; set; }public string MyProperty { get; set; } ="MyProperty";public string MyProperty1 { get; set; } = "MyProperty";public string MyProperty2 { get; set; } = "MyProperty";public string MyProperty3 { get; set; } = "MyProperty";public string MyProperty4 { get; set; } = "MyProperty";public string MyProperty5 { get; set; } = "MyProperty";public string MyProperty6 { get; set; } = "MyProperty";public string MyProperty7 { get; set; } = "MyProperty";public string MyProperty8 { get; set; } = "MyProperty";public string MyProperty9 { get; set; } = "MyProperty";public string MyProperty10 { get; set; } = "MyProperty";}}}
然后我们将使用基准测试开始我们的性能测试:![MessagePack 和System.Text.Json 序列号 反序列化对比](http://img.zhejianglong.com/231020/0009364952-1.png)
文章插图
然后测试结束:
![MessagePack 和System.Text.Json 序列号 反序列化对比](http://img.zhejianglong.com/231020/0009362F9-2.png)
文章插图
我们看到我们的MessagePack的性能在序列号Byte[]的表现对比TextJson上不光是性能比TextJson的更快,内存占用也更小然后是反序列化对象 MessagePack对比TextJson 性能和内存占用都更强在使用MessagePack的前提上我配置了MessagePack的配置
推荐阅读
- 华为nova8和华为mate30pro对比_华为nova8和华为mate30pro有什么区别
- r7 5800u和5800h的区别_r7 5800u和5800h有什么不同
- 兰蔻菁纯粉底液和兰蔻持妆粉底液哪个好_哪个更持久
- 双十一iQOOZ5和红米Note11哪款更值得入手?
- 迪奥720和840哪个好看_迪奥720和840试色对比
- 荣耀平板v7pro和小米平板5pro哪个好_哪款更值得入手
- 煮汤圆的方法和步骤(汤圆加什么一起煮好吃)
- Kotlin Mp3文件标签信息读取和写入
- 荣耀magic3和苹果12哪个好_拍照能力哪个好
- iqoo neo5对比k40哪个好_iqooneo5和红米k40哪个好