forked from capnproto/go-capnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgp_bench_test.go
85 lines (69 loc) · 1.29 KB
/
msgp_bench_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// +build msgpbench
//go:generate msgp -tests=false -o msgp_bench_gen_test.go
//msgp:Tuple Event
package capnp_test
import (
"bytes"
"io"
"math/rand"
"testing"
"time"
"github.com/tinylib/msgp/msgp"
)
type Event struct {
Name string
BirthDay time.Time
Phone string
Siblings int
Spouse bool
Money float64
}
func BenchmarkUnmarshalMsgp(b *testing.B) {
r := rand.New(rand.NewSource(12345))
data := make([][]byte, 1000)
for i := range data {
msg, _ := (*Event)(generateA(r)).MarshalMsg(nil)
data[i] = msg
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var e Event
msg := data[r.Intn(len(data))]
_, err := e.UnmarshalMsg(msg)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkUnmarshalMsgpReader(b *testing.B) {
var buf bytes.Buffer
r := rand.New(rand.NewSource(12345))
w := msgp.NewWriter(&buf)
count := 10000
for i := 0; i < count; i++ {
event := (*Event)(generateA(r))
err := event.EncodeMsg(w)
if err != nil {
b.Fatal(err)
}
}
w.Flush()
blob := buf.Bytes()
b.ReportAllocs()
b.SetBytes(int64(buf.Len()))
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := msgp.NewReader(bytes.NewReader(blob))
for {
var e Event
err := e.DecodeMsg(r)
if err == io.EOF {
break
}
if err != nil {
b.Fatal(err)
}
}
}
}