Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This package extends
macro `@bs` that shows statistical results of the execution, much like the
`@benchmark` macro from
[BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl).
The macro `@bs` supports benchmarking single as well as multiple expressions (see examples below).

In fact, the code is just an adaptation of the BenchmarkTools code.

Expand All @@ -15,6 +16,8 @@ therefore, there is no need to load also Chairmarks.

## Example

### Benchmarking a single expression

```julia-repl
julia> using PrettyChairmarks

Expand All @@ -30,3 +33,31 @@ Chairmarks.Benchmark: 101378 samples with 356 evaluations.

Memory estimate: 144.0 bytes, allocs estimate: 1.
```

### Comparing multiple expressions

```julia-repl
julia> using PrettyChairmarks

julia> bb = @bs 500 rand(_), rand(_^2)
Chairmarks.Benchmark: 888 samples with 1 evaluation.
Range (min … max): 291.000 ns … 1.041 μs ┊ GC (min … max): 0.00% … 0.00%
Time (median): 375.000 ns ┊ GC (median): 0.00%
Time (mean ± σ): 409.350 ns ± 99.203 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
291 ns Histogram: frequency by time 1.14 ms <

Memory estimate: 4.06 KiB, allocs estimate: 3.
Chairmarks.Benchmark: 888 samples with 1 evaluation.
Range (min … max): 124.625 μs … 1.139 ms ┊ GC (min … max): 0.00% … 88.21%
Time (median): 178.646 μs ┊ GC (median): 0.00%
Time (mean ± σ): 204.882 μs ± 107.588 μs ┊ GC (mean ± σ): 4.37% ± 16.51%

▅ █▆▄▁
▁▁▁▁▁▁█▇▅████▇▄▁▁▁▁▁▅▁▅▅▅▆▆▆▇▅▅▅▅▄▆▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▇
291 ns Histogram: log(frequency) by time 1.14 ms <

Memory estimate: 1.91 MiB, allocs estimate: 3.
```
36 changes: 23 additions & 13 deletions src/PrettyChairmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,17 @@ struct PrettyBenchmark
b::Chairmarks.Benchmark
end

postprocess(b::Chairmarks.Benchmark) = PrettyBenchmark(b)
postprocess(bmarks::Tuple{Vararg{Chairmarks.Benchmark}}) = PrettyBenchmark.(bmarks)

macro bs(args...)
call = Chairmarks.process_args(args)
:(PrettyBenchmark($call))
end

macro bcomp(expr...)
call = Chairmarks.process_args(expr)
quote
tuple([PrettyChairmarks.PrettyBenchmark(bnc) for bnc in $call]...)
end
:(postprocess($call))
end

_summary(io, t, args...) = withtypename(() -> print(io, args...), io, t)

Base.summary(io::IO, t::PrettyBenchmark) = _summary(io, t, prettytime(minimum(t -> t.time, bb.b.samples)))
Base.summary(io::IO, t::PrettyBenchmark) = _summary(io, t, prettytime(minimum(t -> t.time, t.b.samples)))

_show(io, t) =
if get(io, :compact, true)
Expand All @@ -110,7 +106,7 @@ _show(io, t) =

Base.show(io::IO, t::PrettyBenchmark) = _show(io, t)

function Base.show(io::IO, ::MIME"text/plain", t1::PrettyBenchmark)
function Base.show(io::IO, ::MIME"text/plain", t1::PrettyBenchmark; histmax::Union{T,Nothing} = nothing, histmin::Union{T,Nothing} = nothing) where {T<:AbstractFloat}
t = t1.b
pad = get(io, :pad, "")
print(
Expand Down Expand Up @@ -229,8 +225,12 @@ function Base.show(io::IO, ::MIME"text/plain", t1::PrettyBenchmark)
histwidth = 42 + lmaxtimewidth + rmaxtimewidth

histtimes = times[1:round(Int, histquantile * end)]
histmin = get(io, :histmin, first(histtimes))
histmax = get(io, :histmax, last(histtimes))
if histmin === nothing
histmin = get(io, :histmin, first(histtimes))
end
if histmax === nothing
histmax = get(io, :histmax, last(histtimes))
end
logbins = get(io, :logbins, nothing)
bins = bindata(histtimes, histwidth - 1, histmin, histmax)
append!(bins, [1, floor((1 - histquantile) * length(times))])
Expand Down Expand Up @@ -310,6 +310,16 @@ function Base.show(io::IO, ::MIME"text/plain", t1::PrettyBenchmark)
return print(io, ".")
end

Base.show(io::IO, m::MIME"text/plain", bmks::Tuple{Vararg{PrettyBenchmark}}) = for b in bmks Base.show(io, m, b) end
function Base.show(io::IO, m::MIME"text/plain", bmks::Tuple{Vararg{PrettyBenchmark}})
# set the min and max for the hist
histquantile = 0.99
_hmin = minimum(t -> minimum(s -> s.time, t.b.samples), bmks)
_hmax = maximum(t -> maximum(sort([s.time for s in t.b.samples])[1:round(Int, histquantile * end)]), bmks)

for b in bmks
show(io, m, b; histmax = _hmax, histmin = _hmin)
print(io, "\n")
end
end

end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ using Test
@test startswith(s, r"Chairmarks\.Benchmark: [0-9]* samples? with [0-9]* evaluations?\.")
@test endswith(s, "allocs estimate: 0.")
@test occursin(r"Histogram: (log\()?frequency\)? by time", s)
bb = @bs sleep(0.001), sleep(0.01); @test (bb isa Tuple)
b1 = @bs sleep(0.001); @test !(b1 isa Tuple)
end
Loading