Skip to content

Commit 6d6858f

Browse files
committed
Display pre-test time
1 parent c76ceae commit 6d6858f

File tree

2 files changed

+45
-12
lines changed

2 files changed

+45
-12
lines changed

src/ParallelTestRunner.jl

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ struct TestRecord <: AbstractTestRecord
8484
bytes::UInt64
8585
gctime::Float64
8686
rss::UInt64
87+
total_time::Float64
8788
end
8889

8990
function memory_usage(rec::TestRecord)
@@ -103,6 +104,7 @@ struct TestIOContext
103104
stdout::IO
104105
stderr::IO
105106
color::Bool
107+
debug_timing::Bool
106108
lock::ReentrantLock
107109
name_align::Int
108110
elapsed_align::Int
@@ -112,7 +114,7 @@ struct TestIOContext
112114
rss_align::Int
113115
end
114116

115-
function test_IOContext(stdout::IO, stderr::IO, lock::ReentrantLock, name_align::Int)
117+
function test_IOContext(stdout::IO, stderr::IO, lock::ReentrantLock, name_align::Int, debug_timing::Bool)
116118
elapsed_align = textwidth("Time (s)")
117119
gc_align = textwidth("GC (s)")
118120
percent_align = textwidth("GC %")
@@ -122,19 +124,26 @@ function test_IOContext(stdout::IO, stderr::IO, lock::ReentrantLock, name_align:
122124
color = get(stdout, :color, false)
123125

124126
return TestIOContext(
125-
stdout, stderr, color, lock, name_align, elapsed_align, gc_align, percent_align,
127+
stdout, stderr, color, debug_timing, lock, name_align, elapsed_align, gc_align, percent_align,
126128
alloc_align, rss_align
127129
)
128130
end
129131

130132
function print_header(ctx::TestIOContext, testgroupheader, workerheader)
131133
lock(ctx.lock)
132134
try
135+
# header top
133136
printstyled(ctx.stdout, " "^(ctx.name_align + textwidth(testgroupheader) - 3), "")
134-
printstyled(ctx.stdout, " │ ──────────────── CPU ──────────────── │\n", color = :white)
137+
printstyled(ctx.stdout, " |", color = :white)
138+
ctx.debug_timing && printstyled(ctx.stdout, " Init │", color = :white)
139+
printstyled(ctx.stdout, " ──────────────── CPU ──────────────── │\n", color = :white)
140+
141+
# header bottom
135142
printstyled(ctx.stdout, testgroupheader, color = :white)
136143
printstyled(ctx.stdout, lpad(workerheader, ctx.name_align - textwidth(testgroupheader) + 1), "", color = :white)
137-
printstyled(ctx.stdout, "Time (s) │ GC (s) │ GC % │ Alloc (MB) │ RSS (MB) │\n", color = :white)
144+
printstyled(ctx.stdout, "Time (s) │", color = :white)
145+
ctx.debug_timing && printstyled(ctx.stdout, " time (s) │", color = :white)
146+
printstyled(ctx.stdout, " GC (s) │ GC % │ Alloc (MB) │ RSS (MB) │\n", color = :white)
138147
flush(ctx.stdout)
139148
finally
140149
unlock(ctx.lock)
@@ -160,9 +169,15 @@ function print_test_finished(record::TestRecord, wrkr, test, ctx::TestIOContext)
160169
try
161170
printstyled(ctx.stdout, test, color = :white)
162171
printstyled(ctx.stdout, lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), "", color = :white)
163-
time_str = @sprintf("%7.2f", record.time)
172+
time = record.time
173+
time_str = @sprintf("%7.2f", time)
164174
printstyled(ctx.stdout, lpad(time_str, ctx.elapsed_align, " "), "", color = :white)
165175

176+
if ctx.debug_timing
177+
init_time_str = @sprintf("%7.2f", record.total_time - time)
178+
printstyled(ctx.stdout, lpad(init_time_str, ctx.elapsed_align, " "), "", color = :white)
179+
end
180+
166181
gc_str = @sprintf("%5.2f", record.gctime)
167182
printstyled(ctx.stdout, lpad(gc_str, ctx.gc_align, " "), "", color = :white)
168183
percent_str = @sprintf("%4.1f", 100 * record.gctime / record.time)
@@ -188,8 +203,11 @@ function print_test_failed(record::TestRecord, wrkr, test, ctx::TestIOContext)
188203
lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), " |"
189204
, color = :red
190205
)
191-
time_str = @sprintf("%7.2f", record.time)
206+
time = record.time
207+
time_str = @sprintf("%7.2f", time)
192208
printstyled(ctx.stderr, lpad(time_str, ctx.elapsed_align + 1, " "), "", color = :red)
209+
init_time_str = @sprintf("%7.2f", record.total_time - time)
210+
printstyled(ctx.stdout, lpad(init_time_str, ctx.elapsed_align, " "), "", color = :red)
193211

194212
failed_str = "failed at $(now())\n"
195213
# 11 -> 3 from " | " 3x and 2 for each " " on either side
@@ -275,7 +293,7 @@ function Test.finish(ts::WorkerTestSet)
275293
return ts.wrapped_ts
276294
end
277295

278-
function runtest(f, name, init_code)
296+
function runtest(f, name, init_code, start_time)
279297
function inner()
280298
# generate a temporary module to execute the tests in
281299
mod = @eval(Main, module $(gensym(name)) end)
@@ -304,7 +322,7 @@ function runtest(f, name, init_code)
304322

305323
# process results
306324
rss = Sys.maxrss()
307-
record = TestRecord(data..., rss)
325+
record = TestRecord(data..., rss, time() - start_time)
308326

309327
GC.gc(true)
310328
return record
@@ -539,6 +557,7 @@ Fields are
539557
540558
* `jobs::Union{Some{Int}, Nothing}`: the number of jobs
541559
* `verbose::Union{Some{Nothing}, Nothing}`: whether verbose printing was enabled
560+
* `debug_timing::Union{Some{Nothing}, Nothing}`: whether debug timing printing was enabled
542561
* `quickfail::Union{Some{Nothing}, Nothing}`: whether quick fail was enabled
543562
* `list::Union{Some{Nothing}, Nothing}`: whether tests should be listed
544563
* `custom::Dict{String,Any}`: a dictionary of custom arguments
@@ -547,6 +566,7 @@ Fields are
547566
struct ParsedArgs
548567
jobs::Union{Some{Int}, Nothing}
549568
verbose::Union{Some{Nothing}, Nothing}
569+
debug_timing::Union{Some{Nothing}, Nothing}
550570
quickfail::Union{Some{Nothing}, Nothing}
551571
list::Union{Some{Nothing}, Nothing}
552572

@@ -604,7 +624,8 @@ function parse_args(args; custom::Array{String} = String[])
604624
--list List all available tests.
605625
--verbose Print more information during testing.
606626
--quickfail Fail the entire run as soon as a single test errored.
607-
--jobs=N Launch `N` processes to perform tests."""
627+
--jobs=N Launch `N` processes to perform tests.
628+
--debug-timing Print testset initialization timings."""
608629

609630
if !isempty(custom)
610631
usage *= "\n\nCustom arguments:"
@@ -619,6 +640,7 @@ function parse_args(args; custom::Array{String} = String[])
619640

620641
jobs = extract_flag!(args, "--jobs"; typ = Int)
621642
verbose = extract_flag!(args, "--verbose")
643+
debug_timing = extract_flag!(args, "--debug-timing")
622644
quickfail = extract_flag!(args, "--quickfail")
623645
list = extract_flag!(args, "--list")
624646

@@ -633,7 +655,7 @@ function parse_args(args; custom::Array{String} = String[])
633655
error("Unknown test options `$(join(optlike_args, " "))` (try `--help` for usage instructions)")
634656
end
635657

636-
return ParsedArgs(jobs, verbose, quickfail, list, custom_args, args)
658+
return ParsedArgs(jobs, verbose, debug_timing, quickfail, list, custom_args, args)
637659
end
638660

639661
"""
@@ -831,7 +853,7 @@ function runtests(mod::Module, args::ParsedArgs;
831853
stderr.lock = print_lock
832854
end
833855

834-
io_ctx = test_IOContext(stdout, stderr, print_lock, name_align)
856+
io_ctx = test_IOContext(stdout, stderr, print_lock, name_align, !isnothing(args.debug_timing))
835857
print_header(io_ctx, testgroupheader, workerheader)
836858

837859
status_lines_visible = Ref(0)
@@ -1015,7 +1037,7 @@ function runtests(mod::Module, args::ParsedArgs;
10151037
result = try
10161038
Malt.remote_eval_wait(Main, wrkr.w, :(import ParallelTestRunner))
10171039
Malt.remote_call_fetch(invokelatest, wrkr.w, runtest,
1018-
testsuite[test], test, init_code)
1040+
testsuite[test], test, init_code, test_t0)
10191041
catch ex
10201042
if isa(ex, InterruptException)
10211043
# the worker got interrupted, signal other tasks to stop

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ cd(@__DIR__)
2424
@test isfile(ParallelTestRunner.get_history_file(ParallelTestRunner))
2525
end
2626

27+
@testset "debug timing" begin
28+
io = IOBuffer()
29+
io_color = IOContext(io, :color => true)
30+
runtests(ParallelTestRunner, ["--debug-timing"]; stdout=io_color, stderr=io_color)
31+
str = String(take!(io))
32+
33+
@test contains(str, "Init") # debug timing
34+
@test contains(str, "time (s)") # debug timing
35+
@test contains(str, "Time (s)") # normal timing
36+
end
37+
2738
@testset "subdir use" begin
2839
d = @__DIR__
2940
testsuite = find_tests(d)

0 commit comments

Comments
 (0)