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.3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRspecTimer



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

def initialize
  reset_metrics
end

Instance Attribute Details

#log_file_pathObject



24
25
26
# File 'lib/rspec_timer.rb', line 24

def log_file_path
  @log_file_path ||= 'rspec_metrics.yml'
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



35
36
37
38
39
40
# File 'lib/rspec_timer.rb', line 35

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



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

def reset_metrics
  @metrics = {}
end

#reset_metrics_log_fileObject



48
49
50
# File 'lib/rspec_timer.rb', line 48

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

#run_and_measure(example) ⇒ Object



42
43
44
45
46
# File 'lib/rspec_timer.rb', line 42

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

#signature_for(example) ⇒ Object



62
63
64
# File 'lib/rspec_timer.rb', line 62

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



28
29
30
31
32
33
# File 'lib/rspec_timer.rb', line 28

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



52
53
54
55
56
57
58
59
60
# File 'lib/rspec_timer.rb', line 52

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