Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: miner benchmarks #224

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}