github.com/btcsuite/btcd@v0.24.0/blockchain/bench_test.go (about)

     1  // Copyright (c) 2015 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package blockchain
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/btcsuite/btcd/btcutil"
    11  	"github.com/btcsuite/btcd/wire"
    12  )
    13  
    14  // BenchmarkIsCoinBase performs a simple benchmark against the IsCoinBase
    15  // function.
    16  func BenchmarkIsCoinBase(b *testing.B) {
    17  	tx, _ := btcutil.NewBlock(&Block100000).Tx(1)
    18  	b.ResetTimer()
    19  	for i := 0; i < b.N; i++ {
    20  		IsCoinBase(tx)
    21  	}
    22  }
    23  
    24  // BenchmarkIsCoinBaseTx performs a simple benchmark against the IsCoinBaseTx
    25  // function.
    26  func BenchmarkIsCoinBaseTx(b *testing.B) {
    27  	tx := Block100000.Transactions[1]
    28  	b.ResetTimer()
    29  	for i := 0; i < b.N; i++ {
    30  		IsCoinBaseTx(tx)
    31  	}
    32  }
    33  
    34  func BenchmarkUtxoFetchMap(b *testing.B) {
    35  	block := Block100000
    36  	transactions := block.Transactions
    37  	b.ResetTimer()
    38  
    39  	for i := 0; i < b.N; i++ {
    40  		needed := make(map[wire.OutPoint]struct{}, len(transactions))
    41  		for _, tx := range transactions[1:] {
    42  			for _, txIn := range tx.TxIn {
    43  				needed[txIn.PreviousOutPoint] = struct{}{}
    44  			}
    45  		}
    46  	}
    47  }
    48  
    49  func BenchmarkUtxoFetchSlices(b *testing.B) {
    50  	block := Block100000
    51  	transactions := block.Transactions
    52  	b.ResetTimer()
    53  
    54  	for i := 0; i < b.N; i++ {
    55  		needed := make([]wire.OutPoint, 0, len(transactions))
    56  		for _, tx := range transactions[1:] {
    57  			for _, txIn := range tx.TxIn {
    58  				needed = append(needed, txIn.PreviousOutPoint)
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  func BenchmarkAncestor(b *testing.B) {
    65  	height := 1 << 19
    66  	blockNodes := chainedNodes(nil, height)
    67  
    68  	b.ResetTimer()
    69  	for i := 0; i < b.N; i++ {
    70  		blockNodes[len(blockNodes)-1].Ancestor(0)
    71  		for j := 0; j <= 19; j++ {
    72  			blockNodes[len(blockNodes)-1].Ancestor(1 << j)
    73  		}
    74  	}
    75  }