-
Notifications
You must be signed in to change notification settings - Fork 87
/
Benchmarks.jl
304 lines (263 loc) · 7.78 KB
/
Benchmarks.jl
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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Copyright (c) 2017: Miles Lubin and contributors
# Copyright (c) 2017: Google Inc.
#
# Use of this source code is governed by an MIT-style license that can be found
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
module Benchmarks
import BenchmarkTools
import MathOptInterface as MOI
const BENCHMARKS = Dict{String,Function}()
"""
suite(
new_model::Function;
exclude::Vector{Regex} = Regex[]
)
Create a suite of benchmarks. `new_model` should be a function that takes no
arguments, and returns a new instance of the optimizer you wish to benchmark.
Use `exclude` to exclude a subset of benchmarks.
## Example
```julia
julia> MOI.Benchmarks.suite() do
return GLPK.Optimizer()
end
julia> MOI.Benchmarks.suite(; exclude = [r"delete"]) do
return Gurobi.Optimizer()
end
```
"""
function suite(new_model::Function; exclude::Vector{Regex} = Regex[])
group = BenchmarkTools.BenchmarkGroup()
for (name, func) in BENCHMARKS
any(occursin.(exclude, Ref(name))) && continue
group[name] = BenchmarkTools.@benchmarkable $func($new_model)
end
return group
end
"""
create_baseline(suite, name::String; directory::String = ""; kwargs...)
Run all benchmarks in `suite` and save to files called `name` in `directory`.
Extra `kwargs` are based to `BenchmarkTools.run`.
## Example
```julia
julia> import MathOptInterface as MOI
julia> import GLPK
julia> my_suite = MOI.Benchmarks.suite(() -> GLPK.Optimizer());
julia> MOI.Benchmarks.create_baseline(
my_suite,
"glpk_master";
directory = "/tmp",
verbose = true,
)
```
"""
function create_baseline(
suite::BenchmarkTools.BenchmarkGroup,
name::String;
directory::String = "",
kwargs...,
)
BenchmarkTools.tune!(suite)
BenchmarkTools.save(
joinpath(directory, name * "_params.json"),
BenchmarkTools.params(suite),
)
results = BenchmarkTools.run(suite; kwargs...)
BenchmarkTools.save(joinpath(directory, name * "_baseline.json"), results)
return
end
"""
compare_against_baseline(
suite, name::String; directory::String = "",
report_filename::String = "report.txt"
)
Run all benchmarks in `suite` and compare against files called `name` in
`directory` that were created by a call to `create_baseline`.
A report summarizing the comparison is written to `report_filename` in
`directory`.
Extra `kwargs` are based to `BenchmarkTools.run`.
## Example
```julia
julia> import MathOptInterface as MOI
julia> import GLPK
julia> my_suite = MOI.Benchmarks.suite(() -> GLPK.Optimizer());
julia> MOI.Benchmarks.compare_against_baseline(
my_suite,
"glpk_master";
directory = "/tmp",
verbose = true,
)
```
"""
function compare_against_baseline(
suite::BenchmarkTools.BenchmarkGroup,
name::String;
directory::String = "",
report_filename::String = "report.txt",
kwargs...,
)
params_filename = joinpath(directory, name * "_params.json")
baseline_filename = joinpath(directory, name * "_baseline.json")
if !isfile(params_filename) || !isfile(baseline_filename)
error("You create a baseline with `create_baseline` first.")
end
BenchmarkTools.loadparams!(
suite,
BenchmarkTools.load(params_filename)[1],
:evals,
:samples,
)
new_results = BenchmarkTools.run(suite; kwargs...)
old_results = BenchmarkTools.load(baseline_filename)[1]
open(joinpath(directory, report_filename), "w") do io
println(stdout, "\n========== Results ==========")
println(io, "\n========== Results ==========")
for key in keys(new_results)
judgement = BenchmarkTools.judge(
BenchmarkTools.median(new_results[key]),
BenchmarkTools.median(old_results[key]),
)
println(stdout, "\n", key)
println(io, "\n", key)
show(stdout, MIME"text/plain"(), judgement)
show(io, MIME"text/plain"(), judgement)
end
end
return
end
###
### Benchmarks
###
macro add_benchmark(f)
name = f.args[1].args[1]
return quote
$(esc(f))
BENCHMARKS[String($(Base.Meta.quot(name)))] = $(esc(name))
end
end
@add_benchmark function add_variable(new_model)
model = new_model()
for i in 1:10_000
MOI.add_variable(model)
end
return model
end
@add_benchmark function add_variables(new_model)
model = new_model()
MOI.add_variables(model, 10_000)
return model
end
@add_benchmark function add_variable_constraint(new_model)
model = new_model()
x = MOI.add_variables(model, 10_000)
for (i, xi) in enumerate(x)
MOI.add_constraint(model, xi, MOI.LessThan(1.0 * i))
end
return model
end
@add_benchmark function add_variable_constraints(new_model)
model = new_model()
x = MOI.add_variables(model, 10_000)
MOI.add_constraints(model, x, MOI.LessThan.(1.0:10_000.0))
return model
end
@add_benchmark function delete_variable(new_model)
model = new_model()
x = MOI.add_variables(model, 1_000)
MOI.add_constraint.(model, x, Ref(MOI.LessThan(1.0)))
MOI.delete.(model, x)
return model
end
@add_benchmark function delete_variable_constraint(new_model)
model = new_model()
x = MOI.add_variables(model, 1_000)
cons = MOI.add_constraint.(model, x, Ref(MOI.LessThan(1.0)))
for con in cons
MOI.delete(model, con)
end
cons = MOI.add_constraint.(model, x, Ref(MOI.LessThan(1.0)))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0),
)
MOI.optimize!(model)
for con in cons
MOI.delete(model, con)
end
return model
end
@add_benchmark function add_constraint(new_model)
model = new_model()
index = MOI.add_variables(model, 10_000)
for (i, x) in enumerate(index)
MOI.add_constraint(
model,
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
MOI.LessThan(1.0 * i),
)
end
return model
end
@add_benchmark function add_constraints(new_model)
model = new_model()
x = MOI.add_variables(model, 10_000)
MOI.add_constraints(
model,
MOI.ScalarAffineFunction{Float64}[
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, xi)], 0.0) for
xi in x
],
MOI.LessThan.(1:1.0:10_000),
)
return model
end
@add_benchmark function delete_constraint(new_model)
model = new_model()
index = MOI.add_variables(model, 1_000)
cons = Vector{
MOI.ConstraintIndex{
MOI.ScalarAffineFunction{Float64},
MOI.LessThan{Float64},
},
}(
undef,
1_000,
)
for (i, x) in enumerate(index)
cons[i] = MOI.add_constraint(
model,
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
MOI.LessThan(1.0 * i),
)
end
for con in cons
MOI.delete(model, con)
end
return model
end
@add_benchmark function copy_model(new_model)
model = new_model()
index = MOI.add_variables(model, 1_000)
cons = Vector{
MOI.ConstraintIndex{
MOI.ScalarAffineFunction{Float64},
MOI.LessThan{Float64},
},
}(
undef,
1_000,
)
for (i, x) in enumerate(index)
cons[i] = MOI.add_constraint(
model,
MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0),
MOI.LessThan(1.0 * i),
)
end
model2 = new_model()
MOI.copy_to(model2, model)
# MOI.copy_to(model2, model, filter_constraints=(x) -> x in cons[1:500])
return model2
end
end