Skip to content

Commit 23ec77d

Browse files
committed
Make TrialContents a struct and use it in tests
1 parent 533c60b commit 23ec77d

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/execution.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function _run(b::Benchmark, p::Parameters; verbose=false, pad="", kwargs...)
108108
params.gcsample && gcscrub()
109109
trial_contents = b.samplefunc(b.quote_vals, params)
110110
push!(trial, trial_contents)
111-
return_val = trial_contents.__return_val
111+
return_val = trial_contents.return_val
112112
iters = 2
113113
while (Base.time() - start_time) < params.seconds && iters params.samples
114114
params.gcsample && gcscrub()
@@ -178,7 +178,7 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
178178
for evals in eachindex(estimates)
179179
params.gcsample && gcscrub()
180180
params.evals = evals
181-
estimates[evals] = first(b.samplefunc(b.quote_vals, params))
181+
estimates[evals] = b.samplefunc(b.quote_vals, params).time
182182
completed += 1
183183
((time() - start_time) > params.seconds) && break
184184
end
@@ -596,7 +596,7 @@ function generate_benchmark_definition(
596596
)
597597
end
598598
end
599-
return (;
599+
return BenchmarkTools.TrialContents(
600600
__time,
601601
__gctime,
602602
__memory,

src/trials.jl

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ mutable struct Trial
1111
linux_perf_stats::Union{LinuxPerf.Stats,Nothing}
1212
end
1313

14+
struct TrialContents
15+
time
16+
gctime
17+
memory
18+
allocs
19+
return_val
20+
return_val_2
21+
linux_perf_stats
22+
end
23+
1424
function Trial(params::Parameters)
1525
return Trial(params, Float64[], Float64[], typemax(Int), typemax(Int), nothing)
1626
end
@@ -27,26 +37,16 @@ function Base.copy(t::Trial)
2737
return Trial(copy(t.params), copy(t.times), copy(t.gctimes), t.memory, t.allocs)
2838
end
2939

30-
const TrialContents = NamedTuple{(
31-
:__time,
32-
:__gctime,
33-
:__memory,
34-
:__allocs,
35-
:__return_val,
36-
:__return_val_2,
37-
:__linux_perf_stats,
38-
)}
39-
4040
function Base.push!(t::Trial, trial_contents::TrialContents)
41-
time = trial_contents.__time
42-
gctime = trial_contents.__gctime
43-
memory = trial_contents.__memory
44-
allocs = trial_contents.__allocs
41+
time = trial_contents.time
42+
gctime = trial_contents.gctime
43+
memory = trial_contents.memory
44+
allocs = trial_contents.allocs
4545
push!(t.times, time)
4646
push!(t.gctimes, gctime)
4747
memory < t.memory && (t.memory = memory)
4848
allocs < t.allocs && (t.allocs = allocs)
49-
t.linux_perf_stats = trial_contents.__linux_perf_stats
49+
t.linux_perf_stats = trial_contents.linux_perf_stats
5050
return t
5151
end
5252

@@ -58,7 +58,12 @@ end
5858

5959
Base.length(t::Trial) = length(t.times)
6060
function Base.getindex(t::Trial, i::Number)
61-
return push!(Trial(t.params), t.times[i], t.gctimes[i], t.memory, t.allocs)
61+
return push!(
62+
Trial(t.params),
63+
TrialContents(
64+
t.times[i], t.gctimes[i], t.memory, t.allocs, nothing, nothing, nothing
65+
),
66+
)
6267
end
6368
Base.getindex(t::Trial, i) = Trial(t.params, t.times[i], t.gctimes[i], t.memory, t.allocs)
6469
Base.lastindex(t::Trial) = length(t)

test/TrialsTests.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@ using Test
66
#########
77
# Trial #
88
#########
9-
109
trial1 = BenchmarkTools.Trial(BenchmarkTools.Parameters(; evals=2))
11-
push!(trial1, 2, 1, 4, 5)
12-
push!(trial1, 21, 0, 41, 51)
10+
push!(trial1, TrialContents(2, 1, 4, 5, nothing, nothing, nothing))
11+
push!(trial1, TrialContents(21, 0, 41, 51, nothing, nothing, nothing))
1312

1413
trial2 = BenchmarkTools.Trial(BenchmarkTools.Parameters(; time_tolerance=0.15))
15-
push!(trial2, 21, 0, 41, 51)
16-
push!(trial2, 2, 1, 4, 5)
14+
push!(trial2, TrialContents(21, 0, 41, 51, nothing, nothing, nothing))
15+
push!(trial2, TrialContents(2, 1, 4, 5, nothing, nothing, nothing))
1716

18-
push!(trial2, 21, 0, 41, 51)
17+
push!(trial2, TrialContents(21, 0, 41, 51, nothing, nothing, nothing))
1918
@test length(trial2) == 3
2019
deleteat!(trial2, 3)
2120
@test length(trial1) == length(trial2) == 2
@@ -33,8 +32,10 @@ trial2.params = trial1.params
3332

3433
@test trial1 == trial2
3534

36-
@test trial1[2] ==
37-
push!(BenchmarkTools.Trial(BenchmarkTools.Parameters(; evals=2)), 21, 0, 4, 5)
35+
@test trial1[2] == push!(
36+
BenchmarkTools.Trial(BenchmarkTools.Parameters(; evals=2)),
37+
TrialContents(21, 0, 4, 5, nothing, nothing, nothing),
38+
)
3839
@test trial1[1:end] == trial1
3940

4041
@test time(trial1) == time(trial2) == 2.0
@@ -61,11 +62,11 @@ rmskew!(trial3)
6162
randtrial = BenchmarkTools.Trial(BenchmarkTools.Parameters())
6263

6364
for _ in 1:40
64-
push!(randtrial, rand(1:20), 1, 1, 1)
65+
push!(randtrial, TrialContents(rand(1:20), 1, 1, 1))
6566
end
6667

6768
while mean(randtrial) <= median(randtrial)
68-
push!(randtrial, rand(10:20), 1, 1, 1)
69+
push!(randtrial, TrialContents(rand(10:20), 1, 1, 1))
6970
end
7071

7172
rmskew!(randtrial)
@@ -230,7 +231,7 @@ tj_r_2 = judge(tr; time_tolerance=2.0, memory_tolerance=2.0)
230231
@test BenchmarkTools.prettymemory(1073741824) == "1.00 GiB"
231232

232233
@test sprint(show, "text/plain", ta) == sprint(show, ta; context=:compact => false) == """
233-
BenchmarkTools.TrialEstimate:
234+
BenchmarkTools.TrialEstimate:
234235
time: 0.490 ns
235236
gctime: 0.000 ns (0.00%)
236237
memory: 2 bytes
@@ -273,7 +274,7 @@ BenchmarkTools.Trial: 2 samples with 1 evaluation.
273274
Time (median): 1.005 ns ┊ GC (median): 0.00%
274275
Time (mean ± σ): 1.005 ns ± 0.007 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
275276
276-
█ █
277+
█ █
277278
█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█ ▁
278279
1 ns Histogram: frequency by time 1.01 ns <
279280

0 commit comments

Comments
 (0)