forked from JuliaCI/BenchmarkTools.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameters.jl
92 lines (80 loc) · 3.24 KB
/
parameters.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
# most machines will be higher resolution than this, but we're playing it safe
const RESOLUTION = 1000 # 1 μs = 1000 ns
##############
# Parameters #
##############
mutable struct Parameters
seconds::Float64
samples::Int
evals::Int
overhead::Float64
gctrial::Bool
gcsample::Bool
time_tolerance::Float64
memory_tolerance::Float64
end
const DEFAULT_PARAMETERS = Parameters(5.0, 10000, 1, 0, true, false, 0.05, 0.01)
function Parameters(; seconds = DEFAULT_PARAMETERS.seconds,
samples = DEFAULT_PARAMETERS.samples,
evals = DEFAULT_PARAMETERS.evals,
overhead = DEFAULT_PARAMETERS.overhead,
gctrial = DEFAULT_PARAMETERS.gctrial,
gcsample = DEFAULT_PARAMETERS.gcsample,
time_tolerance = DEFAULT_PARAMETERS.time_tolerance,
memory_tolerance = DEFAULT_PARAMETERS.memory_tolerance)
return Parameters(seconds, samples, evals, overhead, gctrial,
gcsample, time_tolerance, memory_tolerance)
end
function Parameters(default::Parameters; seconds = nothing, samples = nothing,
evals = nothing, overhead = nothing, gctrial = nothing,
gcsample = nothing, time_tolerance = nothing,
memory_tolerance = nothing)
params = Parameters()
params.seconds = seconds != nothing ? seconds : default.seconds
params.samples = samples != nothing ? samples : default.samples
params.evals = evals != nothing ? evals : default.evals
params.overhead = overhead != nothing ? overhead : default.overhead
params.gctrial = gctrial != nothing ? gctrial : default.gctrial
params.gcsample = gcsample != nothing ? gcsample : default.gcsample
params.time_tolerance = time_tolerance != nothing ? time_tolerance : default.time_tolerance
params.memory_tolerance = memory_tolerance != nothing ? memory_tolerance : default.memory_tolerance
return params::BenchmarkTools.Parameters
end
function Base.:(==)(a::Parameters, b::Parameters)
return a.seconds == b.seconds &&
a.samples == b.samples &&
a.evals == b.evals &&
a.overhead == b.overhead &&
a.gctrial == b.gctrial &&
a.gcsample == b.gcsample &&
a.time_tolerance == b.time_tolerance &&
a.memory_tolerance == b.memory_tolerance
end
Base.copy(p::Parameters) = Parameters(p.seconds, p.samples, p.evals, p.overhead, p.gctrial,
p.gcsample, p.time_tolerance, p.memory_tolerance)
function loadparams!(a::Parameters, b::Parameters, fields...)
fields = isempty(fields) ? fieldnames(Parameters) : fields
for f in fields
setfield!(a, f, getfield(b, f))
end
return a
end
################################
# RESOLUTION/OVERHEAD settings #
################################
@noinline nullfunc() = Base.inferencebarrier(nothing)::Nothing
@noinline function overhead_sample(evals)
start_time = time_ns()
for _ in 1:evals
nullfunc()
end
sample_time = time_ns() - start_time
return (sample_time / evals)
end
function estimate_overhead()
x = typemax(Float64)
for _ in 1:10000
x = min(x, overhead_sample(RESOLUTION))
end
return x
end