-
Notifications
You must be signed in to change notification settings - Fork 8
/
bench_test.go
73 lines (64 loc) · 1.57 KB
/
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
package tnt
import (
"bytes"
"encoding/binary"
"io"
"testing"
)
func BenchmarkSelectPack(b *testing.B) {
for n := 0; n < b.N; n++ {
query := &Select{
Values: Tuple{PackInt(11), PackInt(12)},
Space: 10,
Offset: 13,
Limit: 14,
Index: 15,
}
query.Pack(0, 0)
}
}
func BenchmarkPackInt(b *testing.B) {
value := uint32(4294866796)
for n := 0; n < b.N; n++ {
PackInt(value)
}
}
func BenchmarkPackIntAlt1(b *testing.B) {
value := uint32(4294866796)
for n := 0; n < b.N; n++ {
body := new(bytes.Buffer)
binary.Write(body, binary.LittleEndian, value)
body.Bytes()
}
}
func BenchmarkUnpackBody(b *testing.B) {
body := []uint8{0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x4, 0xa3, 0x51, 0x53, 0x71, 0x4, 0x2, 0x0, 0x0, 0x0}
for n := 0; n < b.N; n++ {
UnpackBody(body)
}
}
func BenchmarkReadHeader(b *testing.B) {
header := make([]byte, 12)
headerLen := len(header)
var bodyLen uint32
var requestID uint32
for n := 0; n < b.N; n++ {
buf := bytes.NewBuffer([]uint8{0x11, 0x0, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0})
io.ReadAtLeast(buf, header, headerLen)
bodyLen = UnpackInt(header[4:8])
requestID = UnpackInt(header[8:12])
if bodyLen != 44 || requestID != 5 {
b.FailNow()
}
}
}
func BenchmarkReadHeaderAlt1(b *testing.B) {
header := make([]int32, 3)
for n := 0; n < b.N; n++ {
buf := bytes.NewBuffer([]uint8{0x11, 0x0, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x5, 0x0, 0x0, 0x0})
binary.Read(buf, binary.LittleEndian, &header)
if header[1] != 44 || header[2] != 5 {
b.FailNow()
}
}
}