-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrent_create_order_benchmark.rb
83 lines (64 loc) · 2.2 KB
/
concurrent_create_order_benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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