Skip to content

Commit d564ee7

Browse files
warm up at the beginning of every measurement (#330)
* warm up at the beginning of every measurement. * use JuliaFormatter(Benchmarktools; overwrite=true, style=BlueStyle()) * deprecate warmup instead of remove * JuliaFormatter * ensure that only one eval is used for the warmup, test deprecations * format * export warmup * with formatting * hmm * add a boolean to run kwargs * cool * warmup in the case evals are unspecified. * clean up regex to optionally match gensymed defs from Julia 1.0 * oops, rusty regex
1 parent 8dcac4d commit d564ee7

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/execution.jl

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ end
106106
# Note that trials executed via `run` and `lineartrial` are always executed at top-level
107107
# scope, in order to allow transfer of locally-scoped variables into benchmark scope.
108108

109-
function _run(b::Benchmark, p::Parameters; verbose=false, pad="", kwargs...)
109+
function _run(b::Benchmark, p::Parameters; verbose=false, pad="", warmup=true, kwargs...)
110110
params = Parameters(p; kwargs...)
111111
@assert params.seconds > 0.0 "time limit must be greater than 0.0"
112112
params.gctrial && gcscrub()
113113
start_time = Base.time()
114114
trial = Trial(params)
115+
if warmup
116+
b.samplefunc(b.quote_vals, Parameters(params; evals=1)) #warmup sample
117+
end
115118
params.gcsample && gcscrub()
116119
s = b.samplefunc(b.quote_vals, params)
117120
push!(trial, s[1:(end - 1)]...)
@@ -180,6 +183,8 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
180183
params = Parameters(p; kwargs...)
181184
estimates = zeros(maxevals)
182185
completed = 0
186+
params.evals = 1
187+
b.samplefunc(b.quote_vals, params) #warmup sample
183188
params.gctrial && gcscrub()
184189
start_time = time()
185190
for evals in eachindex(estimates)
@@ -193,6 +198,10 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
193198
end
194199

195200
function warmup(item; verbose::Bool=true)
201+
Base.depwarn(
202+
"`warmup` is deprecated because `run` now warms up every time automatically",
203+
:warmup,
204+
)
196205
return run(item; verbose=verbose, samples=1, evals=1, gctrial=false, gcsample=false)
197206
end
198207

@@ -288,7 +297,6 @@ function tune!(
288297
kwargs...,
289298
)
290299
if !p.evals_set
291-
warmup(b; verbose=false)
292300
estimate = ceil(Int, minimum(lineartrial(b, p; kwargs...)))
293301
b.params.evals = guessevals(estimate)
294302
end
@@ -436,9 +444,8 @@ macro benchmark(args...)
436444
return esc(
437445
quote
438446
local $tmp = $BenchmarkTools.@benchmarkable $(args...)
439-
$BenchmarkTools.warmup($tmp)
440447
$(hasevals(params) ? :() : :($BenchmarkTools.tune!($tmp)))
441-
$BenchmarkTools.run($tmp)
448+
$BenchmarkTools.run($tmp; warmup=$(hasevals(params)))
442449
end,
443450
)
444451
end
@@ -656,9 +663,10 @@ macro btime(args...)
656663
return esc(
657664
quote
658665
local $bench = $BenchmarkTools.@benchmarkable $(args...)
659-
$BenchmarkTools.warmup($bench)
660666
$tune_phase
661-
local $trial, $result = $BenchmarkTools.run_result($bench)
667+
local $trial, $result = $BenchmarkTools.run_result(
668+
$bench; warmup=$(hasevals(params))
669+
)
662670
local $trialmin = $BenchmarkTools.minimum($trial)
663671
local $trialallocs = $BenchmarkTools.allocs($trialmin)
664672
println(
@@ -704,10 +712,18 @@ macro bprofile(args...)
704712
return esc(
705713
quote
706714
local $tmp = $BenchmarkTools.@benchmarkable $(args...)
707-
$BenchmarkTools.warmup($tmp)
708-
$(hasevals(params) ? :() : :($BenchmarkTools.tune!($tmp)))
715+
$(
716+
if hasevals(params)
717+
:(run(
718+
$tmp, $BenchmarkTools.Parameters($tmp.params; evals=1); warmup=false
719+
))
720+
else
721+
:($BenchmarkTools.tune!($tmp))
722+
end
723+
)
709724
$BenchmarkTools.Profile.clear()
710-
$BenchmarkTools.@profile $BenchmarkTools.run($tmp)
725+
#TODO: improve @bprofile to only measure the running code and none of the setup
726+
$BenchmarkTools.@profile $BenchmarkTools.run($tmp, $tmp.params; warmup=false)
711727
end,
712728
)
713729
end

test/ExecutionTests.jl

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,38 @@ tune!(b_pass)
148148
# warmup #
149149
###########
150150

151-
p = params(warmup(@benchmarkable sin(1)))
151+
@test_deprecated warmup(@benchmarkable sin(1))
152+
153+
is_warm = false
154+
function needs_warm()
155+
global is_warm
156+
if is_warm
157+
sleep(0.1)
158+
else
159+
sleep(2)
160+
is_warm = true
161+
end
162+
end
163+
164+
w = @benchmarkable needs_warm()
165+
w.params.seconds = 1
166+
167+
#test that all measurements from lineartrial used in tune! are warm
168+
is_warm = false
169+
@test maximum(BenchmarkTools.lineartrial(w, w.params)) < 1e9
170+
171+
#test that run warms up the benchmark
172+
tune!(w)
173+
is_warm = false
174+
@test minimum(run(w).times) < 1e9
175+
176+
#test that belapsed warms up the benchmark
177+
is_warm = false
178+
@test (@belapsed needs_warm() seconds = 1) < 1
152179

153-
@test p.samples == 1
154-
@test p.evals == 1
155-
@test p.gctrial == false
156-
@test p.gcsample == false
180+
#test that belapsed warms up the benchmark even when evals are set
181+
is_warm = false
182+
@test (@belapsed needs_warm() seconds = 1 evals = 1) < 1
157183

158184
##############
159185
# @benchmark #
@@ -282,9 +308,8 @@ b = @bprofile likegcd(x, y) setup = (x = rand(2:200); y = rand(2:200))
282308
io = IOBuffer()
283309
Profile.print(IOContext(io, :displaysize => (24, 200)))
284310
str = String(take!(io))
285-
@test occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; _run", str)
286-
@test !occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; warmup", str)
287-
@test !occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; tune!", str)
311+
@test occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; #?_run", str)
312+
@test !occursin(r"BenchmarkTools(\.jl)?(/|\\)src(/|\\)execution\.jl:\d+; #?tune!", str)
288313
b = @bprofile 1 + 1
289314
Profile.print(IOContext(io, :displaysize => (24, 200)))
290315
str = String(take!(io))

0 commit comments

Comments
 (0)