Commit 2ffaeb44da07152d5efc5a891b4e1d20b0bcc40a

Authored by Marius Hanne
Committed by comboy
1 parent 32314aa0a1
Exists in master and in 1 other branch benchmark

refactor FakeBlockchain to work around caching issue

Showing 2 changed files with 26 additions and 23 deletions Side-by-side Diff

spec/bitcoin/helpers/fake_blockchain.rb
... ... @@ -29,11 +29,30 @@
29 29 # how performance looks when storing or validating 1K transactions
30 30 class FakeBlockchain
31 31  
  32 + # Initialize fake blockchain and generate +num_blocks+ starting blocks with given
  33 + # +opts+ (see #generate).
  34 + def initialize num = 50, opts = {}
  35 + Bitcoin.network = :fake
  36 + if File.exist? block_path(0)
  37 + genesis = Bitcoin::P::Block.new File.read block_path 0
  38 + Bitcoin.network[:genesis_hash] = genesis.hash
  39 + else
  40 + STDERR.puts "\nFake blockchain not present, generating (go take a nap)..."
  41 + depth = 0
  42 + FileUtils.mkdir_p fixtures_path "fake_chain"
  43 + generate(num, opts) do |blk|
  44 + File.open(block_path(depth),'w') {|f| f.write blk.to_payload }
  45 + depth += 1
  46 + end
  47 + # return new instance because this one apparently has half the data cached now
  48 + self.class.new
  49 + end
  50 + end
  51 +
32 52 # Generate fake blockchain with +num+ number of blocks
33 53 # Blocks are provided as an argument to the block given to the method
34   - # E.g.
35   - # FakeBlockChain.generate(5) {|b| save_block(b) }
36   - def self.generate(num = 50, opts = {})
  54 + # fake_chain.generate(5) {|b| save_block(b) }
  55 + def generate(num = 50, opts = {})
37 56 Bitcoin.network = :fake
38 57 srand 1337
39 58  
40 59  
... ... @@ -155,27 +174,11 @@
155 174 true
156 175 end
157 176  
158   - def self.prepare
159   - Bitcoin.network = :fake
160   - if File.exist? block_path(0)
161   - genesis = Bitcoin::P::Block.new File.read block_path 0
162   - Bitcoin.network[:genesis_hash] = genesis.hash
163   - else
164   - STDERR.puts "\nFake blockchain not present, generating (go take a nap)..."
165   - depth = 0
166   - FileUtils.mkdir_p fixtures_path "fake_chain"
167   - generate(50) do |blk|
168   - File.open(block_path(depth),'w') {|f| f.write blk.to_payload }
169   - depth += 1
170   - end
171   - end
172   - end
173   -
174   - def self.block(depth)
  177 + def block(depth)
175 178 Bitcoin::Protocol::Block.new File.read block_path depth
176 179 end
177 180  
178   - def self.block_path(depth)
  181 + def block_path(depth)
179 182 fixtures_path "fake_chain/#{depth}.blk"
180 183 end
181 184  
spec/bitcoin/performance/storage_spec.rb
... ... @@ -16,7 +16,7 @@
16 16 @store = storage
17 17 @store.reset
18 18 @store.log.level = :error
19   - FakeBlockchain.prepare
  19 + @fake_chain = FakeBlockchain.new 10
20 20 end
21 21  
22 22 it "block storage" do
... ... @@ -24,7 +24,7 @@
24 24 bm = Benchmark.measure do
25 25 bm = Benchmark.bm do |b|
26 26 10.times do |i|
27   - b.report("storing fake block ##{i}") { @store.new_block FakeBlockchain.block(i) }
  27 + b.report("storing fake block ##{i}") { @store.new_block @fake_chain.block(i) }
28 28 end
29 29 end
30 30 end