Sha256: 2d6e92716e5806bec35731ed67ded0cfc6a1fd204e47906f4d997368b8fa3c91

Contents?: true

Size: 1.33 KB

Versions: 20

Compression:

Stored size: 1.33 KB

Contents

begin
  require "ruby-prof"
rescue LoadError => e
  Merb.fatal! "You need ruby-prof installed to use the profiler middleware", e
end

module Merb
  module Rack
    class Profiler < Merb::Rack::Middleware

      # :api: private
      def initialize(app, types = [RubyProf::ALLOCATIONS, RubyProf::PROCESS_TIME])
        super(app)
        @types = types
      end

      # :api: plugin
      def call(env)
        measure_names = { RubyProf::ALLOCATIONS => 'allocations', 
          RubyProf::PROCESS_TIME => 'time', RubyProf::MEMORY => "memory" }

        ret = nil

        GC.disable
        @types.each do |type|
          next if type.nil?
          
          if GC.respond_to?(:enable_stats)
            GC.enable_stats || GC.clear_stats
          end

          RubyProf.measure_mode = type
          RubyProf.start
          100.times do
            ret = super
          end
          result = RubyProf.stop
          printer = RubyProf::CallTreePrinter.new(result)
          path = "merb_profile_results" / env["PATH_INFO"]
          FileUtils.mkdir_p(path)
          printer.print(
            File.open(
              "#{path}/callgrind.out.#{measure_names[RubyProf::measure_mode]}",     
              'w'))

          GC.disable_stats if GC.respond_to?(:disable_stats)
        end
        GC.enable
        ret
      end

      
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
merb-core-1.1.3 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.1.2 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.1.1 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.1.0 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.1.0.rc1 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.1.0.pre lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.15 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.14 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.13 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.12 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.10 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.11 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.5 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.6.1 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.7.1 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.8.1 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.6 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.7 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.9 lib/merb-core/rack/middleware/profiler.rb
merb-core-1.0.8 lib/merb-core/rack/middleware/profiler.rb