Skip to content

Commit 749215c

Browse files
committed
Move checking of parameter values into constructor
1 parent 5036058 commit 749215c

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

src/execution.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ end
110110
function _run(b::Benchmark, p::Parameters; verbose=false, pad="", warmup=true, kwargs...)
111111
params = Parameters(p; kwargs...)
112112
@assert params.seconds > 0.0 "time limit must be greater than 0.0"
113-
@assert params.enable_customisable_func in (:FALSE, :ALL, :LAST) "invalid value $(params.enable_customisable_func) for enable_customisable_func which must be :FALSE, :ALL or :LAST"
114-
@assert !(
115-
params.run_customisable_func_only && params.enable_customisable_func == :FALSE
116-
) "run_customisable_func_only is set to true, but enable_customisable_func is set to :FALSE"
117113
if warmup #warmup sample
118114
params.run_customisable_func_only &&
119115
b.samplefunc(b.quote_vals, Parameters(params; evals=1))

src/parameters.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,60 @@ mutable struct Parameters{A,B}
2323
sample_result
2424
prehook::A
2525
posthook::B
26+
27+
function Parameters{A,B}(
28+
seconds,
29+
samples,
30+
evals,
31+
evals_set,
32+
overhead,
33+
gctrial,
34+
gcsample,
35+
time_tolerance,
36+
memory_tolerance,
37+
run_customisable_func_only,
38+
enable_customisable_func,
39+
customisable_gcsample,
40+
setup_prehook,
41+
teardown_posthook,
42+
sample_result,
43+
prehook::A,
44+
posthook::B,
45+
) where {A,B}
46+
if enable_customisable_func (:FALSE, :ALL, :LAST)
47+
throw(
48+
ArgumentError(
49+
"invalid value $(enable_customisable_func) for enable_customisable_func which must be :FALSE, :ALL or :LAST",
50+
),
51+
)
52+
end
53+
if run_customisable_func_only && enable_customisable_func == :FALSE
54+
throw(
55+
ArgumentError(
56+
"run_customisable_func_only is set to true, but enable_customisable_func is set to :FALSE",
57+
),
58+
)
59+
end
60+
return new(
61+
seconds,
62+
samples,
63+
evals,
64+
evals_set,
65+
overhead,
66+
gctrial,
67+
gcsample,
68+
time_tolerance,
69+
memory_tolerance,
70+
run_customisable_func_only,
71+
enable_customisable_func,
72+
customisable_gcsample,
73+
setup_prehook,
74+
teardown_posthook,
75+
sample_result,
76+
prehook,
77+
posthook,
78+
)
79+
end
2680
end
2781

2882
# https://github.com/JuliaLang/julia/issues/17186

src/serialization.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,10 @@ function recover(x::Vector)
7070
# These fields should all be >= 0, so we can ignore -Inf case
7171
typemax(ft)
7272
elseif fn == "enable_customisable_func"
73-
if !haskey(fields, fn) || fields[fn] == "FALSE"
73+
if !haskey(fields, fn)
7474
:FALSE
75-
elseif fields[fn] == "LAST"
76-
:LAST
77-
elseif fields[fn] == "ALL"
78-
:ALL
7975
else
80-
throw(
81-
ArgumentError(
82-
"Invalid value $(fields[fn]) for enable_customisable_func which must be one of :ALL, :LAST, :FALSE",
83-
),
84-
)
76+
Symbol(fields[fn])
8577
end
8678
elseif fn in (
8779
"run_customisable_func_only",

test/ParametersTests.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,23 @@ BenchmarkTools.DEFAULT_PARAMETERS.setup_prehook = old_setup_prehook
8787
BenchmarkTools.DEFAULT_PARAMETERS.teardown_posthook = old_teardown_posthook
8888
BenchmarkTools.DEFAULT_PARAMETERS.sample_result = old_sample_result
8989

90+
for vals in (false, true, :ARST, :TRUE, :false, :ON)
91+
@test_throws ArgumentError Parameters(p; enable_customisable_func=vals)
92+
@test_throws ArgumentError Parameters(; enable_customisable_func=vals)
93+
end
94+
95+
@test_throws ArgumentError Parameters(;
96+
enable_customisable_func=:FALSE, run_customisable_func_only=true
97+
)
98+
@test_nowarn Parameters(; enable_customisable_func=:FALSE, run_customisable_func_only=false)
99+
for run_customisable_func_only in (false, true)
100+
@test_nowarn Parameters(;
101+
enable_customisable_func=:ALL, run_customisable_func_only=run_customisable_func_only
102+
)
103+
@test_nowarn Parameters(;
104+
enable_customisable_func=:LAST,
105+
run_customisable_func_only=run_customisable_func_only,
106+
)
107+
end
108+
90109
end # module

0 commit comments

Comments
 (0)