Skip to content

Commit cd3fb8e

Browse files
tkfjrevels
authored andcommitted
Improve/fix show methods (#128)
* Improve/fix show methods Previously the logic for switching compact or verbose form of various benchmark result objects are in `show(io::IO, group::BenchmarkGroup)`. A more natural place would be `show(io::IO, t::Trial)` etc. as it let code outside BenchmarkTools to switch the compactness using the standard `IOContext` interface. For example, with this patch `Trial`s in `Vector` are shown in the compact form automatically. This patch also adds: * Colored output for the words `improvement` and `regression`. * `:typeinfo` IO context support * Remove :verbose IOContext
1 parent f0f22b6 commit cd3fb8e

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

src/groups.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,7 @@ function Base.show(io::IO, group::BenchmarkGroup)
258258
for (k, v) in group
259259
println(io)
260260
print(io, pad, " ", repr(k), " => ")
261-
if get(io, :verbose, false) && isa(v, BenchmarkGroup)
262-
show(IOContext(io, :pad => "\t"*pad), v)
263-
elseif get(io, :compact, true)
264-
summary(IOContext(io, :pad => "\t"*pad), v)
265-
else
266-
show(IOContext(io, :pad => "\t"*pad), v)
267-
end
261+
show(IOContext(io, :pad => "\t"*pad), v)
268262
count > get(io, :limit, 10) && (println(io); print(io, pad, ""); break)
269263
count += 1
270264
end

src/trials.jl

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ isimprovement(t::TrialJudgement) = time(t) == :improvement || memory(t) == :impr
225225
isregression(t::TrialJudgement) = time(t) == :regression || memory(t) == :regression
226226
isinvariant(t::TrialJudgement) = time(t) == :invariant && memory(t) == :invariant
227227

228+
const colormap = (
229+
regression = :red,
230+
improvement = :green,
231+
invariant = :normal,
232+
)
233+
234+
printtimejudge(io, t::TrialJudgement) =
235+
printstyled(io, time(t); color=colormap[time(t)])
236+
printmemoryjudge(io, t::TrialJudgement) =
237+
printstyled(io, memory(t); color=colormap[memory(t)])
238+
228239
###################
229240
# Pretty Printing #
230241
###################
@@ -262,12 +273,40 @@ function prettymemory(b)
262273
return string(@sprintf("%.2f", value), " ", units)
263274
end
264275

265-
Base.summary(io::IO, t::Trial) = print(io, "Trial(", prettytime(time(t)), ")")
266-
Base.summary(io::IO, t::TrialEstimate) = print(io, "TrialEstimate(", prettytime(time(t)), ")")
267-
Base.summary(io::IO, t::TrialRatio) = print(io, "TrialRatio(", prettypercent(time(t)), ")")
268-
Base.summary(io::IO, t::TrialJudgement) = print(io, "TrialJudgement(", prettydiff(time(ratio(t))), " => ", time(t), ")")
276+
function withtypename(f, io, t)
277+
needtype = get(io, :typeinfo, Nothing) !== typeof(t)
278+
if needtype
279+
print(io, nameof(typeof(t)), '(')
280+
end
281+
f()
282+
if needtype
283+
print(io, ')')
284+
end
285+
end
286+
287+
_summary(io, t, args...) = withtypename(() -> print(io, args...), io, t)
288+
289+
Base.summary(io::IO, t::Trial) = _summary(io, t, prettytime(time(t)))
290+
Base.summary(io::IO, t::TrialEstimate) = _summary(io, t, prettytime(time(t)))
291+
Base.summary(io::IO, t::TrialRatio) = _summary(io, t, prettypercent(time(t)))
292+
Base.summary(io::IO, t::TrialJudgement) = withtypename(io, t) do
293+
print(io, prettydiff(time(ratio(t))), " => ")
294+
printtimejudge(io, t)
295+
end
296+
297+
_show(io, t) =
298+
if get(io, :compact, true)
299+
summary(io, t)
300+
else
301+
show(io, MIME"text/plain"(), t)
302+
end
303+
304+
Base.show(io::IO, t::Trial) = _show(io, t)
305+
Base.show(io::IO, t::TrialEstimate) = _show(io, t)
306+
Base.show(io::IO, t::TrialRatio) = _show(io, t)
307+
Base.show(io::IO, t::TrialJudgement) = _show(io, t)
269308

270-
function Base.show(io::IO, t::Trial)
309+
function Base.show(io::IO, ::MIME"text/plain", t::Trial)
271310
if length(t) > 0
272311
min = minimum(t)
273312
max = maximum(t)
@@ -301,7 +340,7 @@ function Base.show(io::IO, t::Trial)
301340
print(io, pad, " evals/sample: ", t.params.evals)
302341
end
303342

304-
function Base.show(io::IO, t::TrialEstimate)
343+
function Base.show(io::IO, ::MIME"text/plain", t::TrialEstimate)
305344
println(io, "BenchmarkTools.TrialEstimate: ")
306345
pad = get(io, :pad, "")
307346
println(io, pad, " time: ", prettytime(time(t)))
@@ -310,7 +349,7 @@ function Base.show(io::IO, t::TrialEstimate)
310349
print(io, pad, " allocs: ", allocs(t))
311350
end
312351

313-
function Base.show(io::IO, t::TrialRatio)
352+
function Base.show(io::IO, ::MIME"text/plain", t::TrialRatio)
314353
println(io, "BenchmarkTools.TrialRatio: ")
315354
pad = get(io, :pad, "")
316355
println(io, pad, " time: ", time(t))
@@ -319,9 +358,13 @@ function Base.show(io::IO, t::TrialRatio)
319358
print(io, pad, " allocs: ", allocs(t))
320359
end
321360

322-
function Base.show(io::IO, t::TrialJudgement)
361+
function Base.show(io::IO, ::MIME"text/plain", t::TrialJudgement)
323362
println(io, "BenchmarkTools.TrialJudgement: ")
324363
pad = get(io, :pad, "")
325-
println(io, pad, " time: ", prettydiff(time(ratio(t))), " => ", time(t), " (", prettypercent(params(t).time_tolerance), " tolerance)")
326-
print(io, pad, " memory: ", prettydiff(memory(ratio(t))), " => ", memory(t), " (", prettypercent(params(t).memory_tolerance), " tolerance)")
364+
print(io, pad, " time: ", prettydiff(time(ratio(t))), " => ")
365+
printtimejudge(io, t)
366+
println(io, " (", prettypercent(params(t).time_tolerance), " tolerance)")
367+
print(io, pad, " memory: ", prettydiff(memory(ratio(t))), " => ")
368+
printmemoryjudge(io, t)
369+
println(io, " (", prettypercent(params(t).memory_tolerance), " tolerance)")
327370
end

test/TrialsTests.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,25 @@ tj_r_2 = judge(tr; time_tolerance = 2.0, memory_tolerance = 2.0)
169169
@test BenchmarkTools.prettymemory(1073741823) == "1024.00 MiB"
170170
@test BenchmarkTools.prettymemory(1073741824) == "1.00 GiB"
171171

172+
@test sprint(show, "text/plain", ta) == sprint(show, ta; context=:compact => false) == """
173+
BenchmarkTools.TrialEstimate:
174+
time: 0.490 ns
175+
gctime: 0.000 ns (0.00%)
176+
memory: 2 bytes
177+
allocs: 1"""
178+
179+
@test sprint(show, ta) == "TrialEstimate(0.490 ns)"
180+
@test sprint(
181+
show, ta;
182+
context = IOContext(
183+
devnull, :compact => true, :typeinfo => BenchmarkTools.TrialEstimate)
184+
) == "0.490 ns"
185+
186+
@test sprint(show, [ta, tb]) == "BenchmarkTools.TrialEstimate[0.490 ns, 1.000 ns]"
187+
188+
@test sprint(show, "text/plain", [ta, tb]) == """
189+
2-element Array{BenchmarkTools.TrialEstimate,1}:
190+
0.490 ns
191+
1.000 ns"""
192+
172193
end # module

0 commit comments

Comments
 (0)