@@ -176,3 +176,122 @@ func benchInsertChain(b *testing.B, disk bool, gen func(int, *BlockGen)) {
176176 b .Fatalf ("insert error (block %d): %v\n " , i , err )
177177 }
178178}
179+
180+ func BenchmarkChainRead_header_10k (b * testing.B ) {
181+ benchReadChain (b , false , 10000 )
182+ }
183+ func BenchmarkChainRead_full_10k (b * testing.B ) {
184+ benchReadChain (b , true , 10000 )
185+ }
186+ func BenchmarkChainRead_header_100k (b * testing.B ) {
187+ benchReadChain (b , false , 100000 )
188+ }
189+ func BenchmarkChainRead_full_100k (b * testing.B ) {
190+ benchReadChain (b , true , 100000 )
191+ }
192+ func BenchmarkChainRead_header_500k (b * testing.B ) {
193+ benchReadChain (b , false , 500000 )
194+ }
195+ func BenchmarkChainRead_full_500k (b * testing.B ) {
196+ benchReadChain (b , true , 500000 )
197+ }
198+ func BenchmarkChainWrite_header_10k (b * testing.B ) {
199+ benchWriteChain (b , false , 10000 )
200+ }
201+ func BenchmarkChainWrite_full_10k (b * testing.B ) {
202+ benchWriteChain (b , true , 10000 )
203+ }
204+ func BenchmarkChainWrite_header_100k (b * testing.B ) {
205+ benchWriteChain (b , false , 100000 )
206+ }
207+ func BenchmarkChainWrite_full_100k (b * testing.B ) {
208+ benchWriteChain (b , true , 100000 )
209+ }
210+ func BenchmarkChainWrite_header_500k (b * testing.B ) {
211+ benchWriteChain (b , false , 500000 )
212+ }
213+ func BenchmarkChainWrite_full_500k (b * testing.B ) {
214+ benchWriteChain (b , true , 500000 )
215+ }
216+
217+ // makeChainForBench writes a given number of headers or empty blocks/receipts
218+ // into a database.
219+ func makeChainForBench (db ethdb.Database , full bool , count uint64 ) {
220+ var hash common.Hash
221+ for n := uint64 (0 ); n < count ; n ++ {
222+ header := & types.Header {
223+ Coinbase : common.Address {},
224+ Number : big .NewInt (int64 (n )),
225+ ParentHash : hash ,
226+ Difficulty : big .NewInt (1 ),
227+ UncleHash : types .EmptyUncleHash ,
228+ TxHash : types .EmptyRootHash ,
229+ ReceiptHash : types .EmptyRootHash ,
230+ }
231+ hash = header .Hash ()
232+ WriteHeader (db , header )
233+ WriteCanonicalHash (db , hash , n )
234+ WriteTd (db , hash , n , big .NewInt (int64 (n + 1 )))
235+ if full || n == 0 {
236+ block := types .NewBlockWithHeader (header )
237+ WriteBody (db , hash , n , block .Body ())
238+ WriteBlockReceipts (db , hash , n , nil )
239+ }
240+ }
241+ }
242+
243+ func benchWriteChain (b * testing.B , full bool , count uint64 ) {
244+ for i := 0 ; i < b .N ; i ++ {
245+ dir , err := ioutil .TempDir ("" , "eth-chain-bench" )
246+ if err != nil {
247+ b .Fatalf ("cannot create temporary directory: %v" , err )
248+ }
249+ db , err := ethdb .NewLDBDatabase (dir , 128 , 1024 )
250+ if err != nil {
251+ b .Fatalf ("error opening database at %v: %v" , dir , err )
252+ }
253+ makeChainForBench (db , full , count )
254+ db .Close ()
255+ os .RemoveAll (dir )
256+ }
257+ }
258+
259+ func benchReadChain (b * testing.B , full bool , count uint64 ) {
260+ dir , err := ioutil .TempDir ("" , "eth-chain-bench" )
261+ if err != nil {
262+ b .Fatalf ("cannot create temporary directory: %v" , err )
263+ }
264+ defer os .RemoveAll (dir )
265+
266+ db , err := ethdb .NewLDBDatabase (dir , 128 , 1024 )
267+ if err != nil {
268+ b .Fatalf ("error opening database at %v: %v" , dir , err )
269+ }
270+ makeChainForBench (db , full , count )
271+ db .Close ()
272+
273+ b .ReportAllocs ()
274+ b .ResetTimer ()
275+
276+ for i := 0 ; i < b .N ; i ++ {
277+ db , err := ethdb .NewLDBDatabase (dir , 128 , 1024 )
278+ if err != nil {
279+ b .Fatalf ("error opening database at %v: %v" , dir , err )
280+ }
281+ chain , err := NewBlockChain (db , testChainConfig (), FakePow {}, new (event.TypeMux ))
282+ if err != nil {
283+ b .Fatalf ("error creating chain: %v" , err )
284+ }
285+
286+ for n := uint64 (0 ); n < count ; n ++ {
287+ header := chain .GetHeaderByNumber (n )
288+ if full {
289+ hash := header .Hash ()
290+ GetBody (db , hash , n )
291+ GetBlockReceipts (db , hash , n )
292+ }
293+ }
294+
295+ db .Close ()
296+ }
297+ }
0 commit comments