Class: Waveformjson::Log
- Inherits:
-
Object
- Object
- Waveformjson::Log
- Defined in:
- lib/waveformjson.rb
Overview
A simple class for logging + benchmarking, nice to have good feedback on a long batch operation.
There’s probably 10,000,000 other bechmarking classes, but writing this was easier than using Google.
Instance Attribute Summary collapse
-
#io ⇒ Object
Returns the value of attribute io.
Instance Method Summary collapse
-
#done!(msg = "") ⇒ Object
Prints the given message to the log followed by the most recent benchmark (note that it calls .end! which will stop the benchmark).
-
#end! ⇒ Object
Returns the elapsed time from the most recently started benchmark clock and ends the benchmark, so that a subsequent call to .end! will return the elapsed time from the previously started benchmark clock.
-
#initialize(io = $stdout) ⇒ Log
constructor
A new instance of Log.
-
#out(msg) ⇒ Object
Prints the given message to the log.
-
#start! ⇒ Object
Starts a new benchmark clock and returns the index of the new clock.
-
#time?(index) ⇒ Boolean
Returns the elapsed time from the benchmark clock w/ the given index (as returned from when .start! was called).
-
#timed(message = nil, &block) ⇒ Object
Benchmarks the given block, printing out the given message first (if given).
Constructor Details
#initialize(io = $stdout) ⇒ Log
Returns a new instance of Log.
145 146 147 |
# File 'lib/waveformjson.rb', line 145 def initialize(io=$stdout) @io = io end |
Instance Attribute Details
#io ⇒ Object
Returns the value of attribute io.
143 144 145 |
# File 'lib/waveformjson.rb', line 143 def io @io end |
Instance Method Details
#done!(msg = "") ⇒ Object
Prints the given message to the log followed by the most recent benchmark (note that it calls .end! which will stop the benchmark)
156 157 158 |
# File 'lib/waveformjson.rb', line 156 def done!(msg="") out "#{msg} (#{self.end!}s)\n" end |
#end! ⇒ Object
Returns the elapsed time from the most recently started benchmark clock and ends the benchmark, so that a subsequent call to .end! will return the elapsed time from the previously started benchmark clock.
174 175 176 177 178 |
# File 'lib/waveformjson.rb', line 174 def end! elapsed = (Time.now - @benchmarks[@current]) @current -= 1 elapsed end |
#out(msg) ⇒ Object
Prints the given message to the log
150 151 152 |
# File 'lib/waveformjson.rb', line 150 def out(msg) io.print(msg) if io end |
#start! ⇒ Object
Starts a new benchmark clock and returns the index of the new clock.
If .start! is called again before .end! then the time returned will be the elapsed time from the next call to start!, and calling .end! again will return the time from this call to start! (that is, the clocks are LIFO)
166 167 168 169 |
# File 'lib/waveformjson.rb', line 166 def start! (@benchmarks ||= []) << Time.now @current = @benchmarks.size - 1 end |
#time?(index) ⇒ Boolean
Returns the elapsed time from the benchmark clock w/ the given index (as returned from when .start! was called).
182 183 184 |
# File 'lib/waveformjson.rb', line 182 def time?(index) Time.now - @benchmarks[index] end |
#timed(message = nil, &block) ⇒ Object
Benchmarks the given block, printing out the given message first (if given).
188 189 190 191 192 193 |
# File 'lib/waveformjson.rb', line 188 def timed(=nil, &block) start! out() if yield done! end |