dongqiao5573 2013-11-22 19:30
浏览 37
已采纳

Golang:在消息包和JSON之间解析基准测试

We are working on a TCP server which takes simple textbased commands over TCP (similar to redis)

We are tossing up between using raw text command, JSON or message pack (http://msgpack.org/)

An example of a command could be:

text command: LOCK some_random_key 1000

JSON command: {"command":"LOCK","key":"some_random_key","timeout":1000}

messagePack: \x83\xA7command\xA4LOCK\xA3key\xAFsome_random_key\xA7timeout\xCD\x03\xE8

Question:

EDIT: I have figured out my own question which is the speed comparison between parsing JSON and MsgPack. Please see results in my answer

  • 写回答

4条回答 默认 最新

  • doulaopu2343 2013-11-24 04:58
    关注

    Parsing Speed Comparison:

    BenchmarkJSON     100000         17888 ns/op
    BenchmarkMsgPack      200000         10432 ns/op
    

    My benchmarking code:

    package benchmark
    
    import (
        "encoding/json"
        "github.com/vmihailenco/msgpack"
        "testing"
    )
    
    var in = map[string]interface{}{"c": "LOCK", "k": "31uEbMgunupShBVTewXjtqbBv5MndwfXhb", "T/O": 1000, "max": 200}
    
    func BenchmarkJSON(b *testing.B) {
        for i := 0; i < b.N; i++ {
            jsonB := EncodeJSON(in)
            DecodeJSON(jsonB)
        }
    }
    
    func BenchmarkMsgPack(b *testing.B) {
        for i := 0; i < b.N; i++ {
            b := EncodeMsgPack(in)
            DecodeMsgPack(b)
        }
    }
    
    func EncodeMsgPack(message map[string]interface{}) []byte {
        b, _ := msgpack.Marshal(message)
        return b
    }
    
    func DecodeMsgPack(b []byte) (out map[string]interface{}) {
        _ = msgpack.Unmarshal(b, &out)
        return
    }
    
    func EncodeJSON(message map[string]interface{}) []byte {
        b, _ := json.Marshal(message)
        return b
    }
    
    func DecodeJSON(b []byte) (out map[string]interface{}) {
        _ = json.Unmarshal(b, &out)
        return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题