class Rack::RubyProf

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/ruby-prof/rack.rb, line 6
def initialize(app, options = {})
  @app = app
  @options = options
  @options[:min_percent] ||= 1

  @tmpdir = options[:path] || Dir.tmpdir
  FileUtils.mkdir_p(@tmpdir)

  @printer_klasses = @options[:printers]  || {::RubyProf::FlatPrinter => 'flat.txt',
                                              ::RubyProf::GraphPrinter => 'graph.txt',
                                              ::RubyProf::GraphHtmlPrinter => 'graph.html',
                                              ::RubyProf::CallStackPrinter => 'call_stack.html'}

  @skip_paths = options[:skip_paths] || [%r{^/assets}, %r{\.(css|js|png|jpeg|jpg|gif)$}]
  @only_paths = options[:only_paths]
end

Public Instance Methods

call(env) click to toggle source
# File lib/ruby-prof/rack.rb, line 23
def call(env)
  request = Rack::Request.new(env)

  if should_profile?(request.path)
    begin
      result = nil
      data = ::RubyProf::Profile.profile(profiling_options) do
        result = @app.call(env)
      end

      path = request.path.gsub('/', '-')
      path.slice!(0)

      print(data, path)
      result
    end
  else
    @app.call(env)
  end
end

Private Instance Methods

paths_match?(path, paths) click to toggle source
# File lib/ruby-prof/rack.rb, line 52
def paths_match?(path, paths)
  paths.any? { |skip_path| skip_path =~ path }
end
print(data, path) click to toggle source
profiling_options() click to toggle source
# File lib/ruby-prof/rack.rb, line 56
def profiling_options
  options = {}
  options[:measure_modes] = [::RubyProf.measure_mode]
  options[:exclude_threads] =
    if @options[:ignore_existing_threads]
      Thread.list.select{|t| t != Thread.current}
    else
      ::RubyProf.exclude_threads
    end
  if @options[:request_thread_only]
    options[:include_threads] = [Thread.current]
  end
  if @options[:merge_fibers]
    options[:merge_fibers] = true
  end
  options
end
should_profile?(path) click to toggle source
# File lib/ruby-prof/rack.rb, line 46
def should_profile?(path)
  return false if paths_match?(path, @skip_paths)

  @only_paths ? paths_match?(path, @only_paths) : true
end