Skip to content

Commit cf4f8e0

Browse files
harshavardhanafwessels
authored andcommitted
Fix benchmarks to report proper values. (minio#7)
``` $ go test -run=NONE -bench . PASS BenchmarkHash64-4 1000000 1036 ns/op 61.77 MB/s BenchmarkHash128-4 2000000 801 ns/op 159.67 MB/s BenchmarkHash1K-4 500000 2464 ns/op 415.53 MB/s BenchmarkHash8K-4 200000 11212 ns/op 730.60 MB/s BenchmarkHash32K-4 30000 40766 ns/op 803.80 MB/s BenchmarkHash128K-4 10000 163170 ns/op 803.28 MB/s ok github.com/minio/blake2b-simd 10.298s ```
1 parent 0056201 commit cf4f8e0

File tree

6 files changed

+63
-79
lines changed

6 files changed

+63
-79
lines changed

blake2b_test.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -601,50 +601,41 @@ var goldenKeyed = []string{
601601
var bench = New512()
602602
var buf = make([]byte, 128*1024)
603603

604-
// Benchmark writes of 64 bytes.
605-
func BenchmarkHash64(b *testing.B) {
606-
b.SetBytes(64)
604+
func benchmarkSize(b *testing.B, size int) {
605+
b.SetBytes(int64(size))
607606
for i := 0; i < b.N; i++ {
608-
Sum512(buf[:64])
607+
bench.Reset()
608+
bench.Write(buf[:size])
609+
bench.Sum(nil)
609610
}
610611
}
611612

613+
// Benchmark writes of 64 bytes.
614+
func BenchmarkHash64(b *testing.B) {
615+
benchmarkSize(b, 64)
616+
}
617+
612618
// Benchmark writes of 128 bytes.
613619
func BenchmarkHash128(b *testing.B) {
614-
b.SetBytes(128)
615-
for i := 0; i < b.N; i++ {
616-
Sum512(buf[:128])
617-
}
620+
benchmarkSize(b, 128)
618621
}
619622

620623
// Benchmark writes of 1KiB bytes.
621-
func BenchmarkWrite1K(b *testing.B) {
622-
b.SetBytes(1024)
623-
for i := 0; i < b.N; i++ {
624-
bench.Write(buf[:1024])
625-
}
624+
func BenchmarkHash1K(b *testing.B) {
625+
benchmarkSize(b, 1024)
626626
}
627627

628628
// Benchmark writes of 8KiB bytes.
629-
func BenchmarkWrite8K(b *testing.B) {
630-
b.SetBytes(int64(len(buf)))
631-
for i := 0; i < b.N; i++ {
632-
bench.Write(buf[:8192])
633-
}
629+
func BenchmarkHash8K(b *testing.B) {
630+
benchmarkSize(b, 8*1024)
634631
}
635632

636633
// Benchmark writes of 32KiB bytes.
637-
func BenchmarkWrite32K(b *testing.B) {
638-
b.SetBytes(int64(len(buf)))
639-
for i := 0; i < b.N; i++ {
640-
bench.Write(buf[:32*1024])
641-
}
634+
func BenchmarkHash32K(b *testing.B) {
635+
benchmarkSize(b, 32*1024)
642636
}
643637

644638
// Benchmark writes of 128KiB bytes.
645-
func BenchmarkWrite128K(b *testing.B) {
646-
b.SetBytes(int64(len(buf)))
647-
for i := 0; i < b.N; i++ {
648-
bench.Write(buf)
649-
}
639+
func BenchmarkHash128K(b *testing.B) {
640+
benchmarkSize(b, 128*1024)
650641
}

compressAvx2_noasm.go

Lines changed: 0 additions & 23 deletions
This file was deleted.

compressAvx_amd64.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//+build !noasm
2+
//+build !appengine
3+
4+
/*
5+
* Minio Cloud Storage, (C) 2016 Minio, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package blake2b
21+
22+
//go:noescape
23+
func blockAVXLoop(p []uint8, in, iv, t, f, shffle, out []uint64)
24+
25+
func compressAVX(d *digest, p []uint8) {
26+
27+
in := make([]uint64, 8, 8)
28+
out := make([]uint64, 8, 8)
29+
30+
shffle := make([]uint64, 2, 2)
31+
// vector for PSHUFB instruction
32+
shffle[0] = 0x0201000706050403
33+
shffle[1] = 0x0a09080f0e0d0c0b
34+
35+
in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7] = d.h[0], d.h[1], d.h[2], d.h[3], d.h[4], d.h[5], d.h[6], d.h[7]
36+
37+
blockAVXLoop(p, in, iv[:], d.t[:], d.f[:], shffle, out)
38+
39+
d.h[0], d.h[1], d.h[2], d.h[3], d.h[4], d.h[5], d.h[6], d.h[7] = out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]
40+
}
File renamed without changes.

compress_amd64.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//+build !noasm
2-
//+build !appengine
3-
41
/*
52
* Minio Cloud Storage, (C) 2016 Minio, Inc.
63
*
@@ -19,34 +16,13 @@
1916

2017
package blake2b
2118

22-
//go:noescape
23-
func blockAVXLoop(p []uint8, in, iv, t, f, shffle, out []uint64)
24-
25-
func compressAVX(d *digest, p []uint8) {
26-
27-
in := make([]uint64, 8, 8)
28-
out := make([]uint64, 8, 8)
29-
30-
shffle := make([]uint64, 2, 2)
31-
// vector for PSHUFB instruction
32-
shffle[0] = 0x0201000706050403
33-
shffle[1] = 0x0a09080f0e0d0c0b
34-
35-
in[0], in[1], in[2], in[3], in[4], in[5], in[6], in[7] = d.h[0], d.h[1], d.h[2], d.h[3], d.h[4], d.h[5], d.h[6], d.h[7]
36-
37-
blockAVXLoop(p, in, iv[:], d.t[:], d.f[:], shffle, out)
38-
39-
d.h[0], d.h[1], d.h[2], d.h[3], d.h[4], d.h[5], d.h[6], d.h[7] = out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7]
40-
}
41-
4219
func compress(d *digest, p []uint8) {
4320
// Verifies if AVX2 or AVX is available, use optimized code path.
4421
if avx2 {
4522
compressAVX2(d, p)
46-
return
4723
} else if avx {
4824
compressAVX(d, p)
49-
return
50-
} // else { fallback to generic approach.
51-
compressGeneric(d, p)
25+
} else {
26+
compressGeneric(d, p)
27+
}
5228
}

cpuid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func haveAVX() bool {
4040

4141
// haveAVX2 returns true if when there is AVX2 support
4242
func haveAVX2() bool {
43-
4443
mfi, _, _, _ := cpuid(0)
44+
4545
// Check AVX2, AVX2 requires OS support, but BMI1/2 don't.
4646
if mfi >= 7 && haveAVX() {
4747
_, ebx, _, _ := cpuidex(7, 0)

0 commit comments

Comments
 (0)