Sha256: 3bf1d6148997fcfb617f7cee6a622835fbd91ab1bd7210ce8fee88c65315734f

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8
require 'tmpdir'

module Rack
  class RubyProf
    def initialize(app, options = {})
      @app = app
      @options = options
      @options[:min_percent] ||= 1
      @tmpdir = options[:path] || Dir.tmpdir
      @printer_klasses = {::RubyProf::FlatPrinter => 'flat.txt',
                          ::RubyProf::GraphPrinter => 'graph.txt',
                          ::RubyProf::GraphHtmlPrinter => 'graph.html',
                          ::RubyProf::CallStackPrinter => 'call_stack.html'}
    end

    def call(env)
      result = nil
      data = ::RubyProf::Profile.profile do
        result = @app.call(env)
      end

      request = Rack::Request.new(env)
      path = request.path.gsub('/', '-')
      path.slice!(0)

      print(data, path)
      result
    end

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby-prof-0.11.0.rc2-x86-mingw32 lib/ruby-prof/rack.rb
ruby-prof-0.11.0.rc2 lib/ruby-prof/rack.rb