-
Notifications
You must be signed in to change notification settings - Fork 28
/
runbenchmark.jl
300 lines (259 loc) · 9.98 KB
/
runbenchmark.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""
benchmarkpkg(pkg, [target]::Union{String, BenchmarkConfig}; kwargs...)
Run a benchmark on the package `pkg` using the [`BenchmarkConfig`](@ref) or git identifier `target`.
Examples of git identifiers are commit shas, branch names, or e.g. `"HEAD~1"`.
Return a [`BenchmarkResults`](@ref).
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 to `benchmark/benchmarks.jl` in the package folder.
* `postprocess` - A function to post-process results. Will be passed the `BenchmarkGroup`, which it can modify, or return a new one.
* `resultfile` - If set, saves the output to `resultfile`
* `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`](@ref). If you choose to, you can save the results manually using
[`writeresults`](@ref) where `results` is the return value of this function. It can be read back with [`readresults`](@ref).
**Example invocations:**
```julia
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)
```
"""
function benchmarkpkg end
function benchmarkpkg(
pkg::String,
target=BenchmarkConfig();
script=nothing,
postprocess=nothing,
resultfile=nothing,
retune=false,
verbose::Bool=true,
logger_factory=nothing,
progressoptions=nothing,
custom_loadpath="" #= used in tests =#
)
if progressoptions !== nothing
Base.depwarn(
"Keyword argument `progressoptions` is ignored. Please use `logger_factory`.",
:benchmarkpkg,
)
end
target = BenchmarkConfig(target)
pkgid = Base.identify_package(pkg)
pkgfile_from_pkgname = pkgid === nothing ? nothing : Base.locate_package(pkgid)
if pkgfile_from_pkgname===nothing
if isdir(pkg)
pkgdir = pkg
else
error("No package '$pkg' found.")
end
else
pkgdir = normpath(joinpath(dirname(pkgfile_from_pkgname), ".."))
end
# Locate script
if script === nothing
script = joinpath(pkgdir, "benchmark", "benchmarks.jl")
elseif !isabspath(script)
script = joinpath(pkgdir, script)
end
if !isfile(script)
error("benchmark script at $script not found")
end
# Locate pacakge
tunefile = joinpath(pkgdir, "benchmark", "tune.json")
isgitrepo = ispath(joinpath(pkgdir, ".git"))
if isgitrepo
isdirty = LibGit2.with(LibGit2.isdirty, LibGit2.GitRepo(pkgdir))
original_sha = _shastring(pkgdir, "HEAD")
end
# In this function the package is at the commit we want to benchmark
function do_benchmark()
shastring = begin
if isgitrepo
isdirty ? "dirty" : _shastring(pkgdir, "HEAD")
else
"non gitrepo"
end
end
local results
results_local = _withtemp(tempname()) do f
_benchinfo("Running benchmarks...")
_runbenchmark(script, f, target, tunefile;
retune = retune,
custom_loadpath = custom_loadpath,
runoptions = (verbose = verbose,),
logger_factory = logger_factory)
end
io = IOBuffer(results_local["results"])
seek(io, 0)
resgroup = BenchmarkTools.load(io)[1]
if postprocess != nothing
retval = postprocess(resgroup)
if retval != nothing
resgroup = retval
end
end
juliasha = results_local["juliasha"]
vinfo = results_local["vinfo"]
results = BenchmarkResults(pkg, shastring, resgroup, now(), juliasha, vinfo, target)
return results
end
if target.id !== nothing
if !isgitrepo
error("$pkgdir is not a git repo, cannot benchmark at $(target.id)")
elseif isdirty
error("$pkgdir is dirty. Please commit/stash your ",
"changes before benchmarking a specific commit")
end
results = _withcommit(do_benchmark, LibGit2.GitRepo(pkgdir), target.id)
else
results = do_benchmark()
end
if resultfile != nothing
writeresults(resultfile, results)
_benchinfo("benchmark results written to $resultfile")
end
if isgitrepo
after_sha = _shastring(pkgdir, "HEAD")
if original_sha != after_sha
@warn("Failed to return back to original sha $original_sha, package now at $after_sha")
end
end
return results
end
function benchmarkpkg(pkg::Module, args...; kwargs...)
dir = pathof(pkg)
dir !== nothing || throw(ArgumentError("Module $pkg is not a package"))
pkg_root = dirname(dirname(dir))
benchmarkpkg(pkg_root, args...; kwargs...)
end
"""
objectpath(x) -> (pkg_uuid::Union{String,Nothing}, pkg_name::String, name::Symbol...)
Get the "fullname" of object, prefixed by package ID.
# Examples
```jldoctest
julia> using PkgBenchmark: objectpath
julia> using Logging
julia> objectpath(ConsoleLogger)
("56ddb016-857b-54e1-b83d-db4d58db5568", "Logging", :ConsoleLogger)
```
"""
function objectpath(x)
m = parentmodule(x)
if x === m
pkg = Base.PkgId(x)
uuid = pkg.uuid === nothing ? nothing : string(pkg.uuid)
return (uuid, pkg.name)
else
n = nameof(x)
if !isdefined(m, n)
error("Object `$x` is not accessible as `$m.$n`.")
end
return (objectpath(m)..., n)
end
end
"""
loadobject((pkg_uuid, pkg_name, name...))
Inverse of `objectpath`.
# Examples
```jldoctest
julia> using PkgBenchmark: loadobject
julia> using Logging
julia> loadobject(("56ddb016-857b-54e1-b83d-db4d58db5568", "Logging", :ConsoleLogger)) ===
ConsoleLogger
true
```
"""
loadobject(path) = _loadobject(path...)
function _loadobject(pkg_uuid, pkg_name, fullname...)
pkgid = Base.PkgId(pkg_uuid === nothing ? pkg_uuid : UUID(pkg_uuid), pkg_name)
return foldl(getproperty, fullname, init = Base.require(pkgid))
end
function _runbenchmark(file::String, output::String, benchmarkconfig::BenchmarkConfig, tunefile::String;
retune = false, custom_loadpath = nothing, runoptions = NamedTuple(),
logger_factory = nothing)
_file, _output, _tunefile, _custom_loadpath = map(escape_string, (file, output, tunefile, custom_loadpath))
logger_factory_path = if logger_factory === nothing
# Default to `TerminalLoggers.TerminalLogger`; load via
# `PkgBenchmark` namespace so that users don't have to add it
# separately.
(objectpath(@__MODULE__)..., :TerminalLogger)
else
objectpath(logger_factory)
end
exec_str = isempty(_custom_loadpath) ? "" : "push!(LOAD_PATH, \"$(_custom_loadpath)\")\n"
exec_str *=
"""
using PkgBenchmark
PkgBenchmark._runbenchmark_local(
$(repr(_file)),
$(repr(_output)),
$(repr(_tunefile)),
$(repr(retune)),
$(repr(runoptions)),
$(repr(logger_factory_path)),
)
"""
# Propagate Julia flags passed into the current Julia process
color = if VERSION < v"1.5.0-DEV.576" # https://github.com/JuliaLang/julia/pull/35324
Base.have_color ? `--color=yes` : `--color=no`
else
``
end
juliacmd = benchmarkconfig.juliacmd
juliacmd = `$(Base.julia_cmd(juliacmd[1])) $color $(juliacmd[2:end])`
target_env = [k => v for (k, v) in benchmarkconfig.env]
withenv(target_env...) do
env_to_use = dirname(Pkg.Types.Context().env.project_file)
run(`$juliacmd --project=$env_to_use --depwarn=no -e $exec_str`)
end
return JSON.parsefile(output)
end
function _runbenchmark_local(file, output, tunefile, retune, runoptions, logger_factory_path)
with_logger(loadobject(logger_factory_path)()) do
__runbenchmark_local(file, output, tunefile, retune, runoptions)
end
end
function __runbenchmark_local(file, output, tunefile, retune, runoptions)
# Loading
Base.include(Main, file)
if !isdefined(Main, :SUITE)
error("`SUITE` variable not found, make sure the BenchmarkGroup is named `SUITE`")
end
suite = Main.SUITE
# Tuning
if isfile(tunefile) && !retune
_benchinfo("using benchmark tuning data in $(abspath(tunefile))")
BenchmarkTools.loadparams!(suite, BenchmarkTools.load(tunefile)[1], :evals, :samples);
else
_benchinfo("creating benchmark tuning file $(abspath(tunefile))...")
mkpath(dirname(tunefile))
BenchmarkTools.tune!(suite; runoptions...)
BenchmarkTools.save(tunefile, params(suite));
end
# Running
results = run(suite; runoptions...)
# Output
vinfo = first(split(sprint((io) -> versioninfo(io; verbose=true)), "Environment"))
juliasha = Base.GIT_VERSION_INFO.commit
open(output, "w") do iof
JSON.print(iof, Dict(
"results" => sprint(BenchmarkTools.save, results),
"vinfo" => vinfo,
"juliasha" => juliasha,
))
end
return nothing
end