当前位置: 代码网 > it编程>编程语言>Asp.net > MessagePack 和System.Text.Json 序列化和反序列化性能及对比分析

MessagePack 和System.Text.Json 序列化和反序列化性能及对比分析

2024年06月02日 Asp.net 我要评论
本博客将测试messagepack 和system.text.json 序列化和反序列化性能项目文件:program.cs代码:using benchmarkdotnet.running;using

本博客将测试messagepack 和system.text.json 序列化和反序列化性能
项目文件:

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 = 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的配置 messagepack.resolvers.contractlessstandardresolver.options
如果不加 messagepack.resolvers.contractlessstandardresolver.options 性能可能并不比json更快更好 启用了配置以后模型不需要添加特性 并且性能更快
在需要更快性能的场景messagepack更适合 并且传输的体积更小,所以非常推荐在需要性能的场景下使用messagepack

顺便我还测试过嵌套序列化和反序列化messagepack的表现还是比json的更强

到此这篇关于messagepack 和system.text.json 序列化和反序列化性能及对比分析的文章就介绍到这了,更多相关messagepack 和system.text.json 序列化和反序列化内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com