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:0msg 序列化用时: 5.112875ms
json 序列化用时: 25.18375ms
content 序列化前大小: 3445442
msg 序列化后大小: 3445447
json 序列化后大小: 4004002
msg:json 用时比: 0.20299408
msg:json 压缩比: 0.8605008
QQQQQQ===test:1msg 序列化用时: 13.055833ms
json 序列化用时: 26.396916ms
content 序列化前大小: 6887442
msg 序列化后大小: 6887447
json 序列化后大小: 8004002
msg:json 用时比: 0.4945825
msg:json 压缩比: 0.8605004
QQQQQQ===test:2msg 序列化用时: 13.461958ms
json 序列化用时: 39.432458ms
content 序列化前大小:10329442
msg 序列化后大小: 10329447
json 序列化后大小: 12004002
msg:json 用时比:0.3413725
msg:json 压缩比: 0.8605003
QQQQQQ===test:3…