-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile
193 lines (151 loc) · 5.15 KB
/
Rakefile
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require 'bundler/gem_tasks'
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
require 'merit/version'
# Coverage -------------------------------------------------------------------
task :coverage do
ENV['COVERAGE'] = 'true'
exec 'bundle exec rspec'
end
# Documentation --------------------------------------------------------------
begin
require 'yard'
require 'yard-tomdoc'
YARD::Rake::YardocTask.new do |doc|
doc.options << '--no-highlight'
end
rescue LoadError
desc 'yard task requires that the yard gem is installed'
task :yard do
abort 'YARD is not available. In order to run yard, you must: gem ' \
'install yard'
end
end
# Console --------------------------------------------------------------------
namespace :console do
task :run do
command = system("which pry > /dev/null 2>&1") ? 'pry' : 'irb'
exec "#{ command } -I./lib -r./lib/merit.rb"
end
desc 'Open a pry or irb session with a stub'
task :stub do
require 'pry'
require_relative 'lib/merit/examples'
print 'Loading stub data...'
order = Merit::Examples.load('examples/much-flex.yml.gz')
order.calculate
puts ' done.'
# rubocop:disable Lint/Debugger
binding.pry
# rubocop:enable Lint/Debugger
end
end
desc 'Open a pry or irb session preloaded with Merit'
task console: ['console:run']
# Performance ----------------------------------------------------------------
namespace :performance do
require 'benchmark'
require 'merit'
require 'time'
require_relative 'lib/merit/examples'
# Loads and runs a scenario, outputing benchmark and profile data
#
# For example:
# $ be rake performance:profile PROFILE=true ITERATIONS=10 FILE=examples/much-flex.yml.gz
task :profile do
# Put any initial startup code which you don't want profiling here:
Merit::Curve.reader = Merit::Curve::CachingReader.new
iterations = (ENV['ITERATIONS'] || 10).to_i
path = ENV['FILE']
if path.nil? || path.empty?
raise 'You must supply a FILE=path to set up the scenario to be profiled'
end
print 'Loading stub data... '
stub = Merit::Examples.read(path)
puts 'done'
# Warm up.
Merit::Examples.build(stub).calculate
if iterations.positive?
print "#{iterations} iterations in ... "
time = Benchmark.realtime { iterations.times { Merit::Examples.build(stub).calculate } }
puts("#{time}s")
end
unless %w[0 no false off].include?(ENV['PROFILE'])
require 'ruby-prof'
print 'Profiling one run... '
order = Merit::Examples.build(stub)
GC.disable
RubyProf.measure_mode = RubyProf::ALLOCATIONS
RubyProf.start
order.calculate
result = RubyProf.stop
printer = RubyProf::GraphHtmlPrinter.new(result)
profile_name = "measurements/#{Time.now.strftime('%Y-%h-%m-%H%M-%S')}.html"
File.open(profile_name, 'w') do |file|
printer.print(file, min_percent: 0)
end
puts "saved to ./#{profile_name}"
GC.enable
end
end
task :reserve do
require 'benchmark/ips'
Benchmark.ips do |x|
x.report('SimpleReserve without decay') do
reserve = Merit::Flex::SimpleReserve.new(10)
8760.times do |frame|
(frame % 2).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
x.report('SimpleReserve with decay') do
reserve = Merit::Flex::SimpleReserve.new(10) { |val| val * 0.1 }
8760.times do |frame|
(frame % 2).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
x.report('SimpleReserve without decay, every 4th hour') do
reserve = Merit::Flex::SimpleReserve.new(10)
8760.times do |frame|
if (frame % 4).zero?
(frame % 8).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
end
x.report('SimpleReserve with decay, every 4th hour') do
reserve = Merit::Flex::SimpleReserve.new(10) { |val| val * 0.1 }
8760.times do |frame|
if (frame % 4).zero?
(frame % 8).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
end
x.report('Reserve without decay') do
reserve = Merit::Flex::Reserve.new(10)
8760.times do |frame|
(frame % 2).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
x.report('Reserve with decay') do
reserve = Merit::Flex::Reserve.new(10) { |val| val * 0.1 }
8760.times do |frame|
(frame % 2).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
x.report('Reserve without decay, every 4th hour') do
reserve = Merit::Flex::Reserve.new(10)
8760.times do |frame|
if (frame % 4).zero?
(frame % 8).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
end
x.report('Reserve with decay, every 4th hour') do
reserve = Merit::Flex::Reserve.new(10) { |val| val * 0.1 }
8760.times do |frame|
if (frame % 4).zero?
(frame % 8).zero? ? reserve.add(frame, 5) : reserve.take(frame, 5)
end
end
end
end
end
end