github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/consensus/accept_bench_test.go (about)

     1  package consensus
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/NebulousLabs/Sia/build"
     8  	"github.com/NebulousLabs/Sia/modules"
     9  	"github.com/NebulousLabs/Sia/types"
    10  )
    11  
    12  // BenchmarkAcceptEmptyBlocks measures how quckly empty blocks are integrated
    13  // into the consensus set.
    14  //
    15  // i7-4770, 1d60d69: 1.356 ms / op
    16  func BenchmarkAcceptEmptyBlocks(b *testing.B) {
    17  	cst, err := createConsensusSetTester("BenchmarkEmptyBlocks")
    18  	if err != nil {
    19  		b.Fatal("Error creating tester: " + err.Error())
    20  	}
    21  	defer cst.Close()
    22  
    23  	// Create an alternate testing consensus set, which does not
    24  	// have any subscribers
    25  	testdir := build.TempDir(modules.ConsensusDir, "BenchmarkEmptyBlocks - 2")
    26  	cs, err := New(cst.gateway, filepath.Join(testdir, modules.ConsensusDir))
    27  	if err != nil {
    28  		b.Fatal(err)
    29  	}
    30  	defer cs.Close()
    31  
    32  	// Synchronisze the cst and the subscriberless consensus set.
    33  	h := cst.cs.dbBlockHeight()
    34  	for i := types.BlockHeight(1); i <= h; i++ {
    35  		id, err := cst.cs.dbGetPath(i)
    36  		if err != nil {
    37  			b.Fatal(err)
    38  		}
    39  		processedBlock, err := cst.cs.dbGetBlockMap(id)
    40  		if err != nil {
    41  			b.Fatal(err)
    42  		}
    43  		err = cs.AcceptBlock(processedBlock.Block)
    44  		if err != nil {
    45  			b.Fatal(err)
    46  		}
    47  	}
    48  
    49  	b.ResetTimer()
    50  	b.StopTimer()
    51  	for j := 0; j < b.N; j++ {
    52  		// Submit a block to the consensus set tester - which has many
    53  		// subscribers. (untimed)
    54  		block, err := cst.miner.AddBlock()
    55  		if err != nil {
    56  			b.Fatal(err)
    57  		}
    58  
    59  		// Submit a block to the consensus set which has no subscribers.
    60  		// (timed)
    61  		b.StartTimer()
    62  		err = cs.AcceptBlock(block)
    63  		if err != nil {
    64  			b.Fatal("error accepting a block:", err)
    65  		}
    66  		b.StopTimer()
    67  	}
    68  }
    69  
    70  // BenchmarkAcceptSmallBlocks measures how quickly smaller blocks are
    71  // integrated into the consensus set.
    72  //
    73  // i7-4770, 1d60d69: 3.579 ms / op
    74  func BenchmarkAcceptSmallBlocks(b *testing.B) {
    75  	cst, err := createConsensusSetTester("BenchmarkAcceptSmallBlocks")
    76  	if err != nil {
    77  		b.Fatal(err)
    78  	}
    79  	defer cst.Close()
    80  
    81  	// COMPAT v0.4.0
    82  	//
    83  	// Push the height of the consensus set tester beyond the fork height.
    84  	for i := 0; i < 10; i++ {
    85  		_, err := cst.miner.AddBlock()
    86  		if err != nil {
    87  			b.Fatal(err)
    88  		}
    89  	}
    90  
    91  	// Create an alternate testing consensus set, which does not
    92  	// have any subscribers
    93  	testdir := build.TempDir(modules.ConsensusDir, "BenchmarkAcceptSmallBlocks - 2")
    94  	cs, err := New(cst.gateway, filepath.Join(testdir, modules.ConsensusDir))
    95  	if err != nil {
    96  		b.Fatal("Error creating consensus: " + err.Error())
    97  	}
    98  	defer cs.Close()
    99  
   100  	// Synchronize the consensus set with the consensus set tester.
   101  	h := cst.cs.dbBlockHeight()
   102  	for i := types.BlockHeight(1); i <= h; i++ {
   103  		id, err := cst.cs.dbGetPath(i)
   104  		if err != nil {
   105  			b.Fatal(err)
   106  		}
   107  		processedBlock, err := cst.cs.dbGetBlockMap(id)
   108  		if err != nil {
   109  			b.Fatal(err)
   110  		}
   111  		err = cs.AcceptBlock(processedBlock.Block)
   112  		if err != nil {
   113  			b.Fatal(err)
   114  		}
   115  	}
   116  
   117  	b.ResetTimer()
   118  	b.StopTimer()
   119  	for j := 0; j < b.N; j++ {
   120  		// Create a transaction with a miner fee, a normal siacoin output, and
   121  		// a funded file contract.
   122  		txnBuilder := cst.wallet.StartTransaction()
   123  		err = txnBuilder.FundSiacoins(types.NewCurrency64(125e6))
   124  		if err != nil {
   125  			b.Fatal(err)
   126  		}
   127  		// Add a small miner fee.
   128  		txnBuilder.AddMinerFee(types.NewCurrency64(5e6))
   129  		// Add a siacoin output.
   130  		txnBuilder.AddSiacoinOutput(types.SiacoinOutput{Value: types.NewCurrency64(20e6)})
   131  		// Add a file contract.
   132  		fc := types.FileContract{
   133  			WindowStart: 1000,
   134  			WindowEnd:   10005,
   135  			Payout:      types.NewCurrency64(100e6),
   136  			ValidProofOutputs: []types.SiacoinOutput{{
   137  				Value: types.NewCurrency64(96100e3),
   138  			}},
   139  			MissedProofOutputs: []types.SiacoinOutput{{
   140  				Value: types.NewCurrency64(96100e3),
   141  			}},
   142  		}
   143  		txnBuilder.AddFileContract(fc)
   144  		txnSet, err := txnBuilder.Sign(true)
   145  		if err != nil {
   146  			b.Fatal(err)
   147  		}
   148  
   149  		// Submit the transaction set and mine the block.
   150  		err = cst.tpool.AcceptTransactionSet(txnSet)
   151  		if err != nil {
   152  			b.Fatal(err)
   153  		}
   154  		block, err := cst.miner.AddBlock()
   155  		if err != nil {
   156  			b.Fatal(err)
   157  		}
   158  
   159  		// Submit the block to the consensus set without subscribers, timing
   160  		// how long it takes for the block to get accepted.
   161  		b.StartTimer()
   162  		err = cs.AcceptBlock(block)
   163  		if err != nil {
   164  			b.Fatal(err)
   165  		}
   166  		b.StopTimer()
   167  	}
   168  }