Skip to content

Commit

Permalink
More compatibility with TimerOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
jipolanco committed Mar 22, 2021
1 parent 9b355dc commit f7a588a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/NoTimerOutput.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@ Base.copy(to::NoTimerOutput) = NoTimerOutput(to.name)
# Add definitions for exported methods
reset_timer!(to::NoTimerOutput) = to
timeit(f::Function, ::NoTimerOutput, ::String) = f()
enable_timer!(to::NoTimerOutput) = true
disable_timer!(to::NoTimerOutput) = false
enable_timer!(::NoTimerOutput) = true
disable_timer!(::NoTimerOutput) = false
flatten(to::NoTimerOutput) = to

ncalls(::NoTimerOutput) = 0
time(::NoTimerOutput) = 0
allocated(::NoTimerOutput) = 0
tottime(::NoTimerOutput) = 0
totallocated(::NoTimerOutput) = 0

complement!(to::NoTimerOutput) = to

Base.haskey(::NoTimerOutput, ::String) = false
Base.getindex(::NoTimerOutput, key::String) = throw(KeyError(key))

function Base.show(io::IO, to::NoTimerOutput)
print(io, "Timer \"", to.name, "\" is a dummy timer")
Expand Down
23 changes: 23 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,17 +463,40 @@ end
@testset "dummy timers" begin
to = TimerOutput()
nt = NoTimerOutput()

@test repr(nt) == "Timer \"root\" is a dummy timer"

let io = IOBuffer()
print_timer(io, nt)
@test String(take!(io)) == "Timer \"root\" is a dummy timer\n"
end

@test copy(nt) == nt
@test reset_timer!(nt) === nt
@test enable_timer!(nt) == true
@test disable_timer!(nt) == false
@test flatten(nt) === nt

foo(x) = x + x
@timeit tt foo(x, tt) = foo(x)
@test foo(4) == foo(4, to)
@test foo(4) == foo(4, nt)

@test @notimeit(nt, foo(3)) == foo(3)

@timeit to "sin" sin(3)
@timeit nt "sin" sin(3)

@test haskey(to, "sin")
@test !haskey(nt, "sin")

@test to["sin"] isa TimerOutput
@test_throws KeyError("sin") nt["sin"]

@test ncalls(nt) == 0
@test TimerOutputs.time(nt) == 0
@test TimerOutputs.allocated(nt) == 0
@test TimerOutputs.tottime(nt) == 0
@test TimerOutputs.totallocated(nt) == 0
@test TimerOutputs.complement!(nt) === nt
end

0 comments on commit f7a588a

Please sign in to comment.