class RSpec::Core::Notifications::ProfileNotification

The ‘ProfileNotification` holds information about the results of running a test suite when profiling is enabled. It is used by formatters to provide information at the end of the test run for profiling information.

@attr duration [Float] the time taken (in seconds) to run the suite @attr examples [Array<RSpec::Core::Example>] the examples run @attr number_of_examples [Fixnum] the number of examples to profile @attr example_groups [Array<RSpec::Core::Profiler>] example groups run

Attributes

duration[R]
examples[R]
number_of_examples[R]

Public Class Methods

new(duration, examples, number_of_examples, example_groups) click to toggle source
# File lib/rspec/core/notifications.rb, line 428
def initialize(duration, examples, number_of_examples, example_groups)
  @duration = duration
  @examples = examples
  @number_of_examples = number_of_examples
  @example_groups = example_groups
end

Public Instance Methods

percentage() click to toggle source

@return [String] the percentage of total time taken

# File lib/rspec/core/notifications.rb, line 453
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 445
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 437
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 462
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 468
def calculate_slowest_groups
  # 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

  groups = @example_groups.sort_by { |_, hash| -hash[:average] }.first(number_of_examples)
  groups.map { |group, data| [group.location, data] }
end