class Object::Profiler

Constants

VERSION

Public Class Methods

disable() click to toggle source
# File lib/object/profiler/profiler.rb, line 37
def disable
  return unless enabled?

  Process.kill("SIGINT", @pid)
  Process.wait
  @pid = nil
end
enable() click to toggle source
# File lib/object/profiler/profiler.rb, line 21
def enable
  return if enabled?

  @tmpfile = Tempfile.new("object::profiler")
  @report = nil
  probe = File.join(__dir__, 'probes', 'object_create.d')
  @pid = Process.spawn "dtrace -q -s #{probe} -p #{$$} -o #{@tmpfile.path}"

  # Better way to wait for dtrace to work?
  sleep 1
end
enabled?() click to toggle source
# File lib/object/profiler/profiler.rb, line 33
def enabled?
  !!@pid
end
report(io=nil) click to toggle source
# File lib/object/profiler/profiler.rb, line 45
def report(io=nil)
  if !@report
    @tmpfile.rewind
  
    # TODO: Could this be done in the provider instead?
    results = []
    @tmpfile.read.strip.lines.each do |line|
      file_line_type, amount = line.split(" ")
      results << [amount.to_i, file_line_type]
    end
    @report = results.map { |r| "%10d %s" % r }
    @report << "%10d %s" % [results.map(&:first).inject(0, &:+), "Total"]
    @report.unshift "\n%10s %s" % ["Amount", "File:Line:Class"]

  end
  io ||= STDOUT
  io.puts @report.join("\n")
end
track(output=nil) { || ... } click to toggle source
# File lib/object/profiler/profiler.rb, line 10
def track(output=nil)
  raise "Object::Profiler.track requires a block." unless block_given?

  enable
  result = yield
  disable
  report(output)

  return result
end