Browse Source

initial commit

master
Brett Langdon 12 years ago
commit
64b416cbfe
8 changed files with 210 additions and 0 deletions
  1. +31
    -0
      README.md
  2. +28
    -0
      bson/bson.go
  3. +17
    -0
      bson/bson_test.go
  4. +28
    -0
      json/json.go
  5. +17
    -0
      json/json_test.go
  6. +44
    -0
      main.go
  7. +28
    -0
      msgpack/msgpack.go
  8. +17
    -0
      msgpack/msgpack_test.go

+ 31
- 0
README.md View File

@ -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
```

+ 28
- 0
bson/bson.go View File

@ -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)
}

+ 17
- 0
bson/bson_test.go View File

@ -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()
}
}

+ 28
- 0
json/json.go View File

@ -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)
}

+ 17
- 0
json/json_test.go View File

@ -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()
}
}

+ 44
- 0
main.go View File

@ -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)
}

+ 28
- 0
msgpack/msgpack.go View File

@ -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)
}

+ 17
- 0
msgpack/msgpack_test.go View File

@ -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()
}
}

Loading…
Cancel
Save