class RspecTimer

Constants

VERSION

Attributes

log_file_path[RW]
metrics[R]

Public Class Methods

new() click to toggle source
# File lib/rspec_timer.rb, line 16
def initialize
  reset_metrics
  @log_file_path ||= 'rspec_metrics.yml'
end

Public Instance Methods

end_measurement(example) click to toggle source
# File lib/rspec_timer.rb, line 32
def end_measurement(example)
  current_metrics              = metrics_for(example)
  current_metrics[:end_time]   = Time.now
  current_metrics[:total_time] = current_metrics[:end_time] - current_metrics[:start_time]
  example
end
reset_metrics() click to toggle source
# File lib/rspec_timer.rb, line 21
def reset_metrics
  @metrics = {}
end
run_and_measure(example) click to toggle source
# File lib/rspec_timer.rb, line 39
def run_and_measure(example)
  start_measurement(example)
  example.run
  end_measurement(example)
end
save_metrics() click to toggle source
# File lib/rspec_timer.rb, line 49
def save_metrics
  # Load any existing metrics
  updated_metrics = (YAML.load_file(log_file_path) if File.exists? (log_file_path)) || {}
  # Merge in the new metrics, updating any existing ones
  @metrics.keys.each { |key| updated_metrics[key] = @metrics[key] }
  # Save metrics to the YAML log file
  File.write(log_file_path, YAML.dump(updated_metrics))
end
signature_for(example) click to toggle source
# File lib/rspec_timer.rb, line 58
def signature_for(example)
  Digest::MD5.hexdigest("#{example_path(example)}:#{example.example.instance_variable_get(:@example_block).source.to_s}")
end
start_measurement(example) click to toggle source
# File lib/rspec_timer.rb, line 25
def start_measurement(example)
  current_metrics              = metrics_for(example)
  current_metrics[:path]       = example_path(example)
  current_metrics[:start_time] = Time.now
  example
end
wipe_stored_metrics() click to toggle source
# File lib/rspec_timer.rb, line 45
def wipe_stored_metrics
  File.write(log_file_path, YAML.dump({}))
end

Private Instance Methods

example_path(example) click to toggle source
# File lib/rspec_timer.rb, line 73
def example_path(example)
  "#{example.metadata[:file_path]}:#{example.metadata[:line_number]}"
end
metrics_for(example) click to toggle source
# File lib/rspec_timer.rb, line 64
def metrics_for(example)
  @metrics[signature_for(example)] ||= {
      path:       nil,
      start_time: nil,
      end_time:   nil,
      total_time: nil
  }
end