Skip to content

Commit e0b2233

Browse files
authored
Simplify control logic for :logbins (#257)
There wasn't an actual bug, but the logic was so tortured that when I looked back at it, I thought there was. This should be easier on the eyes.
1 parent 4009a7a commit e0b2233

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/trials.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,9 @@ function Base.show(io::IO, ::MIME"text/plain", t::Trial)
446446
bins = bindata(histtimes, histwidth - 1, histmin, histmax)
447447
append!(bins, [1, floor((1-histquantile) * length(times))])
448448
# if median size of (bins with >10% average data/bin) is less than 5% of max bin size, log the bin sizes
449-
if (logbins === nothing || logbins === true) && median(filter(b -> b > 0.1 * length(times) / histwidth, bins)) / maximum(bins) < 0.05
449+
if logbins === true || (logbins === nothing && median(filter(b -> b > 0.1 * length(times) / histwidth, bins)) / maximum(bins) < 0.05)
450450
bins, logbins = log.(1 .+ bins), true
451-
elseif logbins === nothing
451+
else
452452
logbins = false
453453
end
454454
hist = asciihist(bins, histheight)

test/ExecutionTests.jl

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,33 @@ tune!(b)
135135
# test kwargs separated by `,`
136136
@benchmark(output=sin(x), setup=(x=1.0; output=0.0), teardown=(@test output == sin(x)))
137137

138-
io = IOBuffer()
139-
ioctx = IOContext(io, :histmin=>0.5, :histmax=>8, :logbins=>false)
140-
b = @benchmark x^3 setup=(x = rand()); show(ioctx, MIME("text/plain"), b)
141-
b = @benchmark x^3.0 setup=(x = rand()); show(ioctx, MIME("text/plain"), b)
142-
str = String(take!(io))
143-
idx = findfirst(r"0.5 ns +Histogram: frequency by time +8 ns", str)
144-
@test isa(idx, UnitRange)
145-
idx = findnext( r"0.5 ns +Histogram: frequency by time +8 ns", str, idx[end]+1)
146-
@test isa(idx, UnitRange)
138+
for (tf, rex1, rex2) in ((false, r"0.5 ns +Histogram: frequency by time +8 ns", r"Histogram: frequency"),
139+
(true, r"0.5 ns +Histogram: log\(frequency\) by time +8 ns", r"Histogram: log\(frequency\)"))
140+
io = IOBuffer()
141+
ioctx = IOContext(io, :histmin=>0.5, :histmax=>8, :logbins=>tf)
142+
@show tf
143+
b = @benchmark x^3 setup=(x = rand()); show(ioctx, MIME("text/plain"), b)
144+
b = @benchmark x^3.0 setup=(x = rand()); show(ioctx, MIME("text/plain"), b)
145+
str = String(take!(io))
146+
idx = findfirst(rex1, str)
147+
@test isa(idx, UnitRange)
148+
idx = findnext( rex1, str, idx[end]+1)
149+
@test isa(idx, UnitRange)
150+
ioctx = IOContext(io, :logbins=>tf)
151+
# A flat distribution won't trigger log by default
152+
b = BenchmarkTools.Trial(BenchmarkTools.DEFAULT_PARAMETERS, 0.001 * (1:100) * 1e9, zeros(100), 0, 0)
153+
show(ioctx, MIME("text/plain"), b)
154+
str = String(take!(io))
155+
idx = findfirst(rex2, str)
156+
@test isa(idx, UnitRange)
157+
# A peaked distribution will trigger log by default
158+
t = [fill(1, 21); 2]
159+
b = BenchmarkTools.Trial(BenchmarkTools.DEFAULT_PARAMETERS, t/sum(t)*1e9*BenchmarkTools.DEFAULT_PARAMETERS.seconds, zeros(100), 0, 0)
160+
show(ioctx, MIME("text/plain"), b)
161+
str = String(take!(io))
162+
idx = findfirst(rex2, str)
163+
@test isa(idx, UnitRange)
164+
end
147165

148166
#############
149167
# @bprofile #

0 commit comments

Comments
 (0)