Running a benchmark suite
Use benchmarkpkg
to run benchmarks defined in a suite as defined in the previous section.
PkgBenchmark.benchmarkpkg
— Functionbenchmarkpkg(pkg, [target]::Union{String, BenchmarkConfig}; kwargs...)
Run a benchmark on the package pkg
using the BenchmarkConfig
or git identifier target
. Examples of git identifiers are commit shas, branch names, or e.g. "HEAD~1"
. Return a BenchmarkResults
.
The argument pkg
can be the module of a package, a package name, or the path to the package's root directory.
Keyword arguments:
script
- The script with the benchmarks, if not given, defaults tobenchmark/benchmarks.jl
in the package folder.postprocess
- A function to post-process results. Will be passed theBenchmarkGroup
, which it can modify, or return a new one.resultfile
- If set, saves the output toresultfile
retune
- Force a re-tune, saving the new tuning to the tune file.verbose::Bool = true
- Print currently running benchmark.logger_factory
- Specify the logger used during benchmark. It is a callable object (typically a type) with no argument that creates a logger. It must exist as a constant in some package (e.g., an anonymous function does not work).progressoptions
- Deprecated.
The result can be used by functions such as judge
. If you choose to, you can save the results manually using writeresults
where results
is the return value of this function. It can be read back with readresults
.
Example invocations:
using PkgBenchmark
import MyPkg
benchmarkpkg(MyPkg) # run the benchmarks at the current state of the repository
benchmarkpkg(MyPkg, "my-feature") # run the benchmarks for a particular branch/commit/tag
benchmarkpkg(MyPkg, "my-feature"; script="/home/me/mycustombenchmark.jl")
benchmarkpkg(MyPkg, BenchmarkConfig(id = "my-feature",
env = Dict("JULIA_NUM_THREADS" => 4),
juliacmd = `julia -O3`))
benchmarkpkg(MyPkg, # Run the benchmarks and divide the (median of) results by 1000
postprocess=(results)->(results["g"] = median(results["g"])/1_000)
The results of a benchmark is returned as a BenchmarkResult
:
PkgBenchmark.BenchmarkResults
— TypeStores the results from running the benchmarks on a package.
The following (unexported) methods are defined on a BenchmarkResults
(written below as results
):
name(results)::String
- The commit of the package benchmarkedcommit(results)::String
- The commit of the package benchmarked. If the package repository was dirty, the string"dirty"
is returned.juliacommit(results)::String
- The commit of the Julia executable that ran the benchmarksbenchmarkgroup(results)::BenchmarkGroup
- aBenchmarkGroup
contaning the results of the benchmark.date(results)::DateTime
- The time when the benchmarks were executedbenchmarkconfig(results)::BenchmarkConfig
- TheBenchmarkConfig
used for the benchmarks.
BenchmarkResults
can be exported to markdown using the function export_markdown
.
More advanced customization
Instead of passing a commit, branch etc. as a String
to benchmarkpkg
, a BenchmarkConfig
can be passed
PkgBenchmark.BenchmarkConfig
— TypeBenchmarkConfig(;id::Union{String, Nothing} = nothing,
juliacmd::Cmd = `joinpath(Sys.BINDIR, Base.julia_exename())`,
env::Dict{String, Any} = Dict{String, Any}())
A BenchmarkConfig
contains the configuration for the benchmarks to be executed by benchmarkpkg
.
This includes the following:
- The commit of the package the benchmarks are run on.
- What julia command should be run, i.e. the path to the Julia executable and
the command flags used (e.g. optimization level with -O
).
- Custom environment variables (e.g.
JULIA_NUM_THREADS
).
The constructor takes the following keyword arguments:
id
- A git identifier like a commit, branch, tag, "HEAD", "HEAD~1" etc. Ifid == nothing
then benchmark will be done on the current state of the repo (even if it is dirty).juliacmd
- Used to execute the benchmarks, defaults to the julia executable that the Pkgbenchmark-functions are called from. Can also include command flags.env
- Contains custom environment variables that will be active when the benchmarks are run.
Examples
julia> using Pkgbenchmark
julia> BenchmarkConfig(id = "performance_improvements",
juliacmd = `julia -O3`,
env = Dict("JULIA_NUM_THREADS" => 4))
BenchmarkConfig:
id: performance_improvements
juliacmd: `julia -O3`
env: JULIA_NUM_THREADS => 4
This object contains the package commit, julia command, and what environment variables will be used when benchmarking. The default values can be seen by using the default constructor
julia> BenchmarkConfig()
BenchmarkConfig:
id: nothing
juliacmd: `/home/user/julia/julia`
env:
The id
is a commit, branch etc as described in the previous section. An id
with value nothing
means that the current state of the package will be benchmarked. The default value of juliacmd
is joinpath(Sys.BINDIR, Base.julia_exename()
which is the command to run the julia executable without any command line arguments.
To instead benchmark the branch PR
, using the julia command julia -O3
with the environment variable JULIA_NUM_THREADS
set to 4
, the config would be created as
julia> config = BenchmarkConfig(id = "PR",
juliacmd = `julia -O3`,
env = Dict("JULIA_NUM_THREADS" => 4))
BenchmarkConfig:
id: "PR"
juliacmd: `julia -O3`
env: JULIA_NUM_THREADS => 4
To benchmark the package with the config, call benchmarkpkg
as e.g.
benchmark("Tensors", config)
The id
keyword to the BenchmarkConfig
does not have to be a branch, it can be most things that git can understand, for example a commit id or a tag.
Benchmarks can be saved and read using writeresults
and `readresults
respectively:
PkgBenchmark.readresults
— Functionreadresults(file::String)
Reads the BenchmarkResults
stored in file
(given as a path).
PkgBenchmark.writeresults
— Functionwriteresults(file::String, results::BenchmarkResults)
Writes the BenchmarkResults
to file
.