Commit 96ae91059705bbc80fbccf816e48835c77981d46

Authored by mhanne
Committed by Marius Hanne
1 parent 1f9fad6432
Exists in benchmark

run storage benchmarks on different git revisions

Showing 5 changed files with 173 additions and 2 deletions Side-by-side Diff

examples/benchmark.rb
  1 +#!/usr/bin/env ruby
  2 +
  3 +$:.unshift( File.expand_path("../../lib", __FILE__) )
  4 +require 'bitcoin'
  5 +require 'timeout'
  6 +require 'json'
  7 +
  8 +require_relative "../spec/bitcoin/helpers/fake_blockchain"
  9 +
  10 +# Bitcoin::NETWORKS[:testnet][:proof_of_work_limit] = 553713663
  11 +# Bitcoin::NETWORKS[:testnet][:genesis_hash] = "007203b460dfb42af8c58c836bb1414e29c93a7c67502f9c7b1a4369c72577b1"
  12 +# Bitcoin.network = :testnet
  13 +
  14 +Bitcoin.network = :testnet3
  15 +Bitcoin.network[:proof_of_work_limit] = 0x20ffffff
  16 +
  17 +class Bitcoin::Validation::Block
  18 + def difficulty; true; end
  19 +end
  20 +
  21 +def reset
  22 + `rm -rf test.db`
  23 + `echo 'drop database bitcoin_test; create database bitcoin_test;' | psql -p 5433`
  24 +# `echo "drop database bitcoin_test; create database bitcoin_test;" | mysql -uroot -ppassword`
  25 +end
  26 +
  27 +def time
  28 + t = Time.now
  29 + yield
  30 + Time.now - t
  31 +end
  32 +
  33 +def size
  34 + case @store.db.adapter_scheme
  35 + when :sqlite
  36 + size = File.size("test.db")
  37 + when :postgres
  38 + size = @store.db.fetch("select pg_database_size('bitcoin_test')").first[:pg_database_size]
  39 + when :mysql
  40 + size = @store.db.fetch("select sum(data_length+index_length) from information_schema.tables where table_schema = 'bitcoin_test';").first.to_a[0][1].to_i
  41 + end
  42 +end
  43 +
  44 +def run
  45 + @fake_blockchain = FakeBlockchain.new(5)
  46 +
  47 + time = time do
  48 + 5.times {|i| @store.new_block @fake_blockchain.block(i) }
  49 + end
  50 +
  51 + puts "#{time.to_i.to_s.rjust(5)} | #{size.to_s.rjust(10)} |"
  52 + return [time, size]
  53 +rescue Exception
  54 + puts "Error"
  55 + p $!
  56 + return [0, 0]
  57 +end
  58 +
  59 +
  60 +@time, @size = {}, {}
  61 +
  62 +[
  63 +#:utxo,
  64 +:sequel
  65 +].each do |backend|
  66 + puts "Running benchmark on #{backend} backend..."
  67 + reset
  68 +
  69 + [
  70 +# "sqlite://test.db",
  71 + "postgres://mhanne:password@localhost:5433/bitcoin_test",
  72 +# "mysql://root:password@localhost/bitcoin_test"
  73 + ].each do |db|
  74 + adapter = db.split(":").first
  75 + print "| #{adapter.ljust(8)} | "
  76 + begin
  77 + @store = Bitcoin::Storage.send(backend, db: db, log_level: :warn)
  78 + time, size = *run
  79 + @store.db.disconnect unless @store.db.adapter_scheme == :sqlite
  80 + rescue Exception
  81 + time, size = 0, 0
  82 + end
  83 + @time["#{backend}_#{adapter}"] = time
  84 + @size["#{backend}_#{adapter}"] = size
  85 + end
  86 +end
  87 +
  88 +File.open("tmp/bench_time.log", "a") do |file|
  89 + file.write "#{ARGV[0] || `git rev-parse HEAD`.strip} #{@time.to_json}\n"
  90 +end
  91 +
  92 +File.open("tmp/bench_size.log", "a") do |file|
  93 + file.write "#{ARGV[0] || `git rev-parse HEAD`.strip} #{@size.to_json}\n"
  94 +end
examples/graph_benchmark_results.rb
  1 +require 'SVG/Graph/Line'
  2 +require 'json'
  3 +
  4 +[:time, :size].each do |type|
  5 +
  6 + @commits = []
  7 + @data = {}
  8 +
  9 + File.read("tmp/bench_#{type}.log").each_line do |line|
  10 + commit, dat = *line.split(" ", 2)
  11 + @commits << commit
  12 + [:utxo, :sequel].each {|backend| [:sqlite, :postgres, :mysql].each {|adapter|
  13 + name = "#{backend}_#{adapter}"
  14 + @data[name] ||= []
  15 + @data[name] << JSON.parse(dat)[name]
  16 + }}
  17 + end
  18 +
  19 + graph = SVG::Graph::Line.new(height: 500, width: 1000, fields: @commits)
  20 + graph.rotate_x_labels = true
  21 + graph.scale_integers = true
  22 + [
  23 +# :utxo,
  24 + :sequel
  25 + ].each {|backend|
  26 + [
  27 +# :sqlite,
  28 + :postgres,
  29 +# :mysql
  30 + ].each {|adapter|
  31 + name = "#{backend}_#{adapter}"
  32 + p @data[name]
  33 + graph.add_data(data: @data[name], title: name)
  34 + }}
  35 +
  36 +File.open("graph_#{type}.svg", "w") {|f|
  37 + # f.write "Content-type: image/svg+xml\r\n\r\n"
  38 + f.write graph.burn()
  39 +}
  40 +end
examples/run_benchmarks.rb
  1 +[
  2 +# "2fd4aa6f88e9a0e94bc3191097eabdca7e2bc46a",
  3 +# "a37c84daef707bed59ab986e7e4376b7c3780aec",
  4 +# "1aa182078d6bb410120e77bc320244aab616b6fb",
  5 +# "3fabf27194dfac2b19711f3eb5d9f5410a2306de",
  6 +# "b33f46d0a0af921f78094c0c3cb9d3c7cdd46512",
  7 +# "0e0688c6b1f94124eeb48fdf2b771ee03e7d7dc5",
  8 +
  9 +# "54e9f9651647325354964e15f7d70a342f8fba73",
  10 +# "fc2fc65cdd67b626344fb24c6e702670ac5907ad",
  11 +# "33c6e1f0aeb75eb0087c270b6862d45f154d7353",
  12 +
  13 +# "185321af961c8b468e798d272bc10a95ff6e12ba",
  14 +# "1d4b174c19942d3ca17bec84b2639fae7ff2a302",
  15 +# "76aea5ee656b670dfe68bb96398bf09d7828e3cc",
  16 +
  17 +# "cbb4d3b6736da4fcbfddae77581e79e55025eaa2",
  18 +
  19 +"v0.0.2", "v0.0.3", "v0.0.4", "v0.0.5",
  20 +"comboy/sequel_copy_into",
  21 +"comboy/validation_optimization",
  22 +"comboy/store_block_optimizations",
  23 +"comboy/check_spent_in_current_block",
  24 +"comboy/validation_optimizations2",
  25 +"master"
  26 +].each do |rev|
  27 + puts "Running benchmarks for revision #{rev}"
  28 + `cp examples/benchmark.rb examples/benchmark.rb.bak`
  29 + `cp spec/bitcoin/helpers/fake_blockchain.rb spec/bitcoin/helpers/fake_blockchain.rb.bak`
  30 + `git checkout -f #{rev}`
  31 + `cp examples/benchmark.rb.bak examples/benchmark.rb`
  32 + `cp spec/bitcoin/helpers/fake_blockchain.rb.bak spec/bitcoin/helpers/fake_blockchain.rb`
  33 +
  34 + system("ruby examples/benchmark.rb #{rev}")
  35 +end
spec/bitcoin/helpers/fake_blockchain.rb
... ... @@ -177,7 +177,8 @@
177 177 end
178 178  
179 179 def block_path(depth)
180   - fixtures_path "fake_chain/#{depth}.blk"
  180 + # fixtures_path "fake_chain/#{depth}.blk"
  181 + "spec/bitcoin/fixtures/fake_chain/#{depth}.blk"
181 182 end
182 183  
183 184 end
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   - @fake_chain = FakeBlockchain.new 10
  19 + @fake_chain = FakeBlockchain.new 2
20 20 end
21 21  
22 22 it "block storage" do