Class: RSpecTelemetry::FactoryAggregation::Accumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_telemetry/factory_aggregation.rb

Instance Method Summary collapse

Constructor Details

#initializeAccumulator

Returns a new instance of Accumulator.



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

def initialize
  @rows = {}
end

Instance Method Details

#add(factory:, strategy:, duration_ms:, self_duration_ms: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rspec_telemetry/factory_aggregation.rb', line 17

def add(factory:, strategy:, duration_ms:, self_duration_ms: nil)
  key = "#{factory}:#{strategy}"
  total = duration_ms.to_f
  self_ms = (self_duration_ms || duration_ms).to_f
  row = (@rows[key] ||= {factory: factory, strategy: strategy, count: 0, total: 0.0, self: 0.0, max: 0.0})
  row[:count] += 1
  row[:total] += total
  row[:self] += self_ms
  row[:max] = total if total > row[:max]
  self
end

#statsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec_telemetry/factory_aggregation.rb', line 29

def stats
  @rows.map do |key, row|
    Stat.new(
      key: key,
      factory: row[:factory],
      strategy: row[:strategy],
      count: row[:count],
      total_ms: row[:total],
      self_total_ms: row[:self],
      max_ms: row[:max]
    )
  end
end

#top(limit = nil) ⇒ Object



43
44
45
46
47
# File 'lib/rspec_telemetry/factory_aggregation.rb', line 43

def top(limit = nil)
  # Rank by self time so nested factories are not double-counted.
  ranked = stats.sort_by { |stat| -stat.self_total_ms }
  limit ? ranked.first(limit) : ranked
end