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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/bench9000/main.rb', line 15
def self.main(args)
pre_args = args.dup
until pre_args.empty?
arg = pre_args.shift
if arg.start_with? "--"
case arg
when "--env"
key = pre_args.shift
value = pre_args.shift
ENV[key] = value
end
end
end
config = Config.new
load_configuration_file(config, 'default')
args = args.dup
command_name = args.shift
subjects = []
flags = {}
until args.empty?
arg = args.shift
if arg.start_with? "--"
case arg
when "--config"
load_configuration_file config, args.shift
when "--data"
flags[arg] = args.shift
when "--baseline"
flags[arg] = args.shift
when "--notes"
flags[arg] = args.shift
when "--resume"
flags[arg] = true
when "--show-commands"
flags[arg] = true
when "--show-samples"
flags[arg] = true
when "--ignore-excludes"
flags[arg] = true
when "--value-per-line"
flags[arg] = true
when "--benchmark-per-line"
flags[arg] = true
when "--json"
flags[arg] = true
when "--test-run"
flags[arg] = true
when "--env"
args.shift
args.shift
else
puts "unknown option #{arg}"
exit 1
end
else
subjects.push arg
end
end
implementations = []
benchmarks = []
subjects.each do |s|
negated = s.start_with? "^"
s = s[1..-1] if negated
if s == "all"
config.benchmarks.values.each do |b|
benchmarks.push b
end
elsif config.implementations.has_key? s
i = config.implementations[s]
if negated
implementations.delete i
else
implementations.push i
end
elsif config.implementation_groups.has_key? s
ig = config.implementation_groups[s]
if negated
implementations.delete_if { |i| ig.members.include? i }
else
implementations.concat ig.members
end
elsif config.benchmarks.has_key? s
b = config.benchmarks[s]
if negated
benchmarks.delete b
else
benchmarks.push b
end
elsif config.benchmark_groups.has_key? s
bg = config.benchmark_groups[s]
if negated
benchmarks.delete_if { |b| bg.members.include? b }
else
benchmarks.concat bg.members
end
else
puts "unknown implementation or benchmark or group #{s}"
exit 1
end
end
options = Bench::Options.new(
config,
implementations,
benchmarks,
flags)
start = Time.now
existing_measurements = Measurements.new
if options.flags.has_key?("--data")
if File.exist?(options.flags["--data"])
file = File.open(options.flags["--data"], "r")
if file.gets.strip != "version #{CONFIG_VERSION}"
puts "the benchmarks have changed since this file was created"
exit 1
end
file.each do |line|
measurement = JSON.parse(line)
if measurement["failed"]
measurement_object = :failed
else
measurement_object = Measurement.new(measurement["warmup_samples"], measurement["samples"])
end
existing_measurements[measurement["benchmark"], measurement["implementation"]] = measurement_object
end
file.close
end
end
builtin_commands = {
"compare" => Commands::Compare,
"compare-reference" => Commands::CompareReference,
"detail" => Commands::Detail,
"list-benchmarks" => Commands::ListBenchmarks,
"list-implementations" => Commands::ListImplementations,
"reference" => Commands::Reference,
"report" => Commands::Report,
"remove" => Commands::Remove,
"score" => Commands::Score
}
commands = builtin_commands.merge(config.commands)
command = commands[command_name]
if command.nil?
puts "unknown command #{command_name}"
exit 1
end
command = command.new
if command.before options, existing_measurements
if options.flags.has_key?("--data")
file = File.open(options.flags["--data"], "w")
file.puts "version #{CONFIG_VERSION}"
file.flush
existing_measurements.measurements.each do |bi, measurement|
b, i = bi
if measurement == :failed
file.puts JSON.generate({ benchmark: b, implementation: i, failed: true })
else
file.puts JSON.generate({ benchmark: b, implementation: i, warmup_samples: measurement.warmup_samples, samples: measurement.samples })
end
file.flush
end
end
measurements = Measurements.new
options.benchmarks.each do |b|
options.implementations.each do |i|
measurement = existing_measurements[b.name, i.name]
if measurement.nil?
if config.fail_hard_exclusions.include?([i.name, b.name]) && !options.flags["--ignore-excludes"]
measurement = :failed
else
measurement = i.measure(flags, b)
end
unless file.nil?
if measurement == :failed
file.puts JSON.generate({ benchmark: b, implementation: i, failed: true })
else
file.puts JSON.generate({ benchmark: b, implementation: i, warmup_samples: measurement.warmup_samples, samples: measurement.samples })
end
file.flush
end
end
measurements[b, i] = measurement
command.result options, b, i, measurement
end
command.benchmark_complete options, b, measurements
end
command.after options, measurements
unless file.nil?
file.close
end
end
puts "took #{Time.now - start}s"
end
|