Skip to content

Instantly share code, notes, and snippets.

@zhenjl
Last active December 21, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhenjl/6371433 to your computer and use it in GitHub Desktop.
Save zhenjl/6371433 to your computer and use it in GitHub Desktop.
Command: go test -bench=.
Machine: Macbook Air 10.8.4 1.8GHz Intel Core i5 4GB 1600MHz DDR3
BSON (message size = 152)
----
BenchmarkMarhsal 200000 6638 ns/op
BenchmarkUnmarshall 200000 9599 ns/op
MsgPack (message size = 126)
-------
BenchmarkMarhsal 500000 5154 ns/op
BenchmarkUnmarshall 500000 3043 ns/op
Machine: SuperMicro Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
MsgPack (message size = 126)
-------
BenchmarkMarhsal 500000 4463 ns/op
BenchmarkUnmarshall 1000000 2799 ns/op
package main
import (
"github.com/ugorji/go/codec"
//"labix.org/v2/mgo/bson"
"testing"
)
var (
r map[string]interface{}
s map[string]interface{}
mh codec.MsgpackHandle
)
func init() {
mh.RawToString = true
r = make(map[string]interface{})
r["timestamp"] = int64(930480234)
r["src_ip"] = "192.168.1.1"
r["dst_ip"] = "172.16.100.1"
r["sport"] = int64(30382)
r["dport"] = int64(800)
r["latency"] = int64(10)
r["jitter"] = int64(2)
r["throughput"] = int64(200)
r["loss"] = float64(0.003)
s = make(map[string]interface{}, 10)
}
func Marshall(in map[string]interface{}) (out []byte, err error) {
err = codec.NewEncoderBytes(&out, &mh).Encode(in)
//out, err = bson.Marshal(in)
return
}
func Unmarshall(in []byte) (map[string]interface{}, error) {
err := codec.NewDecoderBytes(in, &mh).Decode(&s)
//err := bson.Unmarshal(in, s)
return s, err
}
func benchMarshall(b *testing.B, r map[string]interface{}) {
for i := 0; i < b.N; i++ {
Marshall(r)
}
}
func benchUnmarshall(b *testing.B, s []byte) {
for i := 0; i < b.N; i++ {
Unmarshall(s)
}
}
func BenchmarkMarhsal(b *testing.B) {
benchMarshall(b, r)
}
func BenchmarkUnmarshall(b *testing.B) {
s, _ := Marshall(r)
benchUnmarshall(b, s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment