-
Notifications
You must be signed in to change notification settings - Fork 6
/
bench_test.go
185 lines (157 loc) · 4.23 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
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
184
185
// Copyright (c) 2020, 2021 Robert Clausecker <[email protected]>
package pospop
import "math/rand"
import "testing"
import "strconv"
// sizes to benchmark
var benchmarkLengths = []int{
1, 10, 100, 1000, 10 * 1000, 100 * 1000, 1000 * 1000, 10 * 1000 * 1000, 100 * 1000 * 1000,
}
// sizes to benchmark in a short benchmark
var benchmarkLengthsShort = []int{100 * 1000}
// benchmark a count8 implementation
func benchmarkCount8(b *testing.B, buf []uint8, lengths []int, count8 func(*[8]int, []uint8)) {
for _, l := range lengths {
b.Run(strconv.Itoa(l) + "B", func(b *testing.B) {
var counts [8]int
testbuf := buf[:l]
b.SetBytes(int64(l) * 1)
for i := 0; i < b.N; i++ {
count8(&counts, testbuf)
}
})
}
}
// benchmark all Count8 implementations
func BenchmarkCount8(b *testing.B) {
funcs := count8funcs
lengths := benchmarkLengths
// short benchmark: only test the implementation
// actually used and keep it to one size
if testing.Short() {
funcs = []count8impl{{Count8, "dispatch", true}}
lengths = benchmarkLengthsShort
}
maxlen := lengths[len(lengths)-1]
buf := make([]uint8, maxlen)
rand.Read(buf)
for _, impl := range funcs {
b.Run(impl.name, func(bb *testing.B) {
if !impl.available {
bb.SkipNow()
}
benchmarkCount8(bb, buf, lengths, impl.count8)
})
}
}
// benchmark a count16 implementation
func benchmarkCount16(b *testing.B, buf []uint16, lengths []int, count16 func(*[16]int, []uint16)) {
for _, l := range lengths {
b.Run(strconv.Itoa(l), func(b *testing.B) {
var counts [16]int
testbuf := buf[:l/2]
b.SetBytes(int64(l))
for i := 0; i < b.N; i++ {
count16(&counts, testbuf)
}
})
}
}
// benchmark all Count16 implementations
func BenchmarkCount16(b *testing.B) {
funcs := count16funcs
lengths := benchmarkLengths
// short benchmark: only test the implementation
// actually used and keep it to one size
if testing.Short() {
funcs = []count16impl{{Count16, "dispatch", true}}
lengths = benchmarkLengthsShort
}
maxlen := lengths[len(lengths)-1] / 2
buf := make([]uint16, maxlen)
for i := range buf {
buf[i] = uint16(rand.Int63())
}
for _, impl := range funcs {
b.Run(impl.name, func(bb *testing.B) {
if !impl.available {
bb.SkipNow()
}
benchmarkCount16(bb, buf, lengths, impl.count16)
})
}
}
// benchmark a count32 implementation
func benchmarkCount32(b *testing.B, buf []uint32, lengths []int, count32 func(*[32]int, []uint32)) {
for _, l := range lengths {
b.Run(strconv.Itoa(l), func(b *testing.B) {
var counts [32]int
testbuf := buf[:l/4]
b.SetBytes(int64(l))
for i := 0; i < b.N; i++ {
count32(&counts, testbuf)
}
})
}
}
// benchmark all Count32 implementations
func BenchmarkCount32(b *testing.B) {
funcs := count32funcs
lengths := benchmarkLengths
// short benchmark: only test the implementation
// actually used and keep it to one size
if testing.Short() {
funcs = []count32impl{{Count32, "dispatch", true}}
lengths = benchmarkLengthsShort
}
maxlen := lengths[len(lengths)-1] / 4
buf := make([]uint32, maxlen)
for i := range buf {
buf[i] = uint32(rand.Int63())
}
for _, impl := range funcs {
b.Run(impl.name, func(bb *testing.B) {
if !impl.available {
bb.SkipNow()
}
benchmarkCount32(bb, buf, lengths, impl.count32)
})
}
}
// benchmark a count64 implementation
func benchmarkCount64(b *testing.B, buf []uint64, lengths []int, count64 func(*[64]int, []uint64)) {
for _, l := range lengths {
b.Run(strconv.Itoa(l), func(b *testing.B) {
var counts [64]int
testbuf := buf[:l/8]
b.SetBytes(int64(l))
for i := 0; i < b.N; i++ {
count64(&counts, testbuf)
}
})
}
}
// benchmark all Count64 implementations
func BenchmarkCount64(b *testing.B) {
funcs := count64funcs
lengths := benchmarkLengths
// short benchmark: only test the implementation
// actually used and keep it to one size
if testing.Short() {
funcs = []count64impl{{Count64, "dispatch", true}}
lengths = benchmarkLengthsShort
}
maxlen := lengths[len(lengths)-1] / 8
buf := make([]uint64, maxlen)
for i := range buf {
buf[i] = rand.Uint64()
}
for _, impl := range funcs {
b.Run(impl.name, func(bb *testing.B) {
if !impl.available {
bb.SkipNow()
}
benchmarkCount64(bb, buf, lengths, impl.count64)
})
}
}