Skip to content

Commit

Permalink
test: miner benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney committed Jul 14, 2024
1 parent 0c6c407 commit e1fef55
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ golines:
test: mod-tidy
go test -v -race ./...

bench: mod-tidy
go test -v -bench=. ./...

# Build our program binaries
# Depends on GO_FILES to determine when rebuild is needed
$(BINARIES): mod-tidy $(GO_FILES)
Expand Down
70 changes: 70 additions & 0 deletions internal/miner/miner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package miner

import (
"crypto/sha256"
"hash"
"testing"

sha256_simd "github.com/minio/sha256-simd"
)

// BenchmarkRandomNonce tests the performance of generating a completely new random nonce
// each iteration
func BenchmarkRandomNonce(b *testing.B) {
for i := 0; i < b.N; i++ {
randomNonce()
}
}

// BenchmarkRandomNonceAndIncrement tests the performance of generating a random nonce
// and incrementing each byte 255 times
func BenchmarkRandomNonceAndIncrement(b *testing.B) {
var tmpNonce [16]byte
for i := 0; i < b.N; i++ {
if i%256 == 0 {
// Generate random nonce for first and every 256 iterations
tmpNonce = randomNonce()
} else {
// Increment each byte of the last nonce
for j := 0; j < 16; j++ {
tmpNonce[j] = byte(uint8(tmpNonce[j]) + 1)
}
}
}
}

// BenchmarkSha256Builtin tests the performance of crypto/sha256
func BenchmarkSha256Builtin(b *testing.B) {
var hasher hash.Hash
tmpNonce := randomNonce()
for i := 0; i < b.N; i++ {
hasher = sha256.New()
hasher.Write(tmpNonce[:])
hasher.Sum(nil)
}
}

// BenchmarkSha256Simd tests the performance github.com/minio/sha256-simd
func BenchmarkSha256Simd(b *testing.B) {
var hasher hash.Hash
tmpNonce := randomNonce()
for i := 0; i < b.N; i++ {
hasher = sha256_simd.New()
hasher.Write(tmpNonce[:])
hasher.Sum(nil)
}
}

0 comments on commit e1fef55

Please sign in to comment.