mirror of
https://github.com/btcsuite/btcd.git
synced 2025-02-22 14:22:49 +01:00
btcutil: add benchmarks for Hash + WitnessHash
This commit is contained in:
parent
56de9ca878
commit
e102a81268
1 changed files with 80 additions and 0 deletions
80
btcutil/bench_test.go
Normal file
80
btcutil/bench_test.go
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
package btcutil_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
bencHash *chainhash.Hash
|
||||||
|
)
|
||||||
|
|
||||||
|
// BenchmarkTxHash benchmarks the performance of calculating the hash of a
|
||||||
|
// transaction.
|
||||||
|
func BenchmarkTxHash(b *testing.B) {
|
||||||
|
// Make a new block from the test block, we'll then call the Bytes
|
||||||
|
// function to cache the serialized block. Afterwards we all
|
||||||
|
// Transactions to populate the serialization cache.
|
||||||
|
testBlock := btcutil.NewBlock(&Block100000)
|
||||||
|
_, _ = testBlock.Bytes()
|
||||||
|
|
||||||
|
// The second transaction in the block has no witness data. The first
|
||||||
|
// does however.
|
||||||
|
testTx := testBlock.Transactions()[1]
|
||||||
|
testTx2 := testBlock.Transactions()[0]
|
||||||
|
|
||||||
|
// Run a benchmark for the portion that needs to strip the non-witness
|
||||||
|
// data from the transaction.
|
||||||
|
b.Run("tx_hash_has_witness", func(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var txHash *chainhash.Hash
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
txHash = testTx2.Hash()
|
||||||
|
}
|
||||||
|
|
||||||
|
bencHash = txHash
|
||||||
|
})
|
||||||
|
|
||||||
|
// Next, run it for the portion that can just hash the bytes directly.
|
||||||
|
b.Run("tx_hash_no_witness", func(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var txHash *chainhash.Hash
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
txHash = testTx.Hash()
|
||||||
|
}
|
||||||
|
|
||||||
|
bencHash = txHash
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkTxWitnessHash benchmarks the performance of calculating the hash of
|
||||||
|
// a transaction.
|
||||||
|
func BenchmarkTxWitnessHash(b *testing.B) {
|
||||||
|
// Make a new block from the test block, we'll then call the Bytes
|
||||||
|
// function to cache the serialized block. Afterwards we all
|
||||||
|
// Transactions to populate the serialization cache.
|
||||||
|
testBlock := btcutil.NewBlock(&Block100000)
|
||||||
|
_, _ = testBlock.Bytes()
|
||||||
|
|
||||||
|
// The first transaction in the block has been modified to have witness
|
||||||
|
// data.
|
||||||
|
testTx := testBlock.Transactions()[0]
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
var txHash *chainhash.Hash
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
txHash = testTx.WitnessHash()
|
||||||
|
}
|
||||||
|
|
||||||
|
bencHash = txHash
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue