module Pione::Util::Profiler

Util::Profiler is a customized profiler based on ruby-prof.

Attributes

targets[R]

Public Class Methods

init() click to toggle source

Initialize profiler.

# File lib/pione/util/profiler.rb, line 11
def init
  # load ruby-prof
  require "ruby-prof"

  # set profiler variables
  @profile = true
  @reports = Array.new
  @targets = Array.new
  @date = Time.now.strftime("%Y%m%d%H%M%S")

  # set finalizer
  ::Kernel.at_exit { write_reports }
end
profile(report) { || ... } click to toggle source

Take profile within the block. If the report doesn’t have available target name, executed the block without profile.

# File lib/pione/util/profiler.rb, line 27
def profile(report, &b)
  if @profile and @targets.include?(report.name)
    report.result = RubyProf.profile(&b)
    @reports << report
  else
    yield
  end
end
write_reports() click to toggle source

Write profile reports. They are generated at profile report directory(see Global.profile_report_directory).

# File lib/pione/util/profiler.rb, line 38
def write_reports
  if @profile
    # create profile directory
    profile_dir = Global.profile_report_directory
    unless profile_dir.exist?
       profile_dir.mkdir
    end

    # generate reports
    @reports.group_by{|report| report.class}.each do |_, reports|
      reports.each_with_index do |report, i|
        path = profile_dir + ("%s_%s_%s_%d.txt" % [@date, Process.pid, report.name, i])
        path.open("w") do |out|
          report.headers.each do |name, value|
            out.puts "%s: %s" % [name, value]
          end
          out.puts "-" * 50
          RubyProf::FlatPrinter.new(report.result).print(out, :min_percent => 1)
        end
      end
    end

    # clear reports
    @reports.clear
  end
end