MessagePack 和System.Text.Json 序列化和反序列化性能及对比分析(message in a bottle)快来看

随心笔谈2年前发布 编辑
165 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

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=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”;

}
}
}

© 版权声明

相关文章