This blog will test MessagePack and System.Text.Json serialization and deserialization performance

This blog will test MessagePack and System.Text.Json serialization and deserialization performance
Project file:

Program.cs code:

using BenchmarkDotNet.Running;
using Demo;

var summary = BenchmarkRunner.Run<SerializeTest>();

SerializeTest.cs code:

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";

        }
    }
}

Then we'll start our performance testing using the benchmark:

Then the test ends:

We see that the performance of our MessagePack is not only faster than that of TextJson in the performance of serialized Byte[] compared with TextJson, but also has a smaller memory footprint. Then the performance and memory usage of the deserialized
object MessagePack are stronger than TextJson
. On the premise of using MessagePack, I configured the MessagePack configuration MessagePack.Resolvers.ContractlessStandardResolver.Options
. If you do not add MessagePack.Resolvers.ContractlessStandardResolver.Options , the performance may not be faster or better than Json. After the configuration is enabled, the model does not need to add features and has faster performance.
In scenarios that require faster performance, MessagePack is more suitable and the transmission volume is smaller, so it is highly recommended to use MessagePack in scenarios that require performance

By the way, I also tested the performance of nested serialization and deserialization MessagePack is still stronger than Json

Technology sharing group: 737776595

Sharing from token

Guess you like

Origin blog.csdn.net/xiaohucxy/article/details/127843594