Golang: parsing a score between a message batch and JSON

We are working on a TCP server that accepts simple text commands over TCP (similar to redis)

We toss between using the raw text command, JSON, or the message package ( http://msgpack.org/ )

An example of a command might 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 figured out my own question, which is a comparison of the parsing speed of JSON and MsgPack. Please see the results in my answer

+4
source share
4 answers

Sort speed comparison:

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

My control 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
source

, .

() + Snappy ()

+1

msgpack promises json, . , , .

, . , .

:

" , , 97% : - "

, , , . .

http://blog.golang.org/profiling-go-programs

, go.

+1
source

Also, your test cases are canceled. BenchmarkJSON actually calls MsgPack and also BenchmarkMsgPack calls Json

can this do something about it?

0
source

All Articles