class RSpec::Core::Formatters::ProfileFormatter

@api private Formatter for providing profile output.

Attributes

output[R]

@private

Public Class Methods

new(output) click to toggle source
# File lib/rspec/core/formatters/profile_formatter.rb, line 11
def initialize(output)
  @output = output
end

Public Instance Methods

dump_profile(profile) click to toggle source

@api public

This method is invoked after the dumping the summary if profiling is enabled.

@param profile [ProfileNotification] containing duration,

slowest_examples and slowest_example_groups
# File lib/rspec/core/formatters/profile_formatter.rb, line 25
def dump_profile(profile)
  dump_profile_slowest_examples(profile)
  dump_profile_slowest_example_groups(profile)
end

Private Instance Methods

bold(text) click to toggle source
# File lib/rspec/core/formatters/profile_formatter.rb, line 62
def bold(text)
  ConsoleCodes.wrap(text, :bold)
end
dump_profile_slowest_example_groups(profile) click to toggle source
# File lib/rspec/core/formatters/profile_formatter.rb, line 44
def dump_profile_slowest_example_groups(profile)
  return if profile.slowest_groups.empty?

  @output.puts "\nTop #{profile.slowest_groups.size} slowest example groups:"
  profile.slowest_groups.each do |loc, hash|
    average = "#{bold(Helpers.format_seconds(hash[:average]))} #{bold("seconds")} average"
    total   = "#{Helpers.format_seconds(hash[:total_time])} seconds"
    count   = Helpers.pluralize(hash[:count], "example")
    @output.puts "  #{hash[:description]}"
    @output.puts "    #{average} (#{total} / #{count}) #{loc}"
  end
end
dump_profile_slowest_examples(profile) click to toggle source
# File lib/rspec/core/formatters/profile_formatter.rb, line 32
def dump_profile_slowest_examples(profile)
  @output.puts "\nTop #{profile.slowest_examples.size} slowest " \
    "examples (#{Helpers.format_seconds(profile.slow_duration)} " \
    "seconds, #{profile.percentage}% of total time):\n"

  profile.slowest_examples.each do |example|
    @output.puts "  #{example.full_description}"
    @output.puts "    #{bold(Helpers.format_seconds(example.execution_result.run_time))} " \
                 "#{bold("seconds")} #{format_caller(example.location)}"
  end
end
format_caller(caller_info) click to toggle source
# File lib/rspec/core/formatters/profile_formatter.rb, line 57
def format_caller(caller_info)
  RSpec.configuration.backtrace_formatter.backtrace_line(
    caller_info.to_s.split(':in `block').first)
end