btcd/blockchain/bench_test.go
Calvin Kim f396b3d3d9 blockchain: better Ancestor with skiplists
On startup, Ancestor call was taking a lot of time when the node was
loading the blockindex onto memory. This change speeds up the Ancestor
function significantly and speeds up the node during startup.

On testnet3 at blockheight ~2,500,000, the startup was around 30seconds
on current main and was 5 seconds with this change. Below is a benchstat
result showing the significant speedup.

goos: darwin
goarch: arm64
pkg: github.com/utreexo/utreexod/blockchain
           │     old.txt      │               new.txt                │
           │      sec/op      │    sec/op     vs base                │
Ancestor-8   120819.301µ ± 5%   7.013µ ± 19%  -99.99% (p=0.000 n=10)

           │  old.txt   │            new.txt             │
           │    B/op    │    B/op     vs base            │
Ancestor-8   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

           │  old.txt   │            new.txt             │
           │ allocs/op  │ allocs/op   vs base            │
Ancestor-8   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal
2023-11-17 16:55:58 +09:00

76 lines
1.7 KiB
Go

// Copyright (c) 2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"testing"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
)
// BenchmarkIsCoinBase performs a simple benchmark against the IsCoinBase
// function.
func BenchmarkIsCoinBase(b *testing.B) {
tx, _ := btcutil.NewBlock(&Block100000).Tx(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
IsCoinBase(tx)
}
}
// BenchmarkIsCoinBaseTx performs a simple benchmark against the IsCoinBaseTx
// function.
func BenchmarkIsCoinBaseTx(b *testing.B) {
tx := Block100000.Transactions[1]
b.ResetTimer()
for i := 0; i < b.N; i++ {
IsCoinBaseTx(tx)
}
}
func BenchmarkUtxoFetchMap(b *testing.B) {
block := Block100000
transactions := block.Transactions
b.ResetTimer()
for i := 0; i < b.N; i++ {
needed := make(map[wire.OutPoint]struct{}, len(transactions))
for _, tx := range transactions[1:] {
for _, txIn := range tx.TxIn {
needed[txIn.PreviousOutPoint] = struct{}{}
}
}
}
}
func BenchmarkUtxoFetchSlices(b *testing.B) {
block := Block100000
transactions := block.Transactions
b.ResetTimer()
for i := 0; i < b.N; i++ {
needed := make([]wire.OutPoint, 0, len(transactions))
for _, tx := range transactions[1:] {
for _, txIn := range tx.TxIn {
needed = append(needed, txIn.PreviousOutPoint)
}
}
}
}
func BenchmarkAncestor(b *testing.B) {
height := 1 << 19
blockNodes := chainedNodes(nil, height)
b.ResetTimer()
for i := 0; i < b.N; i++ {
blockNodes[len(blockNodes)-1].Ancestor(0)
for j := 0; j <= 19; j++ {
blockNodes[len(blockNodes)-1].Ancestor(1 << j)
}
}
}