From dff85593a29500201043b6286cbb300682732f8b Mon Sep 17 00:00:00 2001 From: Calvin Kim Date: Sat, 6 May 2023 01:22:15 +0900 Subject: [PATCH] wire: Add benchmarking for DoubleHashRaw Other benchmarks for DoubleHash are changed to reflect the actual usage during a TxHash() call. --- wire/bench_test.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/wire/bench_test.go b/wire/bench_test.go index 5176c962e80..dd0cd9aa21a 100644 --- a/wire/bench_test.go +++ b/wire/bench_test.go @@ -604,15 +604,13 @@ func BenchmarkTxHash(b *testing.B) { // BenchmarkDoubleHashB performs a benchmark on how long it takes to perform a // double hash returning a byte slice. func BenchmarkDoubleHashB(b *testing.B) { - var buf bytes.Buffer - if err := genesisCoinbaseTx.Serialize(&buf); err != nil { - b.Errorf("Serialize: unexpected error: %v", err) - return - } - txBytes := buf.Bytes() - - b.ResetTimer() for i := 0; i < b.N; i++ { + var buf bytes.Buffer + if err := genesisCoinbaseTx.Serialize(&buf); err != nil { + b.Fatalf("Serialize: unexpected error: %v", err) + } + txBytes := buf.Bytes() + _ = chainhash.DoubleHashB(txBytes) } } @@ -620,15 +618,22 @@ func BenchmarkDoubleHashB(b *testing.B) { // BenchmarkDoubleHashH performs a benchmark on how long it takes to perform // a double hash returning a chainhash.Hash. func BenchmarkDoubleHashH(b *testing.B) { - var buf bytes.Buffer - if err := genesisCoinbaseTx.Serialize(&buf); err != nil { - b.Errorf("Serialize: unexpected error: %v", err) - return - } - txBytes := buf.Bytes() - b.ResetTimer() for i := 0; i < b.N; i++ { + var buf bytes.Buffer + if err := genesisCoinbaseTx.Serialize(&buf); err != nil { + b.Fatalf("Serialize: unexpected error: %v", err) + } + txBytes := buf.Bytes() + _ = chainhash.DoubleHashH(txBytes) } } + +// BenchmarkDoubleHashRaw performs a benchmark on how long it takes to perform +// a double hash returning a byte slice. +func BenchmarkDoubleHashRaw(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = chainhash.DoubleHashRaw(genesisCoinbaseTx.Serialize) + } +}