-
Notifications
You must be signed in to change notification settings - Fork 18
/
bench_test.go
171 lines (142 loc) · 3.05 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
package blake3
import (
"fmt"
"sync"
"testing"
"github.com/zeebo/blake3/internal/alg"
"github.com/zeebo/blake3/internal/consts"
)
func BenchmarkBLAKE3(b *testing.B) {
out := make([]byte, 32)
buf := make([]byte, 1024*1024+512)
pool := sync.Pool{
New: func() interface{} { return new(hasher) },
}
runIncr := func(b *testing.B, size int) {
buf := buf[:size]
b.ReportAllocs()
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := new(hasher)
t := buf
for len(t) >= 1024 {
h.update(t[:1024])
t = t[1024:]
}
if len(t) > 0 {
h.update(t)
}
h.finalize(out)
}
}
runEntire := func(b *testing.B, size int) {
buf := buf[:size]
b.ReportAllocs()
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := new(hasher)
h.update(buf)
h.finalize(out)
}
}
runReset := func(b *testing.B, size int) {
buf := buf[:size]
b.ReportAllocs()
b.SetBytes(int64(len(buf)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
h := pool.Get().(*hasher)
h.reset()
h.update(buf)
h.finalize(out)
pool.Put(h)
}
}
for _, kind := range []struct {
name string
run func(b *testing.B, size int)
}{
{"Incremental", runIncr},
{"Entire", runEntire},
{"Reset", runReset},
} {
b.Run(kind.name, func(b *testing.B) {
run := kind.run
for _, n := range []int{
1, 4, 8, 12, 16,
} {
b.Run(fmt.Sprintf("%04d_block", n), func(b *testing.B) { run(b, n*64) })
}
for _, n := range []int{
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
} {
b.Run(fmt.Sprintf("%04d_kib", n), func(b *testing.B) { run(b, n*1024) })
}
for _, n := range []int{
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024,
} {
b.Run(fmt.Sprintf("%04d_kib+512", n), func(b *testing.B) { run(b, n*1024+512) })
}
})
}
}
func BenchmarkHashF_1(b *testing.B) {
var input [8192]byte
var chain [8]uint32
var out chainVector
b.SetBytes(1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
alg.HashF(&input, 1, 0, 0, &consts.IV, &out, &chain)
}
}
func BenchmarkHashF_1536(b *testing.B) {
var input [8192]byte
var chain [8]uint32
var out chainVector
b.SetBytes(1536)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
alg.HashF(&input, 1536, 0, 0, &consts.IV, &out, &chain)
}
}
func BenchmarkHashF_8K(b *testing.B) {
var input [8192]byte
var chain [8]uint32
var out chainVector
b.SetBytes(8192)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
alg.HashF(&input, 8192, 0, 0, &consts.IV, &out, &chain)
}
}
func BenchmarkHashP(b *testing.B) {
var left chainVector
var right chainVector
var out chainVector
for n := 1; n <= 8; n++ {
b.Run(fmt.Sprint(n), func(b *testing.B) {
b.SetBytes(int64(64 * n))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
alg.HashP(&left, &right, 0, &consts.IV, &out, n)
}
})
}
}
func BenchmarkCompress(b *testing.B) {
var c [8]uint32
var m, o [16]uint32
b.SetBytes(64)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
alg.Compress(&c, &m, 0, 0, 0, &o)
}
}