Class: Rack::RubyProf

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prof/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RubyProf

Returns a new instance of RubyProf.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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$}, %r{\.js$}, %r{\.png$}, %r{\.jpeg$}, %r{\.jpg$}, %r{\.gif$}]
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-prof/rack.rb', line 22

def call(env)
  request = Rack::Request.new(env)

  if @skip_paths.any? {|skip_path| skip_path =~ request.path}
    @app.call(env)
  else
    result = nil
    data = ::RubyProf::Profile.profile do
      result = @app.call(env)
    end

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

    print(data, path)
    result
  end
end


41
42
43
44
45
46
47
48
49
# File 'lib/ruby-prof/rack.rb', line 41

def print(data, path)
  @printer_klasses.each do |printer_klass, base_name|
    printer = printer_klass.new(data)
    file_name = ::File.join(@tmpdir, "#{path}-#{base_name}")
    ::File.open(file_name, 'wb') do |file|
      printer.print(file, @options)
    end
  end
end