Sha256: 9664b3f9ec00fb55f741a1d47b72d89fd59da33a3983455c4b470042659ea6b4

Contents?: true

Size: 1.3 KB

Versions: 16

Compression:

Stored size: 1.3 KB

Contents

module Middleman
  module Profiling

    # The profiler instance. There can only be one!
    def self.profiler=(prof)
      @profiler = prof
    end
    def self.profiler
      @profiler ||= NullProfiler.new
    end

    # Start the profiler
    def self.start
      profiler.start
    end

    # Stop the profiler and generate a report. Make sure to call start first
    def self.report(report_name)
      profiler.report(report_name)
    end

    # A profiler that does nothing. The default.
    class NullProfiler
      def start
      end

      def report(report_name)
      end
    end

    # A profiler that uses ruby-prof
    class RubyProfProfiler
      def initialize
        begin
          require 'ruby-prof'
        rescue LoadError
          raise "To use the --profile option, you must add the 'ruby-prof' gem to your Gemfile"
        end
      end

      def start
        RubyProf.start
      end

      def report(report_name)
        result = RubyProf.stop

        printer = RubyProf::GraphHtmlPrinter.new(result)
        outfile = File.join("profile", report_name)
        outfile = (outfile + '.html') unless outfile.end_with? '.html'
        FileUtils.mkdir_p(File.dirname(outfile))
        File.open(outfile, 'w') do |f|
          printer.print(f, :min_percent=> 1)
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
middleman-core-3.2.1 lib/middleman-core/profiling.rb
middleman-core-3.2.0 lib/middleman-core/profiling.rb
middleman-core-3.1.6 lib/middleman-core/profiling.rb
middleman-core-3.1.5 lib/middleman-core/profiling.rb
middleman-core-3.1.4 lib/middleman-core/profiling.rb
middleman-core-3.1.3 lib/middleman-core/profiling.rb
middleman-core-3.1.2 lib/middleman-core/profiling.rb
middleman-core-3.1.1 lib/middleman-core/profiling.rb
middleman-core-3.1.0 lib/middleman-core/profiling.rb
middleman-core-3.1.0.rc.4 lib/middleman-core/profiling.rb
middleman-core-3.1.0.rc.3 lib/middleman-core/profiling.rb
middleman-core-3.1.0.rc.2 lib/middleman-core/profiling.rb
middleman-core-3.1.0.rc.1 lib/middleman-core/profiling.rb
middleman-core-3.1.0.beta.3 lib/middleman-core/profiling.rb
middleman-core-3.1.0.beta.2 lib/middleman-core/profiling.rb
middleman-core-3.1.0.beta.1 lib/middleman-core/profiling.rb