-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgotiny_bench_test.go
183 lines (166 loc) · 3.42 KB
/
gotiny_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gotiny
import (
"io"
"math/rand"
"reflect"
"testing"
"time"
)
var (
buf []byte
value = genA()
e *Encoder
d *Decoder
)
func init() {
t := reflect.TypeOf(value).Elem()
e = NewEncoderWithType(t)
d = NewDecoderWithType(t)
buf = e.encode(value)
}
func BenchmarkEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
Marshal(value)
}
}
func BenchmarkDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
Unmarshal(buf, value)
}
}
func BenchmarkEncode2(b *testing.B) {
for i := 0; i < b.N; i++ {
e.encode(value)
}
}
func BenchmarkDecode2(b *testing.B) {
for i := 0; i < b.N; i++ {
d.decode(buf, value)
}
}
type (
baseTyp struct {
FBool bool
FInt8 int8
FInt16 int16
FInt32 int32
FInt64 int64
FInt int
FUint8 uint8
FUint16 uint16
FUint32 uint32
FUint64 uint64
FUint uint
FUintptr uintptr
FFloat32 float32
FFloat64 float64
FComplex64 complex64
FComplex128 complex128
FString string
}
A struct {
Array [10]baseTyp
Slice []baseTyp
BirthDay time.Time
Inter any
M map[string]*baseTyp
}
)
func genBase() baseTyp {
return baseTyp{
FBool: rand.Int()%2 == 0,
FInt8: int8(rand.Int()),
FInt16: int16(rand.Int()),
FInt32: int32(rand.Int()),
FInt64: int64(rand.Int()),
FInt: int(rand.Int()),
FUint8: uint8(rand.Int()),
FUint16: uint16(rand.Int()),
FUint64: uint64(rand.Int()),
FUintptr: uintptr(rand.Int()),
FFloat32: rand.Float32(),
FFloat64: rand.Float64(),
FComplex64: complex(rand.Float32(), rand.Float32()),
FComplex128: complex(rand.Float64(), rand.Float64()),
FString: GetRandomString(20 + rand.Intn(256)),
}
}
func genA() *A {
ml := 10
a := &A{
BirthDay: time.Now(),
Inter: genBase(),
M: make(map[string]*baseTyp),
}
a.Slice = make([]baseTyp, len(a.Array))
for i := 0; i < len(a.Array); i++ {
a.Array[i] = genBase()
a.Slice[i] = genBase()
}
for i := 0; i < ml; i++ {
b := genBase()
a.M[GetRandomString(10)] = &b
}
return a
}
func GetRandomString(l int) string {
bytes := []byte("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
result := make([]byte, l)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result[i] = bytes[r.Intn(62)]
}
return string(result)
}
func BenchmarkDecodeUint64(b *testing.B) {
var ints = make([][]byte, 10000)
for i := 0; i < len(ints); i++ {
a := rand.Uint64()
ints[i] = Marshal(&a)
}
d := Decoder{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.buf = ints[rand.Intn(10000)]
d.index = 0
d.decUint64()
}
}
func BenchmarkEncodeUint64(b *testing.B) {
e := Encoder{buf: make([]byte, 0, 600000000)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
e.encUint64(rand.Uint64())
}
}
func BenchmarkEncodeBool(b *testing.B) {
l := 2000
e := Encoder{buf: make([]byte, 0, 600000000)}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < l*8; j++ {
e.encBool(i%2 == 0)
}
}
}
func BenchmarkDecodeBool(b *testing.B) {
l := 2000
var ints = make([][]byte, 10000)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < len(ints); i++ {
s := make([]byte, l)
io.ReadFull(r, s)
ints[i] = s
}
d := Decoder{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
d.buf = ints[rand.Intn(10000)]
d.boolBit = 0
d.boolPos = 0
d.index = 0
for j := 0; j < l*8; j++ {
d.decBool()
}
}
}