Sha256: 359b30d73a4cf529d9616a7a47df85a474f4a1c61d13150a0e4674bb4ebc461f

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module Speedup
  module Collectors
    class RubyprofCollector < Collector

      def initialize(options={})
        require 'ruby-prof'
        @results_dir = Rails.root.join('tmp', 'rubyprof')
        Dir.mkdir( @results_dir ) unless File.directory?(@results_dir)
        super
      end

      def parse_options
        @profile_request = !!@options[:profile_request]
      end

      # The data results that are inserted at the end of the request for use in
      # deferred placeholders in the Peek the bar.
      #
      # Returns Hash.
      def results
        {}
      end

      def setup_subscribes
        if enabled? && @profile_request
          before_request do
            start_prof
          end
          after_request do
            end_prof
          end
        end
      end


      def filter_event?(evt)
        super || evt.payload[:controller].start_with?('Speedup')
      end

      def start_prof
        RubyProf.start
      end

      def end_prof(result_id=nil)
        result = RubyProf.stop

        Speedup.request.store_event(key, result_id )

        # Print a flat profile to text
        printer = printer_klass.new(result)
        ::File.open(@results_dir.join( Speedup.request.id + result_id.to_s ), 'wb') do |file|
          printer.print(file)
        end
      end

      def profile(result_id=nil, &block)
        start_prof
        yield
        end_prof(next_id)
      end

      private

        def next_id
          @next_id = @next_id.to_i + 1
        end

        def printer_klass
          # RubyProf::GraphHtmlPrinter
          RubyProf::CallStackPrinter
        end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
speedup-rails-0.0.12 lib/speedup/collectors/rubyprof_collector.rb
speedup-rails-0.0.10 lib/speedup/collectors/rubyprof_collector.rb