github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/modules/consensus/accept_bench_test.go (about) 1 package consensus 2 3 import ( 4 "path/filepath" 5 "strconv" 6 "testing" 7 8 "github.com/Synthesix/Sia/build" 9 "github.com/Synthesix/Sia/modules" 10 "github.com/Synthesix/Sia/modules/gateway" 11 "github.com/Synthesix/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 := cst.wallet.StartTransaction() 133 err = txnBuilder.FundSiacoins(types.NewCurrency64(125e6)) 134 if err != nil { 135 b.Fatal(err) 136 } 137 // Add a small miner fee. 138 txnBuilder.AddMinerFee(types.NewCurrency64(5e6)) 139 // Add a siacoin output. 140 txnBuilder.AddSiacoinOutput(types.SiacoinOutput{Value: types.NewCurrency64(20e6)}) 141 // Add a file contract. 142 fc := types.FileContract{ 143 WindowStart: 1000, 144 WindowEnd: 10005, 145 Payout: types.NewCurrency64(100e6), 146 ValidProofOutputs: []types.SiacoinOutput{{ 147 Value: types.NewCurrency64(96100e3), 148 }}, 149 MissedProofOutputs: []types.SiacoinOutput{{ 150 Value: types.NewCurrency64(96100e3), 151 }}, 152 } 153 txnBuilder.AddFileContract(fc) 154 txnSet, err := txnBuilder.Sign(true) 155 if err != nil { 156 b.Fatal(err) 157 } 158 159 // Submit the transaction set and mine the block. 160 err = cst.tpool.AcceptTransactionSet(txnSet) 161 if err != nil { 162 b.Fatal(err) 163 } 164 block, err := cst.miner.AddBlock() 165 if err != nil { 166 b.Fatal(err) 167 } 168 169 // Submit the block to the consensus set without subscribers, timing 170 // how long it takes for the block to get accepted. 171 b.StartTimer() 172 err = cs.AcceptBlock(block) 173 if err != nil { 174 b.Fatal(err) 175 } 176 b.StopTimer() 177 } 178 }