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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRspecTimer

Returns a new instance of RspecTimer.



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

def initialize
  reset_metrics
end

Instance Attribute Details

#log_fileObject

Returns the value of attribute log_file.



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

def log_file
  @log_file
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



31
32
33
34
35
36
# File 'lib/rspec_timer.rb', line 31

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_file(file_name = default_metrics_file_name) ⇒ Object



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

def reset_metrics_log_file(file_name = default_metrics_file_name)
  File.write(file_name, YAML.dump({}))
end

#run_and_measure(example) ⇒ Object



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

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

#signature_for(example) ⇒ Object



58
59
60
# 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) ⇒ Object



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

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_file(file_name = default_metrics_file_name) ⇒ Object



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

def update_metrics_log_file(file_name = default_metrics_file_name)
  updated_metrics = {}
  # Load any existing metrics
  updated_metrics = YAML.load_file(file_name) if File.exists? (file_name)
  # 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(file_name, YAML.dump(updated_metrics))
end