Skip to content

Commit bb867bf

Browse files
committed
tidy + tests
1 parent d8757b4 commit bb867bf

File tree

2 files changed

+100
-30
lines changed

2 files changed

+100
-30
lines changed

src/trials.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ function withtypename(f, io, t)
297297
end
298298
end
299299

300-
function bindata(data, fences::AbstractRange)
300+
function histogram_bindata(data, fences::AbstractRange)
301301
@assert step(fences) > 0
302302
bins = zeros(Int, length(fences))
303303
for t in data
@@ -309,8 +309,6 @@ function bindata(data, fences::AbstractRange)
309309
bins
310310
end
311311

312-
# @test bindata([1.1, 3.1, 99, -99], 1:3) == [1,0,2]
313-
314312
function asciihist(bins, height=1)
315313
histbars = ['', '', '', '', '', '', '', '']
316314
if minimum(bins) == 0
@@ -324,6 +322,15 @@ function asciihist(bins, height=1)
324322
map(height -> if height < 1; ' ' else histbars[height] end, heightmatrix)
325323
end
326324

325+
function hist_round_low(times, lo=minimum(times), av=mean(times))
326+
_min = min(lo, av / 1.03) # demand low edge 3% from mean, or further
327+
return round(_min, RoundDown; sigdigits = 2)
328+
end
329+
function hist_round_high(times, av=mean(times), hi=quantile(times, 0.99))
330+
_max = max(1, hi, 1.03 * av) # demand high edge 3% above mean, and at least 1ns
331+
return round(_max, RoundUp; sigdigits = 2)
332+
end
333+
327334
_summary(io, t, args...) = withtypename(() -> print(io, args...), io, t)
328335

329336
Base.summary(io::IO, t::Trial) = _summary(io, t, prettytime(time(t)))
@@ -404,7 +411,8 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
404411
return
405412
end # done with trivial cases.
406413

407-
# Main text block:
414+
# Main text block
415+
408416
printstyled(io, "", modulestr, "Trial:\n"; color=boxcolor)
409417

410418
printstyled(io, pad, "", boxspace; color=boxcolor)
@@ -424,7 +432,7 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
424432
print(io, ", ")
425433
print(io, showpercentile, "ᵗʰ ")
426434
quantime = quantile(t.times, showpercentile/100)
427-
printstyled(prettytime(quantime); bold=true)
435+
printstyled(io, prettytime(quantime); bold=true)
428436
println(io)
429437

430438
printstyled(io, pad, "", boxspace; color=boxcolor)
@@ -457,13 +465,13 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
457465
# This should fit it within your terminal, but stops growing at 90 columns. Below about
458466
# 55 columns it will stop shrinking, by which point the first line has already wrapped.
459467

460-
histmin = get(io, :histmin, low_edge(t.times, mintime, avgtime))
461-
histmax = get(io, :histmax, high_edge(t.times, avgtime, quantime))
468+
histmin = get(io, :histmin, hist_round_low(t.times, mintime, avgtime))
469+
histmax = get(io, :histmax, hist_round_high(t.times, avgtime, quantime))
462470

463471
# Here nextfloat() ensures both endpoints included, will only matter for
464472
# artificial cases such as: Trial(Parameters(), [3,4,5], [0,0,0], 0, 0)
465473
fences = range(histmin, nextfloat(histmax), length=histwidth)
466-
bins = bindata(t.times, fences)
474+
bins = histogram_bindata(t.times, fences)
467475
# Last bin is everything right of last fence, introduce a gap for printing:
468476
_lastbin = pop!(bins)
469477
push!(bins, 0, _lastbin)
@@ -536,18 +544,6 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
536544
# printstyled(io, "●", color=:light_black) # other options "⋯" "¹⁰⁰"
537545
end
538546

539-
# I wondered about rounding to a few steps per decade. This looks good in 1:10, not great outside:
540-
# round.(0:0.01:10, sigdigits=3, base=2) |> unique
541-
function low_edge(times, lo=minimum(times), av=mean(times))
542-
_min = min(lo, av / 1.03) # demand low edge 3% from mean, or further
543-
return round(_min, RoundDown; sigdigits = 2)
544-
545-
end
546-
function high_edge(times, av=mean(times), hi=quantile(times, 0.99))
547-
_max = max(1, hi, 1.03 * av) # demand high edge 3% above mean, and at least 1ns
548-
return round(_max, RoundUp; sigdigits = 2)
549-
end
550-
551547
function Base.show(io::IO, ::MIME"text/plain", t::TrialEstimate)
552548
println(io, "BenchmarkTools.TrialEstimate: ")
553549
pad = get(io, :pad, "")

test/TrialsTests.jl

Lines changed: 84 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ tj_r_2 = judge(tr; time_tolerance = 2.0, memory_tolerance = 2.0)
198198
@test BenchmarkTools.prettymemory(1073741823) == "1024.00 MiB"
199199
@test BenchmarkTools.prettymemory(1073741824) == "1.00 GiB"
200200

201+
@test BenchmarkTools.prettycount(10) == "10"
202+
@test BenchmarkTools.prettycount(1023) == "1_023"
203+
@test BenchmarkTools.prettycount(40560789) == "40_560_789"
204+
201205
@test sprint(show, "text/plain", ta) == sprint(show, ta; context=:compact => false) == """
202206
BenchmarkTools.TrialEstimate:
203207
time: 0.490 ns
@@ -233,17 +237,87 @@ else
233237

234238
end
235239

236-
trial = BenchmarkTools.Trial(BenchmarkTools.Parameters(), [1.0, 1.01], [0.0, 0.0], 0, 0)
237-
@test sprint(show, "text/plain", trial) == """
238-
BenchmarkTools.Trial: 2 samples with 1 evaluation.
239-
Range (min … max): 1.000 ns … 1.010 ns ┊ GC (min … max): 0.00% … 0.00%
240-
Time (median): 1.005 ns ┊ GC (median): 0.00%
241-
Time (mean ± σ): 1.005 ns ± 0.007 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
240+
t0 = BenchmarkTools.Trial(BenchmarkTools.Parameters(), [], [], 0, 0)
241+
@test sprint(show, "text/plain", t0) == "Trial: 0 samples"
242+
243+
t001 = BenchmarkTools.Trial(BenchmarkTools.Parameters(), [pi * 10^6], [0], 0, 0)
244+
s001 = sprint(show, "text/plain", t001)
245+
@test contains(s001, "┌ Trial:") # box starting at the type
246+
@test contains(s001, "│ time 3.142 ms")
247+
@test contains(s001, "│ 0 allocations\n") # doesn't print 0 bytes after this
248+
@test contains(s001, "└ 1 sample, with 1 evaluation")
249+
250+
@test BenchmarkTools.asciihist([1,2,3]) == ['' '' '']
251+
@test BenchmarkTools.asciihist([1,2,0,3], 2) == [' ' '' ' ' ''; '' '' '' '']
252+
253+
@test BenchmarkTools.histogram_bindata([1.1, 3.1, 99], 1:3) == [1,0,2]
254+
@test BenchmarkTools.histogram_bindata([1.1, -99, 3.1], 1:3.0) == [1,0,1]
255+
256+
t003 = BenchmarkTools.Trial(BenchmarkTools.Parameters(), [0.01, 0.02, 0.04], [0,0,0], 0, 0)
257+
s003 = sprint(show, "text/plain", t003)
258+
@test contains(s003, " 1 ns +") # right limit is 1ns
259+
@test contains(s003, "min 0.010 ns, median 0.020 ns, mean 0.023 ns, 99ᵗʰ 0.040 ns")
260+
261+
@test sprint(show, t001) == "Trial(3.142 ms)"
262+
@test sprint(show, t003) == "Trial(0.010 ns)"
263+
@test sprint(show, "text/plain", [t001, t003]) == "2-element Vector{BenchmarkTools.Trial}:\n 3.142 ms\n 0.010 ns"
264+
@test_skip sprint(show, "text/plain", [t0]) == "1-element Vector{BenchmarkTools.Trial}:\n ??"
265+
266+
t123 = BenchmarkTools.Trial(BenchmarkTools.Parameters(), [1,2,3.], [0,0,0.], 0, 0)
267+
s123 = sprint(show, "text/plain", t123)
268+
@test contains(s123, "min 1.000 ns, median 2.000 ns, mean 2.000 ns")
269+
@test contains(s123, " 0 allocations\n") # doesn't print 0 bytes after this
270+
@test contains(s123, " ◑* ") # median ◑ is shifted left
271+
@test contains(s123, "▁▁█▁▁") # has a histogram, mostly zero
272+
@test contains(s123, "▁▁▁█ ▁\n") # 3.0 fits in last bin, not the overflow
273+
@test endswith(s123, "3 ns +") # right endpoint rounded to 3, no decimals
274+
@test contains(s123, "┌ Trial:") # box starting at the type
275+
@test contains(s123, "3 samples, each 1 evaluation") # caption
276+
277+
t456 = BenchmarkTools.Trial(BenchmarkTools.Parameters(), 100 * [1,1,3,14,16.], [0,0,2,0,0.], 456, 7)
278+
s456 = sprint(show, "text/plain", t456)
279+
@test contains(s456, "7 allocations, total 456 bytes")
280+
@test contains(s456, "GC time: mean 0.400 ns (0.06%), max 2.000 ns (0.67%)")
281+
@test contains(s456, "│ ◔ ") # 1st quartile lines up with bar
282+
@test contains(s456, "│ █▁▁▁▁▁▁▁")
283+
@test contains(s456, "└ 100 ns ") # box closing + left endpoint without decimals
284+
285+
#=
286+
287+
# Some visual histogram checks, in which mean/median should highlight a bar, or not:
288+
289+
using BenchmarkTools: Trial, Parameters
290+
Trial(Parameters(), [pi * 10^9], [0], 0, 0) # one sample
291+
292+
# mean == median, one bar. Symbol for median moves to the left.
293+
Trial(Parameters(), [pi, pi], [0, 0], 0, 0)
294+
Trial(Parameters(), fill(101, 33), vcat(zeros(32), 50), 0, 0)
295+
296+
# mean == median, three bars
297+
Trial(Parameters(), [3,4,5], [0,0,0], 0, 0)
298+
299+
# three bars, including mean not median
300+
Trial(Parameters(), pi * [1,3,4,4], [0,0,0,100], 1, 1)
301+
302+
# three bars, including median & both quartiles, but not mean
303+
Trial(Parameters(), 99.9 * [1,1,3,14,16], [0,0,99,0,0], 222, 2)
304+
305+
# same, but smaller range. Note also max GC is not max time.
306+
Trial(Parameters(), 999 .+ [1,1,3,14,16], [0,0,123,0,0], 45e6, 7)
307+
308+
309+
# Check that auto-sizing stops on very small widths:
310+
io = IOContext(stdout, :displaysize => (25,30))
311+
show(io, MIME("text/plain"), Trial(Parameters(), [3,4,5], [0,0,0], 0, 0))
312+
show(io, MIME("text/plain"), Trial(Parameters(), repeat(100 * [3,4,5], 10^6), zeros(3*10^6), 0, 0))
313+
314+
io = IOContext(stdout, :displaysize => (25,50), :logbins => true) # this is wider
315+
show(io, MIME("text/plain"), Trial(Parameters(), 100 * [3,4,5], [0,0,0], 0, 0))
242316
243-
█ █
244-
█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█ ▁
245-
1 ns Histogram: frequency by time 1.01 ns <
317+
# Check that data off the left is OK, and median still highlighted:
318+
io = IOContext(stdout, :histmin => 200.123)
319+
show(io, MIME("text/plain"), Trial(Parameters(), 99.9 * [1,1,3,14,16], [0,0,99,0,0], 222, 2))
246320
247-
Memory estimate: 0 bytes, allocs estimate: 0."""
321+
=#
248322

249323
end # module

0 commit comments

Comments
 (0)