Skip to content

Instantly share code, notes, and snippets.

@oskarizu
Created January 16, 2013 11:12
Show Gist options
  • Save oskarizu/4546410 to your computer and use it in GitHub Desktop.
Save oskarizu/4546410 to your computer and use it in GitHub Desktop.
Formatter for RSpec 2 based on RspecEnhancedFormatter from https://github.com/grosser/rspec_enhanced_profile
# RSpec 2
require 'rspec/core/formatters/progress_formatter'
class NewProfile < RSpec::Core::Formatters::ProgressFormatter
SHOW_TOP = (ENV['PROFILE_SHOW_TOP'] || 20).to_i
def initialize(*args)
super
@example_times = []
end
def example_started(*args)
@time = Time.now
end
def example_passed(example)
super
@example_times << [
example_group.description,
example.description,
Time.now - @time
]
end
def start_dump
dump_example_times
@output.flush
end
private
def dump_example_times
#@output.puts "\n\nSingle examples:\n"
@output.puts "\n\nTop #{SHOW_TOP} slowest examples:\n\n"
sorted_by_time(@example_times)[0..SHOW_TOP].each do |group, example, time|
@output.puts "#{red(sprintf("%.7f", time))} #{group} #{example}"
end
end
#sorted by last ascending
def sorted_by_time(times)
times.to_a.sort_by{|x| x.last}.reverse
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment