MsgPack/Json性能数据

13 篇文章 1 订阅
8 篇文章 0 订阅

MsgPack/Json性能数据对比

MsgPack是一个高效的二进制序列化格式,像Json一样可以在各语言之间进行交换数据,都用来序列化存储数据。优势是在数据规模大时,效率比Json更高,占用资源更少;缺点是,二进制格式,不易查看数据。

业界Rpc交互之间使用广泛:JD,Baidu…
官网地址:http://pecl.php.net/package/msgpack

下面做了样本实验,简单对比MsgPack于Json性能:

实验数据「样本数据选取string类型,观察对比耗时、占比情况」:
在这里插入图片描述

此样本库可得出
String/Int混合类型中,MsgPack在序列化String时比Json压缩/耗时性能更优秀;Int类型时压缩比仅差1个百分点,耗时优势依旧突出。在数据对象中String类型占比大的场景,MsgPack比Json更适合。

:样本一数据中Msg/Json耗时占比逼近3个百分点;Msg/Json压缩占比平均在86%,短的字符串仅仅只需要比它的长度多3-4个字节的大小;

附样本数据:

//"github.com/vmihailenco/msgpack"
//msgpack于json对比 code
var rangeObj []string
var testSliceStr  string
testStr :="{\"from_trans\":1,\"uid_real_tags\":\"\",\"aid\":\"\",\"uid\":\"5537471228\",\"idfa\":\"\",\"pst_basic_profile\":200,\"positions\":\"3|8|13|18|23\",\"service\":\"discover_hotspot_feed\",\"oaid\":\"\",\"user_ip\":\"172.17.0.1\",\"mid_s_reason\":\"\",\"server_addr\":\"10.13.40.145\",\"experiment\":{},\"ts\":1617690796,\"blog_author_id\":\"\",\"loadmore\":\"0\",\"id\":\"16176907970138000027\",\"rc\":200,\"uve\":{\"ui_hc\":200,\"u_hc\":200,\"ui_rc\":200,\"u_rc\":0,\"passport\":\";;200|1.9888877868652\"},\"ua\":\"1\",\"engine_time\":107,\"render\":{\"resources\":[{\"ur_hc\":200,\"position\":3,\"ur_rc\":0,\"like\":\"404|0;404:1\",\"mblog\":\"404|0;200|3;\",\"time\":42},{\"ur_hc\":200,\"position\":8,\"ur_rc\":0,\"like\":\"404|0;404:2\",\"mblog\":\"404|0;200|13;\",\"time\":34}]},\"stocks\":{\"mixrank_ads\":\"18|23\",\"u_positions\":\"3|8|13\",\"std_positions\":\"3|8|13|18|23\",\"last_span\":\"-1\",\"interval\":\"5\"},\"owner_uid\":\"\",\"from\":\"1093193010\",\"version\":5,\"idx_id\":\"16176907970138000027\",\"bc_tags\":{\"l2_tags\":\";\",\"keywords\":\"\",\"l1_tags\":\"\"},\"mixrank\":1,\"mid\":\"\",\"filtered_imps\":\"\",\"head_time\":9,\"webview_ua\":\"0\",\"follows\":77,\"sub_service_id\":\"\",\"products\":[{\"positions\":[3,8,13,18,23],\"time\":104,\"rc\":200,\"product\":\"Sfst\",\"cands\":[{\"price\":[160,149,149,0],\"position\":3,\"status\":2000,\"ustatus\":12106,\"factors\":[\"3003\",\"3068807231\",\"\",\"1\",\"783_006\"],\"ur_hc\":500,\"bid\":\"160|1|1|1|160|0|0.00031228387|agent|4000\",\"ad_mid\":\"4610321166109510\",\"rstatus\":22106,\"feature\":\"88030004\",\"result\":\"4610321166109510\"},{\"status\":2030,\"price\":[160,0,0,0],\"position\":8,\"factors\":[\"3003\",\"3068807231\",\"\",\"1\",\"783_006\"],\"result\":\"4610321166109510\",\"ad_mid\":\"4610321166109510\",\"bid\":\"160|1|1|1|160|0|0.00031228387|agent|4000\",\"feature\":\"88030004\"},{\"price\":[1,1,1,0],\"position\":8,\"status\":2000,\"ustatus\":12106,\"factors\":[\"3001\",\"5931191764\",\"\",\"1\",\"783_006\"],\"ur_hc\":500,\"bid\":\"1|1|1|1|1|0|0.00030010002|agent|4000\",\"ad_mid\":\"4620027461112281\",\"rstatus\":22106,\"feature\":\"88030003\",\"result\":\"4620027461112281\"},{\"status\":2030,\"price\":[160,0,0,0],\"position\":13,\"factors\":[\"3003\",\"3068807231\",\"\",\"1\",\"783_006\"],\"result\":\"4610321166109510\",\"ad_mid\":\"4610321166109510\",\"bid\":\"160|1|1|1|160|0|0.00031228387|agent|4000\",\"feature\":\"88030004\"},{\"status\":2030,\"price\":[1,0,0,0],\"position\":13,\"factors\":[\"3001\",\"5931191764\",\"\",\"1\",\"783_006\"],\"result\":\"4620027461112281\",\"ad_mid\":\"4620027461112281\",\"bid\":\"1|1|1|1|1|0|0.00030010002|agent|4000\",\"feature\":\"88030003\"},{\"status\":2030,\"price\":[160,0,0,0],\"position\":18,\"factors\":[\"3003\",\"3068807231\",\"\",\"1\",\"783_006\"],\"result\":\"4610321166109510\",\"ad_mid\":\"4610321166109510\",\"bid\":\"160|1|1|1|160|0|0.00031228387|agent|4000\",\"feature\":\"88030004\"},{\"status\":2030,\"price\":[1,0,0,0],\"position\":18,\"factors\":[\"3001\",\"5931191764\",\"\",\"1\",\"783_006\"],\"result\":\"4620027461112281\",\"ad_mid\":\"4620027461112281\",\"bid\":\"1|1|1|1|1|0|0.00030010002|agent|4000\",\"feature\":\"88030003\"},{\"status\":2030,\"price\":[160,0,0,0],\"position\":23,\"factors\":[\"3003\",\"3068807231\",\"\",\"1\",\"783_006\"],\"result\":\"4610321166109510\",\"ad_mid\":\"4610321166109510\",\"bid\":\"160|1|1|1|160|0|0.00031228387|agent|4000\",\"feature\":\"88030004\"},{\"status\":2030,\"price\":[1,0,0,0],\"position\":23,\"factors\":[\"3001\",\"5931191764\",\"\",\"1\",\"783_006\"],\"result\":\"4620027461112281\",\"ad_mid\":\"4620027461112281\",\"bid\":\"1|1|1|1|1|0|0.00030010002|agent|4000\",\"feature\":\"88030003\"}]},{\"rc\":200,\"time\":7,\"positions\":[3,8,13,18,23],\"product\":\"WAX\"}],\"mac\":\"\",\"user_info\":[],\"imei\":\"da472ec3b036b57b98e71a1d9f970723\",\"unread_status\":\"15\",\"stock\":{\"783_006\":[3,8,13,18,23],\"30_003\":[3,8,13,18,23]},\"darwin_code\":\"200\",\"idx_time\":125}"
for i := 0;i<10000;i++{
    testSliceStr = testSliceStr + testStr
    if i%1000 == 0{
        rangeObj = append(rangeObj,testSliceStr)
    }
}
for i,v:= range rangeObj {
    testSliceStr = v
    //1、数据大小对比
    timeStart := time.Now()
    msgSizeRes, _ := msgpack.Marshal(testSliceStr)
    timeEnd := time.Now()
    duration1 := timeEnd.Sub(timeStart)
    fmt.Println("msg 序列化用时:", duration1)


    timeStart = time.Now()
    jsonSizeRes, _ := json.Marshal(testSliceStr)
    timeEnd = time.Now()
    duration2 := timeEnd.Sub(timeStart)
    fmt.Println("json 序列化用时:", duration2)
    fmt.Println("content 序列化前大小:", len([]byte(testSliceStr)))
    fmt.Println("msg 序列化后大小:", len(msgSizeRes))
    fmt.Println("json 序列化后大小:", len(jsonSizeRes))
    fmt.Println("msg:json 用时比:", float32(duration1.Microseconds())/float32(duration2.Microseconds()))
    fmt.Println("msg:json 压缩比:", float32(len(msgSizeRes))/float32(len(jsonSizeRes)))
    fmt.Printf("QQQQQQ===test:%+v\n", i)
}

Res如下:

msg 序列化用时: 360.833μs
json 序列化用时: 369.75μs
content 序列化前大小: 3442
msg序列化后大小: 3445
json 序列化后大小: 4002
msg:json 用时比: 0.9756098
msg:json 压缩比:0.8608196
QQQQQQ===test:0

msg 序列化用时: 5.112875ms
json 序列化用时: 25.18375ms
content 序列化前大小: 3445442
msg 序列化后大小: 3445447
json 序列化后大小: 4004002
msg:json 用时比: 0.20299408
msg:json 压缩比: 0.8605008
QQQQQQ===test:1

msg 序列化用时: 13.055833ms
json 序列化用时: 26.396916ms
content 序列化前大小: 6887442
msg 序列化后大小: 6887447
json 序列化后大小: 8004002
msg:json 用时比: 0.4945825
msg:json 压缩比: 0.8605004
QQQQQQ===test:2

msg 序列化用时: 13.461958ms
json 序列化用时: 39.432458ms
content 序列化前大小:10329442
msg 序列化后大小: 10329447
json 序列化后大小: 12004002
msg:json 用时比:0.3413725
msg:json 压缩比: 0.8605003
QQQQQQ===test:3

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/object.hpp:664:34: error: ‘void* memcpy(void*, const void*, size_t)’ copying an object of non-trivial type ‘struct msgpack::v2::object’ from an array of ‘const msgpack_object’ {aka ‘const struct msgpack_object’} [-Werror=class-memaccess] std::memcpy(&o, &v, sizeof(v)); ^ In file included from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object_fwd.hpp:17, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/adaptor/adaptor_base_decl.hpp:14, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/adaptor/adaptor_base_decl.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/adaptor/adaptor_base.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v1/object_decl.hpp:16, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object_decl.hpp:14, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/object.hpp:13, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack.hpp:10, from /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/example/cpp03/stream.cpp:10: /home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master/include/msgpack/v2/object_fwd.hpp:23:8: note: ‘struct msgpack::v2::object’ declared here struct object : v1::object { ^~~~~~ cc1plus: all warnings being treated as errors make[2]: *** [example/cpp03/CMakeFiles/stream.dir/build.make:63:example/cpp03/CMakeFiles/stream.dir/stream.cpp.o] 错误 1 make[2]: 离开目录“/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master” make[1]: *** [CMakeFiles/Makefile2:415:example/cpp03/CMakeFiles/stream.dir/all] 错误 2 make[1]: 离开目录“/home/AQTJClient/AQTJAuditClient/depends/msgpack-c-master”
07-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

魏小言

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值