Class: CucumberStatistics::StepStatistics

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber_statistics/step_statistics.rb

Instance Method Summary collapse

Constructor Details

#initializeStepStatistics

Returns a new instance of StepStatistics.



3
4
5
# File 'lib/cucumber_statistics/step_statistics.rb', line 3

def initialize
  @all = Hash.new
end

Instance Method Details

#allObject



37
38
39
# File 'lib/cucumber_statistics/step_statistics.rb', line 37

def all
  @all
end

#average_times_plot_dataObject



58
59
60
# File 'lib/cucumber_statistics/step_statistics.rb', line 58

def average_times_plot_data
  @all.map {|step_name, data| data[:average].to_f}.sort.reverse
end

#calculateObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cucumber_statistics/step_statistics.rb', line 24

def calculate
  @all.each do |step_name, step_results|
    step_results[:total] = step_results[:instances].inject{|sum,x| sum + x }
    step_results[:count] = step_results[:instances].count
    step_results[:average] = step_results[:total].to_f / step_results[:count].to_f
    step_results[:fastest] = step_results[:instances].sort.first
    step_results[:slowest] = step_results[:instances].sort.last
    step_results[:variation] = step_results[:slowest] - step_results[:fastest]
    step_results[:variance] = self.sample_variance step_results[:instances]
    step_results[:standard_deviation] = self.standard_deviation step_results[:variance]
  end
end

#highest_averageObject



46
47
48
# File 'lib/cucumber_statistics/step_statistics.rb', line 46

def highest_average
  sort_by_property(:average).reverse.first
end

#highest_totalObject



50
51
52
# File 'lib/cucumber_statistics/step_statistics.rb', line 50

def highest_total
  sort_by_property(:total).reverse.first
end

#highest_variationObject



54
55
56
# File 'lib/cucumber_statistics/step_statistics.rb', line 54

def highest_variation
  sort_by_property(:variation).reverse.first
end

#record(step_name, duration, file_colon_line) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cucumber_statistics/step_statistics.rb', line 7

def record step_name, duration, file_colon_line

  # "/Users/kross/alienfast/acme/features/account management/admin_cancel_account.feature:8"
  step_results = @all[step_name]
  step_results ||= Hash.new
  step_results[:instances] ||= []
  step_results[:instances] << duration
  begin
    file = file_colon_line[file_colon_line.index('features')..-1]
    step_results[:file] = file
  rescue Exception => e
    step_results[:file] = e.message
  end

  @all[step_name] ||= step_results
end

#sample_variance(data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/cucumber_statistics/step_statistics.rb', line 74

def sample_variance data
  count = data.count
  average = data.inject{|sum,x| sum + x } / count.to_f

  return nil if count <= 1
  
  sum = data.inject(0){|acc,i|acc.to_f + (i.to_f - average)**2.0}

  return 1 / (count.to_f - 1.0) * sum.to_f
end

#sort_by_property(property) ⇒ Object



41
42
43
44
# File 'lib/cucumber_statistics/step_statistics.rb', line 41

def sort_by_property property
  result = @all.sort {|a,b| a.last[property.to_sym] <=> b.last[property.to_sym]}
  result
end

#standard_deviation(sample_variance) ⇒ Object



85
86
87
88
89
# File 'lib/cucumber_statistics/step_statistics.rb', line 85

def standard_deviation sample_variance
  return nil if sample_variance.nil?
  
  return Math.sqrt(sample_variance)
end

#step_part_of_totalObject



66
67
68
# File 'lib/cucumber_statistics/step_statistics.rb', line 66

def step_part_of_total
  @all.map {|step_name, data| data[:total]}.sort.reverse
end

#total_elapsed_timeObject



70
71
72
# File 'lib/cucumber_statistics/step_statistics.rb', line 70

def total_elapsed_time
  @all.map {|step_name, data| data[:total]}.inject{|sum,x| sum + x }
end

#total_times_plot_dataObject



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

def total_times_plot_data
  sort_by_property(:average).reverse.map {|step_name, data| data[:total].to_f}
end