github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/ffldb/bench_test.go (about)

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