Sha256: 3f7c9ef2f19e6ceb588bf924b1828e22a507ad022c4b8c563b348687f23f0230

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

require 'rspec/core/formatters/base_formatter'

# Outputs how log each spec takes to run
class ProfileAllFormatter < RSpec::Core::Formatters::BaseFormatter
  RSpec::Core::Formatters.register self,
                                   :example_started, :example_passed,
                                   :start_dump

  def initialize(output)
    super(output)
    @example_times = []
  end

  def start(notification)
    super(notification)
    @output.puts 'Profiling enabled.'
  end

  def example_started(_notification)
    @time = (Time.respond_to?(:zone) && Time.zone) ? Time.zone.now : Time.now
  end

  def example_passed(notification)
    now = (Time.respond_to?(:zone) && Time.zone) ? Time.zone.now : Time.now
    @example_times << [
      notification.example.example_group.description,
      notification.example.description,
      now - @time
    ]
  end

  def start_dump(_notification)
    @output.puts "\n\nExample times:\n"

    @example_times = @example_times.sort_by do |_description, _example, time|
      time
    end.reverse

    @example_times.each do |description, example, time|
      @output.print format('%.7f', time)
      @output.puts " #{description} #{example}"
    end
    @output.flush
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zermelo-1.4.3 spec/support/profile_all_formatter.rb
zermelo-1.4.2 spec/support/profile_all_formatter.rb
zermelo-1.4.1 spec/support/profile_all_formatter.rb
zermelo-1.4.0 spec/support/profile_all_formatter.rb
zermelo-1.3.0 spec/support/profile_all_formatter.rb