Skip to content

Commit

Permalink
add concurrent create order benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Xie committed Mar 14, 2014
1 parent b18cd84 commit 590982a
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
83 changes: 83 additions & 0 deletions bin/concurrent_create_order_benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env ruby

ENV['RAILS_ENV'] = 'test'
require_relative '../config/environment'
require_relative 'matching_benchmark'

class ConcurrentCreateOrderBenchmark < MatchingBenchmark

def initialize(label, num, round, process_num)
super(label, num, round)
@process_num = process_num
end

def collect_time
time = Dir[Rails.root.join('tmp', 'concurrent_create_order_*')].map do |f|
File.open(f, 'r') {|ff| ff.read.to_f }
end.max
puts "elapsed: #{time}"
Benchmark::Tms.new(0, 0, 0, 0, time)
end

def create_orders
members = Member.all
members.in_groups(@process_num, false).each_with_index do |users, i|
unless Process.fork
ActiveRecord::Base.connection.reconnect!
puts "Process #{i+1} started."

t1 = Time.now
users.each {|m| SweatFactory.make_ask_order(m, 10, 4000) }
elapsed = Time.now - t1
File.open(Rails.root.join('tmp', "concurrent_create_order_#{i+1}"), 'w') {|f| f.write(elapsed.to_f) }

puts "Process #{i+1} finished, stop."
exit 0
end
end

pid_and_status = Process.waitall
ActiveRecord::Base.connection.reconnect!

collect_time
end

def run_prepare_orders
(1..@round).map do |i|
puts "\n>> Round #{i}"
Benchmark.benchmark(Benchmark::CAPTION, 20, Benchmark::FORMAT) do |x|
@times[:create_members] << x.report("create members") { create_members }
@times[:lock_funds] << x.report("lock funds") { lock_funds }
nil
end
end
end

def run
run_prepare_orders

Benchmark.benchmark(Benchmark::CAPTION, 20, Benchmark::FORMAT) do |x|
@times[:create_orders] = [ create_orders ]
puts "#{Order.count} orders created by #{@process_num} processes."
end

save
end

end


if $0 == __FILE__
raise "Must run in test environment!" unless Rails.env.test?

process_num = ARGV[0] ? ARGV[0].to_i : 8
num = ARGV[1] ? ARGV[1].to_i : 250
round = ARGV[2] ? ARGV[2].to_i : 4
label = ARGV[3] || Time.now.to_i

puts "\n>> Setup environment"
system("rake db:reset")
Dir[Rails.root.join('tmp', 'concurrent_create_order_*')].each {|f| FileUtils.rm(f) }

ConcurrentCreateOrderBenchmark.new(label, num, round, process_num).run
end
6 changes: 3 additions & 3 deletions bin/matching_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def initialize(label, num, round)
@label = label.to_s
@num = num
@round = round
@times = {create_members: [], lock_funds: [], create_orders: [], matching: []}
@times = Hash.new {|h,k| h[k] = [] }
end

def create_members
Expand Down Expand Up @@ -173,8 +173,8 @@ def save
puts "lock funds: %.2fops" % [@num/avg[:lock_funds].last]
puts "create orders: %.2fops" % [@num/avg[:create_orders].last]
puts "submit orders: %.2fops" % [@num/(avg[:lock_funds].last+avg[:create_orders].last)]
puts "matching: %.2fops" % [@processed/avg[:matching].last]
puts "execution: %.2feps" % [@instructions.size/avg[:execution].last]
puts "matching: %.2fops" % [@processed/avg[:matching].last] if avg[:matching]
puts "execution: %.2feps" % [@instructions.size/avg[:execution].last] if avg[:execution]
puts "* submit order = lock funds + create order"
end

Expand Down

0 comments on commit 590982a

Please sign in to comment.