class RSpec::Core::Notifications::ProfileNotification

Public Instance Methods

percentage() click to toggle source

@return [String] the percentage of total time taken

# File lib/rspec/core/notifications.rb, line 538
def percentage
  @percentage ||=
    begin
      time_taken = slow_duration / duration
      '%.1f' % ((time_taken.nan? ? 0.0 : time_taken) * 100)
    end
end
slow_duration() click to toggle source

@return [Float] the time taken (in seconds) to run the slowest examples

# File lib/rspec/core/notifications.rb, line 530
def slow_duration
  @slow_duration ||=
    slowest_examples.inject(0.0) do |i, e|
      i + e.execution_result.run_time
    end
end
slowest_examples() click to toggle source

@return [Array<RSpec::Core::Example>] the slowest examples

# File lib/rspec/core/notifications.rb, line 522
def slowest_examples
  @slowest_examples ||=
    examples.sort_by do |example|
      -example.execution_result.run_time
    end.first(number_of_examples)
end
slowest_groups() click to toggle source

@return [Array<RSpec::Core::Example>] the slowest example groups

# File lib/rspec/core/notifications.rb, line 547
def slowest_groups
  @slowest_groups ||= calculate_slowest_groups
end

Private Instance Methods

calculate_slowest_groups() click to toggle source
# File lib/rspec/core/notifications.rb, line 553
def calculate_slowest_groups
  example_groups = {}

  examples.each do |example|
    location = example.example_group.parent_groups.last.metadata[:location]

    location_hash = example_groups[location] ||= Hash.new(0)
    location_hash[:total_time]  += example.execution_result.run_time
    location_hash[:count]       += 1
    next if location_hash.key?(:description)
    location_hash[:description] = example.example_group.top_level_description
  end

  # stop if we've only one example group
  return {} if example_groups.keys.length <= 1

  example_groups.each_value do |hash|
    hash[:average] = hash[:total_time].to_f / hash[:count]
  end

  example_groups.sort_by { |_, hash| -hash[:average] }.first(number_of_examples)
end