Golang: Parsing benchmarking between message pack and JSON

go, json, messagepack

Solution

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
}

Problem

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

Original source