From e1fef55f74de379d88ad4cd3e07e0858767d2fd1 Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Sat, 13 Jul 2024 20:03:11 -0500 Subject: [PATCH] test: miner benchmarks --- Makefile | 3 ++ internal/miner/miner_test.go | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 internal/miner/miner_test.go diff --git a/Makefile b/Makefile index d70d390..55f8689 100644 --- a/Makefile +++ b/Makefile @@ -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) diff --git a/internal/miner/miner_test.go b/internal/miner/miner_test.go new file mode 100644 index 0000000..1b82513 --- /dev/null +++ b/internal/miner/miner_test.go @@ -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) + } +}