class CarrierWave::Audio::Processor::Log

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.

Attributes

io[RW]

Public Class Methods

new(io=$stdout) click to toggle source
# File lib/carrierwave/audio/processor.rb, line 178
def initialize(io=$stdout)
  @io = io
end

Public Instance Methods

done!(msg="") click to toggle source

Prints the given message to the log followed by the most recent benchmark (note that it calls .end! which will stop the benchmark)

# File lib/carrierwave/audio/processor.rb, line 189
def done!(msg="")
  out "#{msg} (#{self.end!}s)\n"
end
end!() click to toggle source

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.

# File lib/carrierwave/audio/processor.rb, line 207
def end!
  elapsed = (Time.now - @benchmarks[@current])
  @current -= 1
  elapsed
end
out(msg) click to toggle source

Prints the given message to the log

# File lib/carrierwave/audio/processor.rb, line 183
def out(msg)
  io.print(msg) if io
end
start!() click to toggle source

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)

# File lib/carrierwave/audio/processor.rb, line 199
def start!
  (@benchmarks ||= []) << Time.now
  @current = @benchmarks.size - 1
end
time?(index) click to toggle source

Returns the elapsed time from the benchmark clock w/ the given index (as returned from when .start! was called).

# File lib/carrierwave/audio/processor.rb, line 215
def time?(index)
  Time.now - @benchmarks[index]
end
timed(message=nil) { || ... } click to toggle source

Benchmarks the given block, printing out the given message first (if given).

# File lib/carrierwave/audio/processor.rb, line 221
def timed(message=nil, &block)
  start!
  out(message) if message
  yield
  done!
end