github.com/lbryio/lbcd@v0.22.119/database/ffldb/bench_test.go (about)

     1  // Copyright (c) 2015-2016 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 ffldb
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/lbryio/lbcd/chaincfg"
    13  	"github.com/lbryio/lbcd/database"
    14  	btcutil "github.com/lbryio/lbcutil"
    15  )
    16  
    17  // BenchmarkBlockHeader benchmarks how long it takes to load the mainnet genesis
    18  // block header.
    19  func BenchmarkBlockHeader(b *testing.B) {
    20  	// Start by creating a new database and populating it with the mainnet
    21  	// genesis block.
    22  	dbPath := filepath.Join(os.TempDir(), "ffldb-benchblkhdr")
    23  	_ = os.RemoveAll(dbPath)
    24  	db, err := database.Create("ffldb", dbPath, blockDataNet)
    25  	if err != nil {
    26  		b.Fatal(err)
    27  	}
    28  	defer os.RemoveAll(dbPath)
    29  	defer db.Close()
    30  	err = db.Update(func(tx database.Tx) error {
    31  		block := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
    32  		return tx.StoreBlock(block)
    33  	})
    34  	if err != nil {
    35  		b.Fatal(err)
    36  	}
    37  
    38  	b.ReportAllocs()
    39  	b.ResetTimer()
    40  	err = db.View(func(tx database.Tx) error {
    41  		blockHash := chaincfg.MainNetParams.GenesisHash
    42  		for i := 0; i < b.N; i++ {
    43  			_, err := tx.FetchBlockHeader(blockHash)
    44  			if err != nil {
    45  				return err
    46  			}
    47  		}
    48  		return nil
    49  	})
    50  	if err != nil {
    51  		b.Fatal(err)
    52  	}
    53  
    54  	// Don't benchmark teardown.
    55  	b.StopTimer()
    56  }
    57  
    58  // BenchmarkBlockHeader benchmarks how long it takes to load the mainnet genesis
    59  // block.
    60  func BenchmarkBlock(b *testing.B) {
    61  	// Start by creating a new database and populating it with the mainnet
    62  	// genesis block.
    63  	dbPath := filepath.Join(os.TempDir(), "ffldb-benchblk")
    64  	_ = os.RemoveAll(dbPath)
    65  	db, err := database.Create("ffldb", dbPath, blockDataNet)
    66  	if err != nil {
    67  		b.Fatal(err)
    68  	}
    69  	defer os.RemoveAll(dbPath)
    70  	defer db.Close()
    71  	err = db.Update(func(tx database.Tx) error {
    72  		block := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
    73  		return tx.StoreBlock(block)
    74  	})
    75  	if err != nil {
    76  		b.Fatal(err)
    77  	}
    78  
    79  	b.ReportAllocs()
    80  	b.ResetTimer()
    81  	err = db.View(func(tx database.Tx) error {
    82  		blockHash := chaincfg.MainNetParams.GenesisHash
    83  		for i := 0; i < b.N; i++ {
    84  			_, err := tx.FetchBlock(blockHash)
    85  			if err != nil {
    86  				return err
    87  			}
    88  		}
    89  		return nil
    90  	})
    91  	if err != nil {
    92  		b.Fatal(err)
    93  	}
    94  
    95  	// Don't benchmark teardown.
    96  	b.StopTimer()
    97  }