Skip to content

Instantly share code, notes, and snippets.

@yosisa
Last active August 29, 2015 14:07
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 yosisa/87aa130469ab52c80c6a to your computer and use it in GitHub Desktop.
Save yosisa/87aa130469ab52c80c6a to your computer and use it in GitHub Desktop.
Go の msgpack ライブラリ比較 ref: http://qiita.com/yosisa/items/f21d3476bc8d368d7494
package main
import (
"bytes"
"os"
"testing"
"time"
"github.com/ugorji/go/codec"
"github.com/vmihailenco/msgpack"
)
type Event struct {
Tag string `codec:"tag" msgpack:"tag"`
Time time.Time `codec:"time" msgpack:"time"`
Record map[string]interface{}
}
var (
mh = &codec.MsgpackHandle{RawToString: true}
event *Event
)
func init() {
event = &Event{
Tag: "sysstat.process",
Time: time.Now(),
Record: map[string]interface{}{
"pid": 12353,
"name": "fluxion",
"cmd": "fluxion -c fluxion.toml",
"rss": 3563520,
"vms": 11005952,
"shared": 2666496,
"cpu_time": 80,
},
}
}
func BenchmarkCodecEncode(b *testing.B) {
buf := &bytes.Buffer{}
enc := codec.NewEncoder(buf, mh)
for i := 0; i < b.N; i++ {
enc.Encode(event)
buf.Reset()
}
}
func BenchmarkCodecDecode(b *testing.B) {
buf := &bytes.Buffer{}
codec.NewEncoder(buf, mh).Encode(event)
r := bytes.NewReader(buf.Bytes())
b.ResetTimer()
var ev Event
dec := codec.NewDecoder(r, mh)
for i := 0; i < b.N; i++ {
dec.Decode(&ev)
r.Seek(0, os.SEEK_SET)
}
}
func BenchmarkMsgpackEncode(b *testing.B) {
buf := &bytes.Buffer{}
enc := msgpack.NewEncoder(buf)
for i := 0; i < b.N; i++ {
enc.Encode(event)
buf.Reset()
}
}
func BenchmarkMsgpackDecode(b *testing.B) {
buf := &bytes.Buffer{}
msgpack.NewEncoder(buf).Encode(event)
r := bytes.NewReader(buf.Bytes())
b.ResetTimer()
var ev Event
dec := msgpack.NewDecoder(r)
for i := 0; i < b.N; i++ {
dec.Decode(&ev)
r.Seek(0, os.SEEK_SET)
}
}
$ go test -bench .
testing: warning: no tests to run
PASS
BenchmarkCodecEncode 1000000 1970 ns/op
BenchmarkCodecDecode 1000000 2543 ns/op
BenchmarkMsgpackEncode 500000 5274 ns/op
BenchmarkMsgpackDecode 500000 6793 ns/op
ok _/Users/yoshihisa/junk/msgpack 10.761s
type binaryUnmarshaler interface {
UnmarshalBinary(data []byte) error
}
type binaryMarshaler interface {
MarshalBinary() (data []byte, err error)
}
type encoder interface {
EncodeMsgpack(io.Writer) error
}
type decoder interface {
DecodeMsgpack(io.Reader) error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment