Class: Object::Profiler

Inherits:
Object show all
Defined in:
lib/object/profiler/version.rb,
lib/object/profiler/profiler.rb,
lib/object/profiler/middleware.rb

Defined Under Namespace

Classes: Middleware

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.disableObject



37
38
39
40
41
42
43
# File 'lib/object/profiler/profiler.rb', line 37

def disable
  return unless enabled?

  Process.kill("SIGINT", @pid)
  Process.wait
  @pid = nil
end

.enableObject



21
22
23
24
25
26
27
28
29
30
31
# 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?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/object/profiler/profiler.rb', line 33

def enabled?
  !!@pid
end

.report(io = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# 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