Skip to content

Commit bd6f318

Browse files
committed
Display pre-test time
1 parent 4d49d6d commit bd6f318

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/ParallelTestRunner.jl

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct TestRecord <: AbstractTestRecord
8787
bytes::UInt64
8888
gctime::Float64
8989
rss::UInt64
90+
total_time::Float64
9091
end
9192

9293
function memory_usage(rec::TestRecord)
@@ -106,6 +107,7 @@ struct TestIOContext
106107
stdout::IO
107108
stderr::IO
108109
color::Bool
110+
debug_timing::Bool
109111
lock::ReentrantLock
110112
name_align::Int
111113
elapsed_align::Int
@@ -115,7 +117,7 @@ struct TestIOContext
115117
rss_align::Int
116118
end
117119

118-
function test_IOContext(stdout::IO, stderr::IO, lock::ReentrantLock, name_align::Int)
120+
function test_IOContext(stdout::IO, stderr::IO, lock::ReentrantLock, name_align::Int, debug_timing::Bool)
119121
elapsed_align = textwidth("Time (s)")
120122
gc_align = textwidth("GC (s)")
121123
percent_align = textwidth("GC %")
@@ -125,19 +127,26 @@ function test_IOContext(stdout::IO, stderr::IO, lock::ReentrantLock, name_align:
125127
color = get(stdout, :color, false)
126128

127129
return TestIOContext(
128-
stdout, stderr, color, lock, name_align, elapsed_align, gc_align, percent_align,
130+
stdout, stderr, color, debug_timing, lock, name_align, elapsed_align, gc_align, percent_align,
129131
alloc_align, rss_align
130132
)
131133
end
132134

133135
function print_header(ctx::TestIOContext, testgroupheader, workerheader)
134136
lock(ctx.lock)
135137
try
138+
# header top
136139
printstyled(ctx.stdout, " "^(ctx.name_align + textwidth(testgroupheader) - 3), "")
137-
printstyled(ctx.stdout, " │ ──────────────── CPU ──────────────── │\n", color = :white)
140+
printstyled(ctx.stdout, " |", color = :white)
141+
ctx.debug_timing && printstyled(ctx.stdout, " Init │", color = :white)
142+
printstyled(ctx.stdout, " ──────────────── CPU ──────────────── │\n", color = :white)
143+
144+
# header bottom
138145
printstyled(ctx.stdout, testgroupheader, color = :white)
139146
printstyled(ctx.stdout, lpad(workerheader, ctx.name_align - textwidth(testgroupheader) + 1), "", color = :white)
140-
printstyled(ctx.stdout, "Time (s) │ GC (s) │ GC % │ Alloc (MB) │ RSS (MB) │\n", color = :white)
147+
printstyled(ctx.stdout, "Time (s) │", color = :white)
148+
ctx.debug_timing && printstyled(ctx.stdout, " time (s) │", color = :white)
149+
printstyled(ctx.stdout, " GC (s) │ GC % │ Alloc (MB) │ RSS (MB) │\n", color = :white)
141150
flush(ctx.stdout)
142151
finally
143152
unlock(ctx.lock)
@@ -163,9 +172,15 @@ function print_test_finished(record::TestRecord, wrkr, test, ctx::TestIOContext)
163172
try
164173
printstyled(ctx.stdout, test, color = :white)
165174
printstyled(ctx.stdout, lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), "", color = :white)
175+
166176
time_str = @sprintf("%7.2f", record.time)
167177
printstyled(ctx.stdout, lpad(time_str, ctx.elapsed_align, " "), "", color = :white)
168178

179+
if ctx.debug_timing
180+
init_time_str = @sprintf("%7.2f", record.total_time - record.time)
181+
printstyled(ctx.stdout, lpad(init_time_str, ctx.elapsed_align, " "), "", color = :white)
182+
end
183+
169184
gc_str = @sprintf("%5.2f", record.gctime)
170185
printstyled(ctx.stdout, lpad(gc_str, ctx.gc_align, " "), "", color = :white)
171186
percent_str = @sprintf("%4.1f", 100 * record.gctime / record.time)
@@ -191,9 +206,15 @@ function print_test_failed(record::TestRecord, wrkr, test, ctx::TestIOContext)
191206
lpad("($wrkr)", ctx.name_align - textwidth(test) + 1, " "), " |"
192207
, color = :red
193208
)
209+
194210
time_str = @sprintf("%7.2f", record.time)
195211
printstyled(ctx.stderr, lpad(time_str, ctx.elapsed_align + 1, " "), "", color = :red)
196212

213+
if ctx.debug_stats
214+
init_time_str = @sprintf("%7.2f", record.total_time - record.time)
215+
printstyled(ctx.stdout, lpad(init_time_str, ctx.elapsed_align + 1, " "), "", color = :red)
216+
end
217+
197218
failed_str = "failed at $(now())\n"
198219
# 11 -> 3 from " | " 3x and 2 for each " " on either side
199220
fail_align = (11 + ctx.gc_align + ctx.percent_align + ctx.alloc_align + ctx.rss_align - textwidth(failed_str)) ÷ 2 + textwidth(failed_str)
@@ -278,7 +299,7 @@ function Test.finish(ts::WorkerTestSet)
278299
return ts.wrapped_ts
279300
end
280301

281-
function runtest(f, name, init_code)
302+
function runtest(f, name, init_code, start_time)
282303
function inner()
283304
# generate a temporary module to execute the tests in
284305
mod = @eval(Main, module $(gensym(name)) end)
@@ -307,7 +328,7 @@ function runtest(f, name, init_code)
307328

308329
# process results
309330
rss = Sys.maxrss()
310-
record = TestRecord(data..., rss)
331+
record = TestRecord(data..., rss, time() - start_time)
311332

312333
GC.gc(true)
313334
return record
@@ -536,6 +557,7 @@ Fields are
536557
537558
* `jobs::Union{Some{Int}, Nothing}`: the number of jobs
538559
* `verbose::Union{Some{Nothing}, Nothing}`: whether verbose printing was enabled
560+
* `debug_timing::Union{Some{Nothing}, Nothing}`: whether debug timing printing was enabled
539561
* `quickfail::Union{Some{Nothing}, Nothing}`: whether quick fail was enabled
540562
* `list::Union{Some{Nothing}, Nothing}`: whether tests should be listed
541563
* `custom::Dict{String,Any}`: a dictionary of custom arguments
@@ -544,6 +566,7 @@ Fields are
544566
struct ParsedArgs
545567
jobs::Union{Some{Int}, Nothing}
546568
verbose::Union{Some{Nothing}, Nothing}
569+
debug_timing::Union{Some{Nothing}, Nothing}
547570
quickfail::Union{Some{Nothing}, Nothing}
548571
list::Union{Some{Nothing}, Nothing}
549572

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

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

617641
jobs = extract_flag!(args, "--jobs"; typ = Int)
618642
verbose = extract_flag!(args, "--verbose")
643+
debug_timing = extract_flag!(args, "--debug-timing")
619644
quickfail = extract_flag!(args, "--quickfail")
620645
list = extract_flag!(args, "--list")
621646

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

633-
return ParsedArgs(jobs, verbose, quickfail, list, custom_args, args)
658+
return ParsedArgs(jobs, verbose, debug_timing, quickfail, list, custom_args, args)
634659
end
635660

636661
"""
@@ -831,7 +856,7 @@ function runtests(mod::Module, args::ParsedArgs;
831856
stderr.lock = print_lock
832857
end
833858

834-
io_ctx = test_IOContext(stdout, stderr, print_lock, name_align)
859+
io_ctx = test_IOContext(stdout, stderr, print_lock, name_align, !isnothing(args.debug_timing))
835860
print_header(io_ctx, testgroupheader, workerheader)
836861

837862
status_lines_visible = Ref(0)
@@ -1014,7 +1039,7 @@ function runtests(mod::Module, args::ParsedArgs;
10141039
result = try
10151040
Malt.remote_eval_wait(Main, wrkr.w, :(import ParallelTestRunner))
10161041
Malt.remote_call_fetch(invokelatest, wrkr.w, runtest,
1017-
testsuite[test], test, init_code)
1042+
testsuite[test], test, init_code, test_t0)
10181043
catch ex
10191044
if isa(ex, InterruptException)
10201045
# 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
@@ -26,6 +26,17 @@ include(joinpath(@__DIR__, "utils.jl"))
2626
@test isfile(ParallelTestRunner.get_history_file(ParallelTestRunner))
2727
end
2828

29+
@testset "debug timing" begin
30+
io = IOBuffer()
31+
io_color = IOContext(io, :color => true)
32+
runtests(ParallelTestRunner, ["--debug-timing"]; stdout=io_color, stderr=io_color)
33+
str = String(take!(io))
34+
35+
@test contains(str, "Init") # debug timing
36+
@test contains(str, "time (s)") # debug timing
37+
@test contains(str, "Time (s)") # normal timing
38+
end
39+
2940
@testset "default njobs" begin
3041
@test ParallelTestRunner.default_njobs(; cpu_threads=4, free_memory=UInt64(2) ^ 28) == 1
3142
@test ParallelTestRunner.default_njobs(; cpu_threads=4, free_memory=UInt64(2) ^ 30) == 1

0 commit comments

Comments
 (0)