Skip to content
This repository was archived by the owner on Jul 4, 2023. It is now read-only.

Commit e887ddb

Browse files
authored
More tests and some fixes (#5)
* add BenchmarkTools' license * add some tests, tweak wording * test empty benchmark case
1 parent 85813c3 commit e887ddb

File tree

5 files changed

+97
-14
lines changed

5 files changed

+97
-14
lines changed

LICENSE

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Eric Hanson <[email protected]> and contributors
3+
Copyright (c) 2021 Eric P. Hanson and contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -19,3 +19,27 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.
22+
23+
The file `src/vendor.jl` contains code taken from BenchmarkTools.jl, which
24+
is available under the following MIT license:
25+
26+
> Copyright (c) 2015: Jarrett Revels.
27+
>
28+
> Permission is hereby granted, free of charge, to any person obtaining
29+
> a copy of this software and associated documentation files (the
30+
> "Software"), to deal in the Software without restriction, including
31+
> without limitation the rights to use, copy, modify, merge, publish,
32+
> distribute, sublicense, and/or sell copies of the Software, and to
33+
> permit persons to whom the Software is furnished to do so, subject to
34+
> the following conditions:
35+
>
36+
> The above copyright notice and this permission notice shall be
37+
> included in all copies or substantial portions of the Software.
38+
>
39+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40+
> EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41+
> MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42+
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
43+
> CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44+
> TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45+
> SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ Wraps [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl/) to prov
77

88
This means one should not call `using` on both BenchmarkHistograms and BenchmarkTools in the same namespace, or else these `@benchmark` macros will conflict ("WARNING: using `BenchmarkTools.@benchmark` in module Main conflicts with an existing identifier.")
99

10-
However, BenchmarkHistograms re-exports all the export of BenchmarkTools, so you can simply call `using BenchmarkHistograms`.
10+
However, BenchmarkHistograms re-exports all of BenchmarkTools (including the module `BenchmarkTools` itself), so you can simply call `using BenchmarkHistograms` instead.
1111

1212
Providing this functionality in BenchmarkTools itself was discussed in <https://github.com/JuliaCI/BenchmarkTools.jl/pull/180>.
1313

14-
Use the setting `BenchmarkHistograms.NBINS[] = 10` to change the number of histogram bins used.
14+
Use the setting `BenchmarkHistograms.NBINS[]` to change the number of histogram bins used, e.g.
15+
```julia
16+
BenchmarkHistograms.NBINS[] = 10
17+
```
18+
to use 10 bins.
1519

1620
## Example
1721

src/BenchmarkHistograms.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct BenchmarkHistogram
2929
end
3030

3131
# borrowed some from `show` implementation for `BenchmarkTools.Trial`
32-
function Base.show(io::IO, ::MIME"text/plain", bp::BenchmarkHistogram)
32+
function Base.show(io::IO, ::MIME"text/plain", bp::BenchmarkHistogram; nbins=NBINS[])
3333
t = bp.trial
3434
if length(t) > 0
3535
min = minimum(t)
@@ -51,10 +51,11 @@ function Base.show(io::IO, ::MIME"text/plain", bp::BenchmarkHistogram)
5151
meanstr = "N/A"
5252
end
5353
println(io, "samples: ", length(t), "; evals/sample: ", t.params.evals, "; memory estimate: ", memorystr, "; allocs estimate: ", allocsstr)
54-
55-
bin_arg = NBINS[] <= 0 ? NamedTuple() : (; nbins=NBINS[])
56-
show(io, histogram(t.times; ylabel="ns", xlabel="Counts", bin_arg...))
57-
println(io)
54+
if length(t) > 0
55+
bin_arg = nbins <= 0 ? NamedTuple() : (; nbins=nbins)
56+
show(io, histogram(t.times; ylabel="ns", xlabel="Counts", bin_arg...))
57+
println(io)
58+
end
5859
print(io, "min: ", minstr, "; mean: ", meanstr, "; median: ", medstr, "; max: ", maxstr, ".")
5960
end
6061

test/runtests.jl

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using BenchmarkHistograms
22
using Test
3+
import BenchmarkTools
34

4-
function tests()
5+
6+
function counting_tests(nbins=nothing)
57
bp = @benchmark 1+1
68
output = sprint(show, MIME"text/plain"(), bp)
79

@@ -26,19 +28,52 @@ function tests()
2628
return nothing
2729
end
2830

29-
function tests(nbins)
31+
32+
function empty_test()
33+
bp = @benchmark 1+1
34+
empty!(bp.trial.times)
35+
output = sprint(show, MIME"text/plain"(), bp)
36+
37+
# Don't want to test the exact string since the stats will
38+
# fluctuate. So let's just test that it contains the right
39+
# number of the right things, and assume they're arranged properly.
40+
n_matches = r -> length(collect(eachmatch(r, output)))
41+
42+
@test n_matches(r"samples:") == 1
43+
@test n_matches(r"evals/sample:") == 1
44+
@test n_matches(r"memory estimate:") == 1
45+
@test n_matches(r"allocs estimate:") == 1
46+
@test n_matches(r"ns") == 0
47+
@test n_matches(r"Counts") == 0
48+
@test n_matches(r"min") == n_matches(r"mean") == n_matches(r"median") == n_matches(r"max") == 1
49+
@test n_matches(r"% GC") == 0
50+
@test n_matches(r"") == n_matches(r"") == n_matches(r"") == n_matches(r"") == 0
51+
return nothing
52+
end
53+
54+
function with_bins(f, nbins)
3055
pre = BenchmarkHistograms.NBINS[]
3156
BenchmarkHistograms.NBINS[] = nbins
3257
try
33-
tests()
58+
f(nbins)
3459
finally
3560
BenchmarkHistograms.NBINS[] = pre
3661
end
3762
return nothing
3863
end
3964

4065
@testset "BenchmarkHistograms.jl" begin
41-
tests()
42-
tests(10)
43-
tests(-1)
66+
67+
@testset "Exports" begin
68+
@test symdiff(names(BenchmarkTools), names(BenchmarkHistograms)) == [:BenchmarkHistograms]
69+
end
70+
71+
@testset "Counting tests" begin
72+
counting_tests()
73+
with_bins(counting_tests, 10)
74+
with_bins(counting_tests, -1)
75+
empty_test()
76+
end
4477
end
78+
79+
include("vendor.jl")

test/vendor.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# taken from <https://github.com/JuliaCI/BenchmarkTools.jl/blob/e058ff249215671c196f2c24a0a3f401de27b718/test/TrialsTests.jl#L177-L199>
2+
3+
@testset "Pretty printing" begin
4+
@test BenchmarkHistograms.prettypercent(.3120123) == "31.20%"
5+
6+
@test BenchmarkHistograms.prettytime(999) == "999.000 ns"
7+
@test BenchmarkHistograms.prettytime(1000) == "1.000 μs"
8+
@test BenchmarkHistograms.prettytime(999_999) == "999.999 μs"
9+
@test BenchmarkHistograms.prettytime(1_000_000) == "1.000 ms"
10+
@test BenchmarkHistograms.prettytime(999_999_999) == "1000.000 ms"
11+
@test BenchmarkHistograms.prettytime(1_000_000_000) == "1.000 s"
12+
13+
@test BenchmarkHistograms.prettymemory(1023) == "1023 bytes"
14+
@test BenchmarkHistograms.prettymemory(1024) == "1.00 KiB"
15+
@test BenchmarkHistograms.prettymemory(1048575) == "1024.00 KiB"
16+
@test BenchmarkHistograms.prettymemory(1048576) == "1.00 MiB"
17+
@test BenchmarkHistograms.prettymemory(1073741823) == "1024.00 MiB"
18+
@test BenchmarkHistograms.prettymemory(1073741824) == "1.00 GiB"
19+
end

0 commit comments

Comments
 (0)