您的位置:首页 > 编程语言 > C语言/C++

json, protobuf-c++,pbc,sproto 性能测试与解决方案

2015-06-24 19:41 716 查看
  本文目的是比较这四个方法的优缺点,并给出解决方案。

1. 测试环境说明

CPU: i3-2310M CPU @ 2.10GHz
OS: Linux(64位)
库的版本: lua5.1, luajit2.0,
注: 1. sproto库作者在代码中使用了lua5.2新库bitwise Operators,和我们使用的lua5.1环境不匹配,所以用能兼容lua5.2和lua5.1的库bit.numberlua代替 bitwise Operators。
2. 也尝试过用protoc-gen-lua,但解出来的table需要手动解析,而且耗费时间较大。

2. 测试

2.0 测试函数接口说明

libraryfunction interfaceinput/ output parameternote
sproto1.en=cookie.encode(ab)
2.de=cookie.decode(en)

1. input: ab is table
2. input: en is binary string

1. the schema of the sproto is very strong and flexible
2. 编写proto文件非常简单而且很人性化
3. 使用简单易用

sproto(nopack)similar with sprotosimilar with sproto
pbc-lua
1.en=protobuf.encode
("Cookie.CookieValue", CookieValue)
2.de=protobuf.decode
("Cookie.CookieValue", en)

1. input: CookieValue is table,
2. input en is binary

这个decode出来的table是惰性展开的,访问该table时才能解开,不然会出现乱码
protobuf-c++
1. str=bilin.Serialize(tb,'cookie')
2.bilin.Parse(str,'cookie',#str)

1. input: tb is a table,
2.input: str is binary string

1. 一有改动,较难维护
2. 耗费时间多小与压缩比效果很好,比其他的方法

lua-cjson1. en=cjson.encode(tb)
2.de=cjson.decode(en)

1. input: tb is a table,
2. input: en is cjson string

1. 空间占用很大
2. 耗费时间过大
3. 优点:(对我们现有的使用来看)无须转换程序,即解即用

2.1 测试结果

libraryencode times(1M次)decode times(1M次)size(byets)
sproto4.6501181125641s11.385328769684s139
sproto(nopack)4.0319328308105s9.9806959629059s272
pbc-lua9.3938179016113s7.0795350074768s117
protobuf-c++6.0948710441589s9.6896359920502s117
lua-cjson23.541377067566s15.335454940796s437
测 试数据:{"pbid":[{"id":100188,"value":3},{"id":100189,"value":3}, {"id":100190,"value":3}],"daily":[{"daily_id":1125,"date_id":[{"id":100188,"value":1}]},{"daily_id":1126,"date_id":[{"id":100118,"value":1}]}],"total":[{"id":100188,"value":1}],"segments":[{"type":1,"ct":1415384160,"ttl":1415384160,"id":100001}],"imp":[{"win":2,"bid":3,"time":8160,"id":100188},{"win":2,"bid":3,"time":8155,"id":100182},{"win":2,"bid":3,"time":8157,"id":100181}]}

3. 结果分析

在“2.0 测试函数接口说明”中“note”栏,已经指出了这五种方法的优缺点,可以选择一下两种方案作为我们这次问题的解决方案:

方案一:protobuf-c++,特点:在时间耗费和空间占用上,优势很明显,但一旦有新需求要变动,维护起来较为复杂;

方案二:sproto,特点:在时间耗费和空间占用上,也非常有优势,维护起来很方便,具体请参考sproto:https://github.com/cloudwu/sproto ,pbc请参考:https://github.com/cloudwu/pbc;谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: