Module: Pione::Util::Profiler

Defined in:
lib/pione/util/profiler.rb

Overview

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.targetsObject (readonly)

Returns the value of attribute targets.



8
9
10
# File 'lib/pione/util/profiler.rb', line 8

def targets
  @targets
end

Class Method Details

.initObject

Initialize profiler.



11
12
13
14
15
16
17
18
19
20
21
22
23
# 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, &b) ⇒ Object

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



27
28
29
30
31
32
33
34
# 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_reportsObject

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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