Skip to content

Commit b7c7b32

Browse files
Show timing and allocations for tests (#48)
* Show timing and allocations for tests * Revert removal of newline * Apply suggestions from code review Co-authored-by: Jose Storopoli <[email protected]> Co-authored-by: Jose Storopoli <[email protected]>
1 parent 5731396 commit b7c7b32

File tree

6 files changed

+30
-13
lines changed

6 files changed

+30
-13
lines changed

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
55
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
66
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
77
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8+
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
89

910
[compat]
1011
CSV = "0.9, 1"
1112
CategoricalArrays = "0.10"
1213
DataFrames = "1"
14+
TimerOutputs = "0.5"

test/data_constructors.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@testset "data_constructors.jl" begin
1+
@timed_testset "data_constructors" begin
22
@testset "data_response" begin
33
@testset "NamedTuples" begin
44
expected = [2, 3, 4, 5]

test/priors.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@testset "prior.jl" begin
1+
@timed_testset "priors" begin
22
@testset "types" begin
33
@test DefaultPrior() isa T.Prior
44

test/runtests.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ using DataFrames
55
using CategoricalArrays: CategoricalValue
66
using CategoricalArrays: categorical, levels
77
using Statistics: mean, std
8+
using TimerOutputs: TimerOutputs, @timeit
89
using Random: seed!
910

1011
const T = TuringGLM
@@ -29,9 +30,23 @@ df_str = DataFrame(nt_str)
2930

3031
df_cat = DataFrame(nt_cat)
3132

32-
@testset "TuringGLM.jl" begin
33+
# To capture time and allocation information.
34+
const TIMEROUTPUT = TimerOutputs.TimerOutput()
35+
macro timed_testset(str, block)
36+
return quote
37+
@timeit TIMEROUTPUT "$($(esc(str)))" begin
38+
@testset "$($(esc(str)))" begin
39+
$(esc(block))
40+
end
41+
end
42+
end
43+
end
44+
45+
@testset "TuringGLM" begin
3346
include("data_constructors.jl")
3447
include("utils.jl")
3548
include("priors.jl")
3649
include("turing_model.jl")
3750
end
51+
52+
show(TIMEROUTPUT; compact=true, sortby=:firstexec)

test/turing_model.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
@testset "turing_model.jl" begin
1+
@timed_testset "turing_model" begin
22
DATA_DIR = joinpath("..", "data")
33
kidiq = CSV.read(joinpath(DATA_DIR, "kidiq.csv"), DataFrame)
44
wells = CSV.read(joinpath(DATA_DIR, "wells.csv"), DataFrame)
55
roaches = CSV.read(joinpath(DATA_DIR, "roaches.csv"), DataFrame)
66
cheese = CSV.read(joinpath(DATA_DIR, "cheese.csv"), DataFrame)
7-
@testset "Gaussian Model" begin
7+
@timed_testset "Gaussian Model" begin
88
f = @formula(kid_score ~ mom_iq * mom_hs)
99
@testset "standardize=false" begin
1010
m = turing_model(f, kidiq)
@@ -38,7 +38,7 @@
3838
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] 0.593 atol = 0.2
3939
end
4040
end
41-
@testset "Student Model" begin
41+
@timed_testset "TDist Model" begin
4242
f = @formula(kid_score ~ mom_iq * mom_hs)
4343
@testset "standardize=false" begin
4444
m = turing_model(f, kidiq; model=TDist)
@@ -59,7 +59,7 @@
5959
@test quantile(chn)[, Symbol("50.0%")] 1.178 atol = 0.5
6060
end
6161
end
62-
@testset "Logistic Model" begin
62+
@timed_testset "Bernoulli Model" begin
6363
f = @formula(switch ~ arsenic + dist + assoc + educ)
6464
@testset "standardize=false" begin
6565
m = turing_model(f, wells; model=Bernoulli)
@@ -78,7 +78,7 @@
7878
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.009 atol = 0.2
7979
end
8080
end
81-
@testset "Pois Model" begin
81+
@timed_testset "Poisson Model" begin
8282
f = @formula(y ~ roach1 + treatment + senior + exposure2)
8383
@testset "standardize=false" begin
8484
m = turing_model(f, roaches; model=Poisson)
@@ -97,7 +97,7 @@
9797
@test quantile(chn)[Symbol("β[2]"), Symbol("50.0%")] -0.5145 atol = 0.2
9898
end
9999
end
100-
@testset "NegBin Model" begin
100+
@timed_testset "NegativeBinomial Model" begin
101101
f = @formula(y ~ roach1 + treatment + senior + exposure2)
102102
@testset "standardize=false" begin
103103
m = turing_model(f, roaches; model=NegativeBinomial)
@@ -118,7 +118,7 @@
118118
@test quantile(chn)[:ϕ⁻, Symbol("50.0%")] 3.56 atol = 0.2
119119
end
120120
end
121-
@testset "Hierarchical Model" begin
121+
@timed_testset "Hierarchical Model" begin
122122
f = @formula(y ~ (1 | cheese) + background)
123123
m = turing_model(f, cheese)
124124
chn = sample(seed!(123), m, NUTS(), MCMCThreads(), 2_000, 2)

test/utils.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
@testset "utils" begin
2-
@testset "center_predictors" begin
1+
@timed_testset "utils" begin
2+
@timed_testset "center_predictors" begin
33
@testset "NamedTuples" begin
44
f = @formula(y_int ~ x_float + x_cat)
55
X = T.data_fixed_effects(f, nt_str)
@@ -29,7 +29,7 @@
2929
end
3030
end
3131

32-
@testset "standardize_predictors" begin
32+
@timed_testset "standardize_predictors" begin
3333
@testset "NamedTuples" begin
3434
f = @formula(y_int ~ x_float + x_cat)
3535
y = T.data_response(f, nt_str)

0 commit comments

Comments
 (0)