mirror of
https://github.com/btcsuite/btcd.git
synced 2025-02-23 14:40:44 +01:00
The doc formatting changes introduced in the recent go version is increasing the diff for all of the new commits. Formatting it all in this commit will help the readability of future PRs by reducing the diff.
97 lines
2.2 KiB
Go
97 lines
2.2 KiB
Go
// Copyright (c) 2015-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ffldb
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
"github.com/btcsuite/btcd/database"
|
|
)
|
|
|
|
// BenchmarkBlockHeader benchmarks how long it takes to load the mainnet genesis
|
|
// block header.
|
|
func BenchmarkBlockHeader(b *testing.B) {
|
|
// Start by creating a new database and populating it with the mainnet
|
|
// genesis block.
|
|
dbPath := filepath.Join(os.TempDir(), "ffldb-benchblkhdr")
|
|
_ = os.RemoveAll(dbPath)
|
|
db, err := database.Create("ffldb", dbPath, blockDataNet)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dbPath)
|
|
defer db.Close()
|
|
err = db.Update(func(tx database.Tx) error {
|
|
block := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
|
|
return tx.StoreBlock(block)
|
|
})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
err = db.View(func(tx database.Tx) error {
|
|
blockHash := chaincfg.MainNetParams.GenesisHash
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := tx.FetchBlockHeader(blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
// Don't benchmark teardown.
|
|
b.StopTimer()
|
|
}
|
|
|
|
// BenchmarkBlockHeader benchmarks how long it takes to load the mainnet genesis
|
|
// block.
|
|
func BenchmarkBlock(b *testing.B) {
|
|
// Start by creating a new database and populating it with the mainnet
|
|
// genesis block.
|
|
dbPath := filepath.Join(os.TempDir(), "ffldb-benchblk")
|
|
_ = os.RemoveAll(dbPath)
|
|
db, err := database.Create("ffldb", dbPath, blockDataNet)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer os.RemoveAll(dbPath)
|
|
defer db.Close()
|
|
err = db.Update(func(tx database.Tx) error {
|
|
block := btcutil.NewBlock(chaincfg.MainNetParams.GenesisBlock)
|
|
return tx.StoreBlock(block)
|
|
})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
err = db.View(func(tx database.Tx) error {
|
|
blockHash := chaincfg.MainNetParams.GenesisHash
|
|
for i := 0; i < b.N; i++ {
|
|
_, err := tx.FetchBlock(blockHash)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
// Don't benchmark teardown.
|
|
b.StopTimer()
|
|
}
|