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

warm up at the beginning of every measurement #330

Merged
merged 17 commits into from
Feb 27, 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
34 changes: 25 additions & 9 deletions src/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ end
# Note that trials executed via `run` and `lineartrial` are always executed at top-level
# scope, in order to allow transfer of locally-scoped variables into benchmark scope.

function _run(b::Benchmark, p::Parameters; verbose=false, pad="", kwargs...)
function _run(b::Benchmark, p::Parameters; verbose=false, pad="", warmup=true, kwargs...)
params = Parameters(p; kwargs...)
@assert params.seconds > 0.0 "time limit must be greater than 0.0"
params.gctrial && gcscrub()
start_time = Base.time()
trial = Trial(params)
if warmup
b.samplefunc(b.quote_vals, Parameters(params; evals=1)) #warmup sample
end
params.gcsample && gcscrub()
s = b.samplefunc(b.quote_vals, params)
push!(trial, s[1:(end - 1)]...)
Expand Down Expand Up @@ -180,6 +183,8 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
params = Parameters(p; kwargs...)
estimates = zeros(maxevals)
completed = 0
params.evals = 1
b.samplefunc(b.quote_vals, params) #warmup sample
params.gctrial && gcscrub()
start_time = time()
for evals in eachindex(estimates)
Expand All @@ -193,6 +198,10 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
end

function warmup(item; verbose::Bool=true)
Base.depwarn(
"`warmup` is deprecated because `run` now warms up every time automatically",
:warmup,
)
return run(item; verbose=verbose, samples=1, evals=1, gctrial=false, gcsample=false)
end

Expand Down Expand Up @@ -288,7 +297,6 @@ function tune!(
kwargs...,
)
if !p.evals_set
warmup(b; verbose=false)
estimate = ceil(Int, minimum(lineartrial(b, p; kwargs...)))
b.params.evals = guessevals(estimate)
end
Expand Down Expand Up @@ -436,9 +444,8 @@ macro benchmark(args...)
return esc(
quote
local $tmp = $BenchmarkTools.@benchmarkable $(args...)
$BenchmarkTools.warmup($tmp)
$(hasevals(params) ? :() : :($BenchmarkTools.tune!($tmp)))
$BenchmarkTools.run($tmp)
$BenchmarkTools.run($tmp; warmup=$(hasevals(params)))
gdalle marked this conversation as resolved.
Show resolved Hide resolved
end,
)
end
Expand Down Expand Up @@ -656,9 +663,10 @@ macro btime(args...)
return esc(
quote
local $bench = $BenchmarkTools.@benchmarkable $(args...)
$BenchmarkTools.warmup($bench)
$tune_phase
local $trial, $result = $BenchmarkTools.run_result($bench)
local $trial, $result = $BenchmarkTools.run_result(
$bench; warmup=$(hasevals(params))
gdalle marked this conversation as resolved.
Show resolved Hide resolved
)
local $trialmin = $BenchmarkTools.minimum($trial)
local $trialallocs = $BenchmarkTools.allocs($trialmin)
println(
Expand Down Expand Up @@ -704,10 +712,18 @@ macro bprofile(args...)
return esc(
quote
local $tmp = $BenchmarkTools.@benchmarkable $(args...)
$BenchmarkTools.warmup($tmp)
$(hasevals(params) ? :() : :($BenchmarkTools.tune!($tmp)))
$(
if hasevals(params)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
:(run(
$tmp, $BenchmarkTools.Parameters($tmp.params; evals=1); warmup=false
))
else
:($BenchmarkTools.tune!($tmp))
end
)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
$BenchmarkTools.Profile.clear()
$BenchmarkTools.@profile $BenchmarkTools.run($tmp)
#TODO: improve @bprofile to only measure the running code and none of the setup
$BenchmarkTools.@profile $BenchmarkTools.run($tmp, $tmp.params; warmup=false)
end,
)
end
41 changes: 33 additions & 8 deletions test/ExecutionTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,38 @@ tune!(b_pass)
# warmup #
###########

p = params(warmup(@benchmarkable sin(1)))
@test_deprecated warmup(@benchmarkable sin(1))

is_warm = false
function needs_warm()
global is_warm
if is_warm
sleep(0.1)
else
sleep(2)
is_warm = true
end
end

w = @benchmarkable needs_warm()
w.params.seconds = 1

#test that all measurements from lineartrial used in tune! are warm
is_warm = false
@test maximum(BenchmarkTools.lineartrial(w, w.params)) < 1e9

#test that run warms up the benchmark
tune!(w)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
gdalle marked this conversation as resolved.
Show resolved Hide resolved
is_warm = false
@test minimum(run(w).times) < 1e9
willow-ahrens marked this conversation as resolved.
Show resolved Hide resolved
gdalle marked this conversation as resolved.
Show resolved Hide resolved

#test that belapsed warms up the benchmark
gdalle marked this conversation as resolved.
Show resolved Hide resolved
is_warm = false
@test (@belapsed needs_warm() seconds = 1) < 1

This comment was marked as resolved.


@test p.samples == 1
@test p.evals == 1
@test p.gctrial == false
@test p.gcsample == false
#test that belapsed warms up the benchmark even when evals are set
is_warm = false
@test (@belapsed needs_warm() seconds = 1 evals = 1) < 1

##############
# @benchmark #
Expand Down Expand Up @@ -282,9 +308,8 @@ b = @bprofile likegcd(x, y) setup = (x = rand(2:200); y = rand(2:200))
io = IOBuffer()
Profile.print(IOContext(io, :displaysize => (24, 200)))
str = String(take!(io))
@test occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; _run", str)
@test !occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; warmup", str)
gdalle marked this conversation as resolved.
Show resolved Hide resolved
@test !occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; tune!", str)
@test occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; #?_run", str)
@test !occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; #?tune!", str)
b = @bprofile 1 + 1
Profile.print(IOContext(io, :displaysize => (24, 200)))
str = String(take!(io))
Expand Down
Loading