Sha256: 93df416c036d336b33b35210a75dee9d6a2c2fef231b2943fe42171ed48ca264

Contents?: true

Size: 1.26 KB

Versions: 29

Compression:

Stored size: 1.26 KB

Contents

module Middleman
  module Profiling
    class << self
      # The profiler instance. There can only be one!
      attr_writer :profiler
      def profiler
        @profiler ||= NullProfiler.new
      end

      # Start the profiler
      def start
        profiler.start
      end

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

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

      def report(_)
      end
    end

    # A profiler that uses ruby-prof
    class RubyProfProfiler
      def initialize
        require 'ruby-prof'
      rescue LoadError
        raise "To use the --profile option, you must add the 'ruby-prof' gem to your Gemfile"
      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

29 entries across 29 versions & 2 rubygems

Version Path
middleman-core-with-external-sources-watch-fix-4.1.10 lib/middleman-core/profiling.rb
middleman-core-with-external-sources-watch-fix-4.1.0 lib/middleman-core/profiling.rb
middleman-core-4.1.14 lib/middleman-core/profiling.rb
middleman-core-4.1.13 lib/middleman-core/profiling.rb
middleman-core-4.1.12 lib/middleman-core/profiling.rb
middleman-core-4.1.11 lib/middleman-core/profiling.rb
middleman-core-4.1.10 lib/middleman-core/profiling.rb
middleman-core-4.1.9 lib/middleman-core/profiling.rb
middleman-core-4.1.8 lib/middleman-core/profiling.rb
middleman-core-4.1.7 lib/middleman-core/profiling.rb
middleman-core-4.1.6 lib/middleman-core/profiling.rb
middleman-core-4.1.5 lib/middleman-core/profiling.rb
middleman-core-4.1.3 lib/middleman-core/profiling.rb
middleman-core-4.1.2 lib/middleman-core/profiling.rb
middleman-core-4.1.1 lib/middleman-core/profiling.rb
middleman-core-4.1.0 lib/middleman-core/profiling.rb
middleman-core-4.1.0.rc.2 lib/middleman-core/profiling.rb
middleman-core-4.1.0.rc.1 lib/middleman-core/profiling.rb
middleman-core-4.0.0 lib/middleman-core/profiling.rb
middleman-core-4.0.0.rc.3 lib/middleman-core/profiling.rb