forked from JuliaCI/BenchmarkTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrials.jl
375 lines (314 loc) · 11.1 KB
/
trials.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#########
# Trial #
#########
mutable struct Trial
params::Parameters
times::Vector{Float64}
gctimes::Vector{Float64}
memory::Int
allocs::Int
end
Trial(params::Parameters) = Trial(params, Float64[], Float64[], typemax(Int), typemax(Int))
function Base.:(==)(a::Trial, b::Trial)
return a.params == b.params &&
a.times == b.times &&
a.gctimes == b.gctimes &&
a.memory == b.memory &&
a.allocs == b.allocs
end
Base.copy(t::Trial) = Trial(copy(t.params), copy(t.times), copy(t.gctimes), t.memory, t.allocs)
function Base.push!(t::Trial, time, gctime, memory, allocs)
push!(t.times, time)
push!(t.gctimes, gctime)
memory < t.memory && (t.memory = memory)
allocs < t.allocs && (t.allocs = allocs)
return t
end
function Base.deleteat!(t::Trial, i)
deleteat!(t.times, i)
deleteat!(t.gctimes, i)
return t
end
Base.length(t::Trial) = length(t.times)
Base.getindex(t::Trial, i::Number) = push!(Trial(t.params), t.times[i], t.gctimes[i], t.memory, t.allocs)
Base.getindex(t::Trial, i) = Trial(t.params, t.times[i], t.gctimes[i], t.memory, t.allocs)
Base.lastindex(t::Trial) = length(t)
function Base.sort!(t::Trial)
inds = sortperm(t.times)
t.times = t.times[inds]
t.gctimes = t.gctimes[inds]
return t
end
Base.sort(t::Trial) = sort!(copy(t))
Base.time(t::Trial) = time(minimum(t))
gctime(t::Trial) = gctime(minimum(t))
memory(t::Trial) = t.memory
allocs(t::Trial) = t.allocs
params(t::Trial) = t.params
# returns the index of the first outlier in `values`, if any outliers are detected.
# `values` is assumed to be sorted from least to greatest, and assumed to be right-skewed.
function skewcutoff(values)
current_values = copy(values)
while mean(current_values) > median(current_values)
deleteat!(current_values, length(current_values))
end
return length(current_values) + 1
end
skewcutoff(t::Trial) = skewcutoff(t.times)
function rmskew!(t::Trial)
sort!(t)
i = skewcutoff(t)
i <= length(t) && deleteat!(t, i:length(t))
return t
end
function rmskew(t::Trial)
st = sort(t)
return st[1:(skewcutoff(st) - 1)]
end
trim(t::Trial, percentage = 0.1) = t[1:max(1, floor(Int, length(t) - (length(t) * percentage)))]
#################
# TrialEstimate #
#################
mutable struct TrialEstimate
params::Parameters
time::Float64
gctime::Float64
memory::Int
allocs::Int
end
function TrialEstimate(trial::Trial, t, gct)
return TrialEstimate(params(trial), t, gct, memory(trial), allocs(trial))
end
function Base.:(==)(a::TrialEstimate, b::TrialEstimate)
return a.params == b.params &&
a.time == b.time &&
a.gctime == b.gctime &&
a.memory == b.memory &&
a.allocs == b.allocs
end
Base.copy(t::TrialEstimate) = TrialEstimate(copy(t.params), t.time, t.gctime, t.memory, t.allocs)
function Base.minimum(trial::Trial)
i = argmin(trial.times)
return TrialEstimate(trial, trial.times[i], trial.gctimes[i])
end
function Base.maximum(trial::Trial)
i = argmax(trial.times)
return TrialEstimate(trial, trial.times[i], trial.gctimes[i])
end
Statistics.median(trial::Trial) = TrialEstimate(trial, median(trial.times), median(trial.gctimes))
Statistics.mean(trial::Trial) = TrialEstimate(trial, mean(trial.times), mean(trial.gctimes))
Base.isless(a::TrialEstimate, b::TrialEstimate) = isless(time(a), time(b))
Base.time(t::TrialEstimate) = t.time
gctime(t::TrialEstimate) = t.gctime
memory(t::TrialEstimate) = t.memory
allocs(t::TrialEstimate) = t.allocs
params(t::TrialEstimate) = t.params
##############
# TrialRatio #
##############
mutable struct TrialRatio
params::Parameters
time::Float64
gctime::Float64
memory::Float64
allocs::Float64
end
function Base.:(==)(a::TrialRatio, b::TrialRatio)
return a.params == b.params &&
a.time == b.time &&
a.gctime == b.gctime &&
a.memory == b.memory &&
a.allocs == b.allocs
end
Base.copy(t::TrialRatio) = TrialRatio(copy(t.params), t.time, t.gctime, t.memory, t.allocs)
Base.time(t::TrialRatio) = t.time
gctime(t::TrialRatio) = t.gctime
memory(t::TrialRatio) = t.memory
allocs(t::TrialRatio) = t.allocs
params(t::TrialRatio) = t.params
function ratio(a::Real, b::Real)
if a == b # so that ratio(0.0, 0.0) returns 1.0
return one(Float64)
end
return Float64(a / b)
end
function ratio(a::TrialEstimate, b::TrialEstimate)
ttol = max(params(a).time_tolerance, params(b).time_tolerance)
mtol = max(params(a).memory_tolerance, params(b).memory_tolerance)
p = Parameters(params(a); time_tolerance = ttol, memory_tolerance = mtol)
return TrialRatio(p, ratio(time(a), time(b)), ratio(gctime(a), gctime(b)),
ratio(memory(a), memory(b)), ratio(allocs(a), allocs(b)))
end
gcratio(t::TrialEstimate) = ratio(gctime(t), time(t))
##################
# TrialJudgement #
##################
struct TrialJudgement
ratio::TrialRatio
time::Symbol
memory::Symbol
end
function TrialJudgement(r::TrialRatio)
ttol = params(r).time_tolerance
mtol = params(r).memory_tolerance
return TrialJudgement(r, judge(time(r), ttol), judge(memory(r), mtol))
end
function Base.:(==)(a::TrialJudgement, b::TrialJudgement)
return a.ratio == b.ratio &&
a.time == b.time &&
a.memory == b.memory
end
Base.copy(t::TrialJudgement) = TrialJudgement(copy(t.params), t.time, t.memory)
Base.time(t::TrialJudgement) = t.time
memory(t::TrialJudgement) = t.memory
ratio(t::TrialJudgement) = t.ratio
params(t::TrialJudgement) = params(ratio(t))
judge(a::TrialEstimate, b::TrialEstimate; kwargs...) = judge(ratio(a, b); kwargs...)
function judge(r::TrialRatio; kwargs...)
newr = copy(r)
newr.params = Parameters(params(r); kwargs...)
return TrialJudgement(newr)
end
function judge(ratio::Real, tolerance::Float64)
if isnan(ratio) || (ratio - tolerance) > 1.0
return :regression
elseif (ratio + tolerance) < 1.0
return :improvement
else
return :invariant
end
end
isimprovement(f, t::TrialJudgement) = f(t) == :improvement
isimprovement(t::TrialJudgement) = isimprovement(time, t) || isimprovement(memory, t)
isregression(f, t::TrialJudgement) = f(t) == :regression
isregression(t::TrialJudgement) = isregression(time, t) || isregression(memory, t)
isinvariant(f, t::TrialJudgement) = f(t) == :invariant
isinvariant(t::TrialJudgement) = isinvariant(time, t) && isinvariant(memory, t)
const colormap = (
regression = :red,
improvement = :green,
invariant = :normal,
)
printtimejudge(io, t::TrialJudgement) =
printstyled(io, time(t); color=colormap[time(t)])
printmemoryjudge(io, t::TrialJudgement) =
printstyled(io, memory(t); color=colormap[memory(t)])
###################
# Pretty Printing #
###################
prettypercent(p) = string(@sprintf("%.2f", p * 100), "%")
function prettydiff(p)
diff = p - 1.0
return string(diff >= 0.0 ? "+" : "", @sprintf("%.2f", diff * 100), "%")
end
function prettytime(t)
if t < 1e3
value, units = t, "ns"
elseif t < 1e6
value, units = t / 1e3, "μs"
elseif t < 1e9
value, units = t / 1e6, "ms"
else
value, units = t / 1e9, "s"
end
return string(@sprintf("%.3f", value), " ", units)
end
function prettymemory(b)
if b < 1024
return string(b, " bytes")
elseif b < 1024^2
value, units = b / 1024, "KiB"
elseif b < 1024^3
value, units = b / 1024^2, "MiB"
else
value, units = b / 1024^3, "GiB"
end
return string(@sprintf("%.2f", value), " ", units)
end
function withtypename(f, io, t)
needtype = get(io, :typeinfo, Nothing) !== typeof(t)
if needtype
print(io, nameof(typeof(t)), '(')
end
f()
if needtype
print(io, ')')
end
end
_summary(io, t, args...) = withtypename(() -> print(io, args...), io, t)
Base.summary(io::IO, t::Trial) = _summary(io, t, prettytime(time(t)))
Base.summary(io::IO, t::TrialEstimate) = _summary(io, t, prettytime(time(t)))
Base.summary(io::IO, t::TrialRatio) = _summary(io, t, prettypercent(time(t)))
Base.summary(io::IO, t::TrialJudgement) = withtypename(io, t) do
print(io, prettydiff(time(ratio(t))), " => ")
printtimejudge(io, t)
end
_show(io, t) =
if get(io, :compact, true)
summary(io, t)
else
show(io, MIME"text/plain"(), t)
end
Base.show(io::IO, t::Trial) = _show(io, t)
Base.show(io::IO, t::TrialEstimate) = _show(io, t)
Base.show(io::IO, t::TrialRatio) = _show(io, t)
Base.show(io::IO, t::TrialJudgement) = _show(io, t)
function Base.show(io::IO, ::MIME"text/plain", t::Trial)
if length(t) > 0
min = minimum(t)
max = maximum(t)
med = median(t)
avg = mean(t)
memorystr = string(prettymemory(memory(min)))
allocsstr = string(allocs(min))
minstr = string(prettytime(time(min)), " (", prettypercent(gcratio(min)), " GC)")
maxstr = string(prettytime(time(max)), " (", prettypercent(gcratio(max)), " GC)")
medstr = string(prettytime(time(med)), " (", prettypercent(gcratio(med)), " GC)")
meanstr = string(prettytime(time(avg)), " (", prettypercent(gcratio(avg)), " GC)")
else
memorystr = "N/A"
allocsstr = "N/A"
minstr = "N/A"
maxstr = "N/A"
medstr = "N/A"
meanstr = "N/A"
end
println(io, "BenchmarkTools.Trial: ")
pad = get(io, :pad, "")
println(io, pad, " memory estimate: ", memorystr)
println(io, pad, " allocs estimate: ", allocsstr)
println(io, pad, " --------------")
println(io, pad, " minimum time: ", minstr)
println(io, pad, " median time: ", medstr)
println(io, pad, " mean time: ", meanstr)
println(io, pad, " maximum time: ", maxstr)
println(io, pad, " --------------")
println(io, pad, " samples: ", length(t))
print(io, pad, " evals/sample: ", t.params.evals)
end
function Base.show(io::IO, ::MIME"text/plain", t::TrialEstimate)
println(io, "BenchmarkTools.TrialEstimate: ")
pad = get(io, :pad, "")
println(io, pad, " time: ", prettytime(time(t)))
println(io, pad, " gctime: ", prettytime(gctime(t)), " (", prettypercent(gctime(t) / time(t)),")")
println(io, pad, " memory: ", prettymemory(memory(t)))
print(io, pad, " allocs: ", allocs(t))
end
function Base.show(io::IO, ::MIME"text/plain", t::TrialRatio)
println(io, "BenchmarkTools.TrialRatio: ")
pad = get(io, :pad, "")
println(io, pad, " time: ", time(t))
println(io, pad, " gctime: ", gctime(t))
println(io, pad, " memory: ", memory(t))
print(io, pad, " allocs: ", allocs(t))
end
function Base.show(io::IO, ::MIME"text/plain", t::TrialJudgement)
println(io, "BenchmarkTools.TrialJudgement: ")
pad = get(io, :pad, "")
print(io, pad, " time: ", prettydiff(time(ratio(t))), " => ")
printtimejudge(io, t)
println(io, " (", prettypercent(params(t).time_tolerance), " tolerance)")
print(io, pad, " memory: ", prettydiff(memory(ratio(t))), " => ")
printmemoryjudge(io, t)
println(io, " (", prettypercent(params(t).memory_tolerance), " tolerance)")
end