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

随心笔谈9个月前发布 admin
78 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”;

}
}
}

© 版权声明

相关文章