-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_msgpack_test.go
60 lines (53 loc) · 1.38 KB
/
bench_msgpack_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package jsonbench_test
import (
"encoding/json"
"fmt"
"log"
"runtime"
"testing"
"github.com/riftbit/protocol_benches/structs"
"github.com/riftbit/protocol_benches/vars"
"github.com/vmihailenco/msgpack"
)
var (
MSGPackExampleJSONBytes []byte
MSGPackExampleMsgPackBytes []byte
MSGPackFilledClassicStruct structs.ClassicJSON
)
func init() {
MSGPackExampleJSONBytes = []byte(vars.ExampleJsonMinString)
err := json.Unmarshal(MSGPackExampleJSONBytes, &MSGPackFilledClassicStruct)
if err != nil {
log.Fatal(err)
}
MSGPackExampleMsgPackBytes, _ = msgpack.Marshal(&MSGPackFilledClassicStruct)
runtime.GC()
}
//
func Benchmark_msgpack_vmihailenco_Marshal(b *testing.B) {
// b.SetBytes(int64(len(MSGPackExampleMsgPackBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, err := msgpack.Marshal(MSGPackExampleMsgPackBytes)
if err != nil {
b.Fatal(err)
}
if len(buf) < 1000 {
b.Fatal(fmt.Errorf(`wrong size of buf - got %d`, len(buf)))
}
}
}
//
func Benchmark_msgpack_vmihailenco_Unmarshal(b *testing.B) {
// b.SetBytes(int64(len(MSGPackExampleMsgPackBytes)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
var buf structs.ClassicJSON
if err := msgpack.Unmarshal(MSGPackExampleMsgPackBytes, &buf); err != nil {
b.Fatal(err)
}
if buf.Aliceblue != "#f0f8ff" {
b.Fatal(fmt.Errorf(`wrong data in Aliceblue - got "%s" but expected "#f0f8ff"`, buf.Aliceblue))
}
}
}