|
| 1 | +module CustomisableBenchmarkTests |
| 2 | + |
| 3 | +using BenchmarkTools |
| 4 | +using Test |
| 5 | + |
| 6 | +x = Ref(0) |
| 7 | +setup_prehook(_) = x[] += 1 |
| 8 | +prehook() = x[] += 1 |
| 9 | +posthook() = x[] += 1 |
| 10 | +function sample_result(_, setup_prehook_result, preehook_result, posthook_result) |
| 11 | + @test setup_prehook_result == 1 |
| 12 | + @test preehook_result == 2 |
| 13 | + @test posthook_result == 3 |
| 14 | + @test x[] == 3 |
| 15 | + return x[] += 1 |
| 16 | +end |
| 17 | +function teardown_posthook(_, setup_prehook_result) |
| 18 | + @test setup_prehook_result == 1 |
| 19 | + @test x[] == 4 |
| 20 | + return x[] += 1 |
| 21 | +end |
| 22 | + |
| 23 | +@testset "Disabled custom benchmarking" begin |
| 24 | + x[] = 0 |
| 25 | + res = @benchmark nothing setup_prehook = setup_prehook prehook = prehook posthook = posthook sample_result = |
| 26 | + sample_result teardown_posthook = teardown_posthook run_customisable_func_only = false |
| 27 | + @test res.customisable_result === nothing |
| 28 | + @test !res.customisable_result_for_every_sample |
| 29 | +end |
| 30 | + |
| 31 | +@testset "custom benchmarking last" begin |
| 32 | + for run_customisable_func_only in (true, false) |
| 33 | + x[] = 0 |
| 34 | + res = @benchmark nothing setup_prehook = setup_prehook prehook = prehook posthook = |
| 35 | + posthook sample_result = sample_result teardown_posthook = teardown_posthook enable_customisable_func = |
| 36 | + :LAST run_customisable_func_only = run_customisable_func_only |
| 37 | + if run_customisable_func_only |
| 38 | + @test isempty(res.times) |
| 39 | + @test isempty(res.gctimes) |
| 40 | + @test res.memory == typemax(Int) |
| 41 | + @test res.allocs == typemax(Int) |
| 42 | + end |
| 43 | + @test !res.customisable_result_for_every_sample |
| 44 | + @test res.customisable_result === 4 |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +@testset "custom benchmark every sample, independent of iterations" begin |
| 49 | + for run_customisable_func_only in (true, false) |
| 50 | + x[] = 0 |
| 51 | + setup_prehook(_) = x[] = 1 |
| 52 | + res = @benchmark nothing setup_prehook = setup_prehook prehook = prehook posthook = |
| 53 | + posthook sample_result = sample_result teardown_posthook = teardown_posthook enable_customisable_func = |
| 54 | + :ALL run_customisable_func_only = run_customisable_func_only samples = 1000 |
| 55 | + if run_customisable_func_only |
| 56 | + @test isempty(res.times) |
| 57 | + @test isempty(res.gctimes) |
| 58 | + @test res.memory == typemax(Int) |
| 59 | + @test res.allocs == typemax(Int) |
| 60 | + end |
| 61 | + @test res.customisable_result_for_every_sample |
| 62 | + @test res.customisable_result == fill(4, 1000) |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +@testset "custom benchmark every sample with iteration dependence" begin |
| 67 | + for run_customisable_func_only in (true, false) |
| 68 | + x[] = 0 |
| 69 | + setup_prehook(_) = x[] += 1 |
| 70 | + prehook() = x[] += 1 |
| 71 | + posthook() = x[] += 1 |
| 72 | + function sample_result(_, setup_prehook_result, preehook_result, posthook_result) |
| 73 | + return x[] += 1 |
| 74 | + end |
| 75 | + function teardown_posthook(_, setup_prehook_result) |
| 76 | + return x[] += 1 |
| 77 | + end |
| 78 | + res = @benchmark nothing setup_prehook = setup_prehook prehook = prehook posthook = |
| 79 | + posthook sample_result = sample_result teardown_posthook = teardown_posthook enable_customisable_func = |
| 80 | + :ALL run_customisable_func_only = run_customisable_func_only samples = 1000 |
| 81 | + if run_customisable_func_only |
| 82 | + @test isempty(res.times) |
| 83 | + @test isempty(res.gctimes) |
| 84 | + @test res.memory == typemax(Int) |
| 85 | + @test res.allocs == typemax(Int) |
| 86 | + end |
| 87 | + @test res.customisable_result_for_every_sample |
| 88 | + @test res.customisable_result == collect(5 * (1:1000) .- 1) |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +end # module |
0 commit comments