Skip to content

Commit

Permalink
Do not measure timing if debugging is disabled or no block of code wa…
Browse files Browse the repository at this point in the history
…s given.
  • Loading branch information
gavinlaking committed Jan 29, 2016
1 parent 73931fd commit 76edeac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 17 additions & 9 deletions lib/vedeu/logging/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,30 @@ def initialize(message = '')
end

# Write an entry to the log file stating how long a section of
# code took in milliseconds. Useful for debugging performance.
# code took in milliseconds when debugging is enabled and a
# block was Useful for debugging performance.
#
# @param block [Proc]
# @macro raise_requires_block
# @return [void] The return value of the executed block.
# @return [void|NilClass] The return value of the executed
# block if given, or nil if debugging is disabled.
def measure(&block)
fail Vedeu::Error::RequiresBlock unless block_given?
if block_given?
if Vedeu.config.debug?
work = yield

if Vedeu.config.debug?
work = yield
Vedeu.log(type: :timer,
message: "#{message} took #{elapsed}ms.")

Vedeu.log(type: :timer,
message: "#{message} took #{elapsed}ms.")
work

work
else
yield

end

else
yield
fail Vedeu::Error::RequiresBlock if Vedeu.config.debug?

end
end
Expand All @@ -65,6 +71,8 @@ def measure(&block)
# @return [String]
attr_reader :message

private

# Returns the elapsed time in milliseconds with 3 decimal
# places.
#
Expand Down
12 changes: 11 additions & 1 deletion test/lib/vedeu/logging/timer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ module Logging
context 'when the block is not given' do
subject { instance.measure }

it { proc { subject }.must_raise(Vedeu::Error::RequiresBlock) }
context 'when debugging is enabled' do
before { Vedeu.config.stubs(:debug?).returns(true) }

it { proc { subject }.must_raise(Vedeu::Error::RequiresBlock) }
end

context 'when debugging is disabled' do
before { Vedeu.config.stubs(:debug?).returns(false) }

it { subject.must_equal(nil) }
end
end
end

Expand Down

0 comments on commit 76edeac

Please sign in to comment.