Skip to content

Commit

Permalink
Make all examples doctests (#13)
Browse files Browse the repository at this point in the history
Excludes the README because they are copied directly from index.md & wouldn't actually run anyway.
  • Loading branch information
LilithHafner authored Feb 23, 2024
1 parent d983f5a commit 662cdce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
```@meta
CurrentModule = Chairmarks
DocTestSetup = quote
using Chairmarks
end
DocTestFilters = [r"\d\d?\d?\.\d{3} [μmn]?s( \(.*\))?"]
```

# Chairmarks
Expand All @@ -10,7 +14,7 @@ CurrentModule = Chairmarks

Capable of detecting 1% difference in runtime in ideal conditions

```julia
```jldoctest
julia> f(n) = sum(rand() for _ in 1:n)
f (generic function with 1 method)
Expand All @@ -37,28 +41,28 @@ julia> @b f(1010)

Chairmarks uses a concise pipeline syntax to define benchmarks. When providing a single argument, that argument is automatically wrapped in a function for higher performance and executed

```julia
```jldoctest
julia> @b sort(rand(100))
1.500 μs (3 allocs: 2.625 KiB)
```

When providing two arguments, the first is setup code and only the runtime of the second is measured

```julia
```jldoctest
julia> @b rand(100) sort
1.018 μs (2 allocs: 1.750 KiB)
```

You may use `_` in the later arguments to refer to the output of previous arguments

```julia
```jldoctest
julia> @b rand(100) sort(_, by=x -> exp(-x))
5.521 μs (2 allocs: 1.750 KiB)
```

A third argument can run a "teardown" function to integrate testing into the benchmark and ensure that the benchmarked code is behaving correctly

```julia
```jldoctest
julia> @b rand(100) sort(_, by=x -> exp(-x)) issorted(_) || error()
ERROR:
Stacktrace:
Expand Down
4 changes: 2 additions & 2 deletions src/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ $(replace(DOCSTRING_BODY, "@bx" => "@b"))
# Examples
```julia
```jldoctest; filter = [r"\\d\\d?\\d?\\.\\d{3} [μmn]?s( \\(.*\\))?"=>s"RES"], setup=(using Random)
julia> @b rand(10000) # Benchmark a function
5.833 μs (2 allocs: 78.172 KiB)
Expand Down Expand Up @@ -130,7 +130,7 @@ $(replace(DOCSTRING_BODY, "@bx" => "@be"))
# Examples
```julia
```jldoctest; filter = [r"\\d\\d?\\d?\\.\\d{3} [μmn]?s( \\(.*\\))?"=>s"RES", r"\\d+ (sample|evaluation)s?"=>s"### \\1"], setup=(using Random)
julia> @be rand(10000) # Benchmark a function
Benchmark: 267 samples with 2 evaluations
min 8.500 μs (2 allocs: 78.172 KiB)
Expand Down

0 comments on commit 662cdce

Please sign in to comment.