Class: RspecTimer

Inherits:
Object
  • Object
show all
Extended by:
SingleForwardable
Includes:
Singleton
Defined in:
lib/rspec_timer.rb,
lib/rspec_timer/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRspecTimer

Returns a new instance of RspecTimer.



16
17
18
19
# File 'lib/rspec_timer.rb', line 16

def initialize
  reset_metrics
  @log_file_path ||= 'rspec_metrics.yml'
end

Instance Attribute Details

#log_file_path=(value) ⇒ Object (writeonly)

Sets the attribute log_file_path

Parameters:

  • value

    the value to set the attribute log_file_path to.



14
15
16
# File 'lib/rspec_timer.rb', line 14

def log_file_path=(value)
  @log_file_path = value
end

#metricsObject (readonly)

Returns the value of attribute metrics.



13
14
15
# File 'lib/rspec_timer.rb', line 13

def metrics
  @metrics
end

Instance Method Details

#end_measurement(example) ⇒ Object



32
33
34
35
36
37
# 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_metricsObject



21
22
23
# File 'lib/rspec_timer.rb', line 21

def reset_metrics
  @metrics = {}
end

#reset_metrics_log_fileObject



45
46
47
# File 'lib/rspec_timer.rb', line 45

def reset_metrics_log_file
  File.write(log_file_path, YAML.dump({}))
end

#run_and_measure(example) ⇒ Object



39
40
41
42
43
# File 'lib/rspec_timer.rb', line 39

def run_and_measure(example)
  start_measurement(example)
  example.run
  end_measurement(example)
end

#signature_for(example) ⇒ Object



59
60
61
# File 'lib/rspec_timer.rb', line 59

def signature_for(example)
  Digest::MD5.hexdigest("#{example_path(example)}:#{example.example.instance_variable_get(:@example_block).source.to_s}")
end

#start_measurement(example) ⇒ Object



25
26
27
28
29
30
# 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

#update_metrics_log_fileObject



49
50
51
52
53
54
55
56
57
# File 'lib/rspec_timer.rb', line 49

def update_metrics_log_file
  updated_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