Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round display even on old versions to simplify tests #18

Merged
merged 6 commits into from
Feb 24, 2024
Merged
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
8 changes: 7 additions & 1 deletion src/compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
iterate(x, i) === nothing || throw(ArgumentError("Expected only one element"))
ret
end
end
end
@static if VERSION < v"1.6"
writefixed(x, digits) = rpad(string(round(x, digits=digits, RoundNearestTiesAway)),

Check warning on line 26 in src/compat.jl

View check run for this annotation

Codecov / codecov/patch

src/compat.jl#L26

Added line #L26 was not covered by tests
digits+2+max(0,floor(Int, log10(abs(x))))+(x<0), '0')
else
const writefixed = Base.Ryu.writefixed
end
2 changes: 1 addition & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function print_rounded(@nospecialize(io::IO), x::Float64, digits::Int)
elseif 0 < x < 1/10^digits
print(io, "<0.", '0'^(digits-1), "1")
else
print(io, VERSION < v"1.6" ? string(x) : Base.Ryu.writefixed(x, digits))
print(io, writefixed(x, digits))
end
end
function print_time(io, seconds::Float64)
Expand Down
103 changes: 56 additions & 47 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,34 @@ using Chairmarks: Sample, Benchmark
@test_throws UndefKeywordError Sample(allocs=1.5, bytes=1729) # needs `time`
end

@testset "writefixed" begin
@test Chairmarks.writefixed(-1.23045, 4) == "-1.2305"
@test Chairmarks.writefixed(-1.23045, 3) == "-1.230"
@test Chairmarks.writefixed(1.23045, 6) == "1.230450"
@test Chairmarks.writefixed(10.0, 1) == "10.0"
@test Chairmarks.writefixed(11.0, 1) == "11.0"
@test Chairmarks.writefixed(0.5, 1) == "0.5"
@test Chairmarks.writefixed(0.005, 1) == "0.0"
@test Chairmarks.writefixed(0.005, 2) == "0.01"
@test Chairmarks.writefixed(0.005, 3) == "0.005"
@test Chairmarks.writefixed(0.005, 4) == "0.0050"
@test Chairmarks.writefixed(-0.005, 1) == "-0.0"
@test Chairmarks.writefixed(-0.005, 2) == "-0.01"
@test Chairmarks.writefixed(-0.005, 3) == "-0.005"
@test Chairmarks.writefixed(-0.005, 4) == "-0.0050"
end

@testset "display" begin

# Basic
x = Sample(evals=20076, time=2.822275353656107e-10)
@test repr(x) == "Sample(evals=20076, time=2.822275353656107e-10)"
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == (VERSION < v"1.6" ? "0.28222753536561074 ns" : "0.282 ns")
@test sprint(show, MIME"text/plain"(), x) == "0.282 ns"

x = Sample(time=1.013617427, allocs=30354, bytes=2045496, compile_fraction=0.01090194061945622, recompile_fraction=0.474822474626834, warmup=0)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == (VERSION < v"1.6" ?
"1.014 s (30354 allocs: 1.951 MiB, 1.0901940619456219% compile time 47.482247462683404% of which was recompilation, without a warmup)" :
"1.014 s (30354 allocs: 1.951 MiB, 1.09% compile time 47.48% of which was recompilation, without a warmup)")
@test sprint(show, MIME"text/plain"(), x) == "1.014 s (30354 allocs: 1.951 MiB, 1.09% compile time 47.48% of which was recompilation, without a warmup)"

x = Benchmark([
Sample(time=0.10223923, allocs=166, bytes=16584)
Expand Down Expand Up @@ -101,50 +116,44 @@ using Chairmarks: Sample, Benchmark
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "0 ns"

# Fractional allocs
x = Sample(time=0.006083914078095797, allocs=1.5, bytes=1729)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (1.50 allocs: 1.688 KiB)"

if VERSION >= v"1.6"
# In earlier versions we don't round digits so all these tests would need
# to be specially adapted. But I don't care taht much about pre-1.6.

# Fractional allocs
x = Sample(time=0.006083914078095797, allocs=1.5, bytes=1729)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (1.50 allocs: 1.688 KiB)"

# Few bytes
x = Sample(time=0.006083914078095797, allocs=.5, bytes=0.5)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (0.50 allocs: 0.500 bytes)"
x = Sample(time=0.006083914078095797, allocs=.5, bytes=5)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (0.50 allocs: 5 bytes)"

# GC time
x = Sample(time=0.0019334290000000002, allocs=2, bytes=800048, gc_fraction=0.9254340345572555)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "1.933 ms (2 allocs: 781.297 KiB, 92.54% gc time)"

# Non-integral warmup
x = Sample(time=0.006083914078095797, warmup=0)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (without a warmup)"
x = Sample(time=0.006083914078095797, warmup=0.5)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (50.0% warmed up)"
x = Sample(time=0.006083914078095797, warmup=1)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms"

x = Benchmark([
Sample(time=0.1, evals=2)
Sample(time=0.1)
])
@test eval(Meta.parse(repr(x))).data == x.data
@test sprint(show, MIME"text/plain"(), x) == """
Benchmark: 2 samples with variable evaluations
100.000 ms
100.000 ms"""
end
# Few bytes
x = Sample(time=0.006083914078095797, allocs=.5, bytes=0.5)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (0.50 allocs: 0.500 bytes)"
x = Sample(time=0.006083914078095797, allocs=.5, bytes=5)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (0.50 allocs: 5 bytes)"

# GC time
x = Sample(time=0.0019334290000000002, allocs=2, bytes=800048, gc_fraction=0.9254340345572555)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "1.933 ms (2 allocs: 781.297 KiB, 92.54% gc time)"

# Non-integral warmup
x = Sample(time=0.006083914078095797, warmup=0)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (without a warmup)"
x = Sample(time=0.006083914078095797, warmup=0.5)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms (50.0% warmed up)"
x = Sample(time=0.006083914078095797, warmup=1)
@test eval(Meta.parse(repr(x))) === x
@test sprint(show, MIME"text/plain"(), x) == "6.084 ms"

x = Benchmark([
Sample(time=0.1, evals=2)
Sample(time=0.1)
])
@test eval(Meta.parse(repr(x))).data == x.data
@test sprint(show, MIME"text/plain"(), x) == """
Benchmark: 2 samples with variable evaluations
100.000 ms
100.000 ms"""
end
end

Expand Down
Loading