module RuSlow

Constants

VERSION

Attributes

root[RW]

Public Instance Methods

add(name, result) click to toggle source

Add a new call to the profiler.

@param [String] name

the name of the thing to benchmark (like a label)

@param [Benchmark] result

the result of the benchmark

@return [Boolean]

true if the benchmark was added, false if the benchmark already
exists
# File lib/ru-slow.rb, line 23
def add(name, result)
  if profiler.has_key?(name)
    false
  else
    line = caller[1].split(' ', 2).first[0..-4]

    if line.include?(root)
      profiler[name] = result
      true
    else
      false
    end
  end
end
report!() click to toggle source

Generate the report.

# File lib/ru-slow.rb, line 41
def report!
  by_time = profiler.sort_by { |_, t| -t.total }
  padding = by_time.map(&:first).group_by(&:size).max.last.first.length

  puts "\n"*3
  puts "Label".ljust(padding) + ' Real'
  puts '-'*padding + '---------'

  by_time.each do |label, benchmark|
    puts label.ljust(padding) + ' ' + benchmark.real.to_s
  end
rescue => e
  Kernel.warn "There was an error running the report: #{e.message}"
end

Private Instance Methods

profiler() click to toggle source

@return [Hash]

# File lib/ru-slow.rb, line 59
def profiler
  @profiler ||= {}
end