本博客将测试MessagePack 和System.Text.Json 序列号 反序列化性能项目文件:
文章插图
Program.cs代码:
【MessagePack 和System.Text.Json 序列号 反序列化对比】
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的性能在序列号Byte[]的表现对比TextJson上不光是性能比TextJson的更快,内存占用也更小然后是反序列化对象 MessagePack对比TextJson 性能和内存占用都更强在使用MessagePack的前提上我配置了MessagePack的配置
经验总结扩展阅读
- 江门医保和社保有什么区别 待遇标准怎么样
- 社保和五险一金是一样吗 区别是什么
- 2023社保办理流程和所需资料一览
- 等离子好还是液晶好 等离子和液晶电视的区别
- 美的和tcl冰箱怎么样 哪个比较好呢
- 海信和tcl电视质量怎么样 哪个液晶电视品牌好呢
- 液晶电视屏幕哪种好 硬屏和软屏有何不同
- 水杨酸和面膜先敷哪个?
- g露和haba油怎么用?
- 十二星座如何理顺爱情和工作的关系