From 3df73a850c54b965a7a371eac429f0b12bde6a41 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 28 May 2025 09:41:17 +0800 Subject: [PATCH] crypto, core: add benchmarks --- core/rawdb/database_test.go | 17 ----------------- crypto/crypto_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 17 deletions(-) delete mode 100644 core/rawdb/database_test.go diff --git a/core/rawdb/database_test.go b/core/rawdb/database_test.go deleted file mode 100644 index a0d7b5ec66..0000000000 --- a/core/rawdb/database_test.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package rawdb diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 5b0c9c0533..e620d6ee3a 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -19,6 +19,7 @@ package crypto import ( "bytes" "crypto/ecdsa" + "crypto/rand" "encoding/hex" "math/big" "os" @@ -297,3 +298,38 @@ func TestPythonIntegration(t *testing.T) { t.Logf("msg: %x, privkey: %s sig: %x\n", msg0, kh, sig0) t.Logf("msg: %x, privkey: %s sig: %x\n", msg1, kh, sig1) } + +// goos: darwin +// goarch: arm64 +// pkg: github.com/ethereum/go-ethereum/crypto +// cpu: Apple M1 Pro +// BenchmarkKeccak256Hash +// BenchmarkKeccak256Hash-8 931095 1270 ns/op 32 B/op 1 allocs/op +func BenchmarkKeccak256Hash(b *testing.B) { + var input [512]byte + rand.Read(input[:]) + + b.ReportAllocs() + for i := 0; i < b.N; i++ { + Keccak256Hash(input[:]) + } +} + +// goos: darwin +// goarch: arm64 +// pkg: github.com/ethereum/go-ethereum/crypto +// cpu: Apple M1 Pro +// BenchmarkHashData +// BenchmarkHashData-8 793386 1278 ns/op 32 B/op 1 allocs/op +func BenchmarkHashData(b *testing.B) { + var ( + input [512]byte + buffer = NewKeccakState() + ) + rand.Read(input[:]) + + b.ReportAllocs() + for i := 0; i < b.N; i++ { + HashData(buffer, input[:]) + } +}