Skip to content

Commit 53d586b

Browse files
authored
Show trials with 1 sample without error (#234)
* Show trials with 1 sample without error When only a single sample is present, show the info on that one result. Also use plurals appropriately with sample(s) and evaluation(s). * Add test for displaying single-sample trial
1 parent 3233d3f commit 53d586b

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

src/trials.jl

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,12 @@ Base.show(io::IO, t::TrialRatio) = _show(io, t)
339339
Base.show(io::IO, t::TrialJudgement) = _show(io, t)
340340

341341
function Base.show(io::IO, ::MIME"text/plain", t::Trial)
342-
if length(t) > 0
342+
343+
pad = get(io, :pad, "")
344+
print(io, "BenchmarkTools.Trial: ", length(t), " sample", if length(t) > 1 "s" else "" end,
345+
" with ", t.params.evals, " evaluation", if t.params.evals > 1 "s" else "" end ,".\n")
346+
347+
if length(t) > 1
343348
med = median(t)
344349
avg = mean(t)
345350
std = Statistics.std(t)
@@ -354,13 +359,20 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
354359

355360
memorystr = string(prettymemory(memory(min)))
356361
allocsstr = string(allocs(min))
362+
elseif length(t) == 1
363+
print(io, pad, " Single result which took ")
364+
printstyled(io, prettytime(t.times[1]); color=:blue)
365+
print(io, " (", prettypercent(t.gctimes[1]/t.times[1]), " GC) ")
366+
print(io, "to evaluate,\n")
367+
print(io, pad, " with a memory estimate of ")
368+
printstyled(io, prettymemory(t.memory[1]); color=:yellow)
369+
print(io, ", over ")
370+
printstyled(io, t.allocs[1]; color=:yellow)
371+
print(io, " allocations.")
372+
return
357373
else
358-
medtime, medgc = "N/A", "N/A"
359-
avgtime, avggc = "N/A", "N/A"
360-
stdtime, stdgc = "N/A", "N/A"
361-
mintime, mingc = "N/A", "N/A"
362-
maxtime, maxgc = "N/A", "N/A"
363-
memorystr, allocsstr = "N/A", "N/A"
374+
print(io, pad, " No results.")
375+
return
364376
end
365377

366378
lmaxtimewidth = maximum(length.((medtime, avgtime, mintime)))
@@ -370,10 +382,7 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
370382

371383
# Main stats
372384

373-
pad = get(io, :pad, "")
374-
print(io, "BenchmarkTools.Trial: ", length(t), " samples with ", t.params.evals, " evaluations.")
375-
376-
print(io, "\n", pad, " Range ")
385+
print(io, pad, " Range ")
377386
printstyled(io, "("; color=:light_black)
378387
printstyled(io, "min"; color=:cyan, bold=true)
379388
print(io, "")
@@ -434,12 +443,16 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
434443
bins, logbins = log.(1 .+ bins), true
435444
end
436445
hist = asciihist(bins, histheight)
437-
hist[end,end-1] = ' '
446+
hist[:,end-1] .= ' '
438447
maxbin = maximum(bins)
439448

440449
delta1 = (histtimes[end] - histtimes[1]) / (histwidth - 1)
441-
medpos = 1 + round(Int, (histtimes[length(t.times) ÷ 2] - histtimes[1]) / delta1)
442-
avgpos = 1 + round(Int, (mean(t.times) - histtimes[1]) / delta1)
450+
if delta1 > 0
451+
medpos = 1 + round(Int, (histtimes[length(t.times) ÷ 2] - histtimes[1]) / delta1)
452+
avgpos = 1 + round(Int, (mean(t.times) - histtimes[1]) / delta1)
453+
else
454+
medpos, avgpos = 1, 1
455+
end
443456

444457
print(io, "\n")
445458
for histrow in eachrow(hist)

test/TrialsTests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ BenchmarkTools.TrialEstimate:
214214

215215
@test sprint(show, [ta, tb]) == "BenchmarkTools.TrialEstimate[0.490 ns, 1.000 ns]"
216216

217+
trial1sample = BenchmarkTools.Trial(BenchmarkTools.Parameters(), [1], [1], 1, 1)
218+
@test try display(trial1sample); true catch e false end
219+
217220
@static if VERSION < v"1.6-"
218221

219222
@test sprint(show, "text/plain", [ta, tb]) == """

0 commit comments

Comments
 (0)