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 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退