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