From 64b416cbfe091325797dd037752e37ee962ae32d Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Tue, 22 Apr 2014 14:48:52 -0400 Subject: [PATCH] initial commit --- README.md | 31 +++++++++++++++++++++++++++++ bson/bson.go | 28 ++++++++++++++++++++++++++ bson/bson_test.go | 17 ++++++++++++++++ json/json.go | 28 ++++++++++++++++++++++++++ json/json_test.go | 17 ++++++++++++++++ main.go | 44 +++++++++++++++++++++++++++++++++++++++++ msgpack/msgpack.go | 28 ++++++++++++++++++++++++++ msgpack/msgpack_test.go | 17 ++++++++++++++++ 8 files changed, 210 insertions(+) create mode 100644 README.md create mode 100644 bson/bson.go create mode 100644 bson/bson_test.go create mode 100644 json/json.go create mode 100644 json/json_test.go create mode 100644 main.go create mode 100644 msgpack/msgpack.go create mode 100644 msgpack/msgpack_test.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..7306539 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +Some redimentary benchmarks for performance + size of different over the wire protocols. + + +```bash +$ git clone git://github.com/brettlangdon/proto-bench +$ cd ./proto-bench +$ go run main.go +Struct Size: 48 +BSON Size: 24 +BSON Length: 90 +Zversion@methodechoparams+0some1params2here +JSON Size: 24 +JSON Length: 63 +{"Version":2,"Method":"echo","Params":["some","params","here"]} +MSGPACK Size: 24 +MSGPACK Length: 51 +��Version�@�Method�echo�Params��some�params�here +$ go test -bench=. -benchmem ./bson ./json ./msgpack +PASS +BenchmarkBsonPack 500000 3950 ns/op 621 B/op 23 allocs/op +BenchmarkBsonUnpack 1000000 1206 ns/op 261 B/op 6 allocs/op +ok github.com/brettlangdon/proto-bench/bson 3.303s +PASS +BenchmarkJsonPack 1000000 1723 ns/op 287 B/op 4 allocs/op +BenchmarkJsonUnpack 500000 5349 ns/op 576 B/op 15 allocs/op +ok github.com/brettlangdon/proto-bench/json 4.472s +PASS +BenchmarkMsgpackPack 1000000 1585 ns/op 286 B/op 6 allocs/op +BenchmarkMsgpackUnpack 1000000 1440 ns/op 390 B/op 7 allocs/op +ok github.com/brettlangdon/proto-bench/msgpack 3.064s +``` diff --git a/bson/bson.go b/bson/bson.go new file mode 100644 index 0000000..76df371 --- /dev/null +++ b/bson/bson.go @@ -0,0 +1,28 @@ +package bson + +import ( + "labix.org/v2/mgo/bson" +) + +type BsonTest struct { + Version float32 + Method string + Params []string +} + +func BsonPack() { + bson.Marshal(&BsonTest{ + Version: 2.0, + Method: "echo", + Params: []string{ + "some", + "params", + "here", + }, + }) +} + +func BsonUnpack() { + var test BsonTest + bson.Unmarshal([]byte(`{"Version":2.0,"Method":"echo","params":["some","params","here"]}`), &test) +} diff --git a/bson/bson_test.go b/bson/bson_test.go new file mode 100644 index 0000000..40200c9 --- /dev/null +++ b/bson/bson_test.go @@ -0,0 +1,17 @@ +package bson + +import ( + "testing" +) + +func BenchmarkBsonPack(b *testing.B) { + for n := 0; n < b.N; n++ { + BsonPack() + } +} + +func BenchmarkBsonUnpack(b *testing.B) { + for n := 0; n < b.N; n++ { + BsonUnpack() + } +} diff --git a/json/json.go b/json/json.go new file mode 100644 index 0000000..5e7a47a --- /dev/null +++ b/json/json.go @@ -0,0 +1,28 @@ +package json + +import ( + "encoding/json" +) + +type JsonTest struct { + Version float32 + Method string + Params []string +} + +func JsonPack() { + json.Marshal(&JsonTest{ + Version: 2.0, + Method: "echo", + Params: []string{ + "some", + "params", + "here", + }, + }) +} + +func JsonUnpack() { + var test JsonTest + json.Unmarshal([]byte(`{"Version":2.0,"Method":"echo","params":["some","params","here"]}`), &test) +} diff --git a/json/json_test.go b/json/json_test.go new file mode 100644 index 0000000..f0ec4d2 --- /dev/null +++ b/json/json_test.go @@ -0,0 +1,17 @@ +package json + +import ( + "testing" +) + +func BenchmarkJsonPack(b *testing.B) { + for n := 0; n < b.N; n++ { + JsonPack() + } +} + +func BenchmarkJsonUnpack(b *testing.B) { + for n := 0; n < b.N; n++ { + JsonUnpack() + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..8b95c52 --- /dev/null +++ b/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/vmihailenco/msgpack" + "labix.org/v2/mgo/bson" + "unsafe" +) + +type Request struct { + Version float32 + Method string + Params []string +} + +func main() { + var req = Request{ + Version: 2.0, + Method: "echo", + Params: []string{ + "some", + "params", + "here", + }, + } + + fmt.Printf("Struct Size: %d\r\n", unsafe.Sizeof(req)) + + bson_str, _ := bson.Marshal(&req) + fmt.Printf("BSON Size: %d\r\n", unsafe.Sizeof(bson_str)) + fmt.Printf("BSON Length: %d\r\n", len(bson_str)) + fmt.Printf("%s\r\n", bson_str) + + json_str, _ := json.Marshal(&req) + fmt.Printf("JSON Size: %d\r\n", unsafe.Sizeof(json_str)) + fmt.Printf("JSON Length: %d\r\n", len(json_str)) + fmt.Printf("%s\r\n", json_str) + + msgpack_str, _ := msgpack.Marshal(&req) + fmt.Printf("MSGPACK Size: %d\r\n", unsafe.Sizeof(msgpack_str)) + fmt.Printf("MSGPACK Length: %d\r\n", len(msgpack_str)) + fmt.Printf("%s\r\n", msgpack_str) +} diff --git a/msgpack/msgpack.go b/msgpack/msgpack.go new file mode 100644 index 0000000..ab8f673 --- /dev/null +++ b/msgpack/msgpack.go @@ -0,0 +1,28 @@ +package msgpack + +import ( + "github.com/vmihailenco/msgpack" +) + +type MsgpackTest struct { + Version float32 + Method string + Params []string +} + +func MsgpackPack() { + msgpack.Marshal(&MsgpackTest{ + Version: 2.0, + Method: "echo", + Params: []string{ + "some", + "params", + "here", + }, + }) +} + +func MsgpackUnpack() { + var test MsgpackTest + msgpack.Unmarshal([]byte(`{"Version":2.0,"Method":"echo","params":["some","params","here"]}`), &test) +} diff --git a/msgpack/msgpack_test.go b/msgpack/msgpack_test.go new file mode 100644 index 0000000..c99492a --- /dev/null +++ b/msgpack/msgpack_test.go @@ -0,0 +1,17 @@ +package msgpack + +import ( + "testing" +) + +func BenchmarkMsgpackPack(b *testing.B) { + for n := 0; n < b.N; n++ { + MsgpackPack() + } +} + +func BenchmarkMsgpackUnpack(b *testing.B) { + for n := 0; n < b.N; n++ { + MsgpackUnpack() + } +}