Class: RubyProf::FlameGraphPrinter

Inherits:
AbstractPrinter
  • Object
show all
Defined in:
lib/ruby-prof/printers/flame_graph_printer.rb

Overview

wow much flame graph many stack wow!!

Constant Summary collapse

VERSION =
'0.3.0'

Instance Method Summary collapse

Instance Method Details

#min_timeObject


34
35
36
# File 'lib/ruby-prof/printers/flame_graph_printer.rb', line 34

def min_time
  0
end

#name(call_info) ⇒ Object


55
56
57
58
# File 'lib/ruby-prof/printers/flame_graph_printer.rb', line 55

def name(call_info)
  method = call_info.target
  "#{method.full_name} (#{call_info.called})"
end

#number(x) ⇒ Object


60
61
62
# File 'lib/ruby-prof/printers/flame_graph_printer.rb', line 60

def number(x)
  ("%.6f" % x).sub(/\.0*$/, '').sub(/(\..*?)0+$/, '\1')
end

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby-prof/printers/flame_graph_printer.rb', line 12

def print(output = STDOUT, options = {})
  @output = output
  setup_options(options)

  @overall_threads_time = @result.threads.reduce(0) { |a, thread| a + thread.total_time }

  @result.threads.each do |thread|
    @current_thread_id = thread.fiber_id
    @overall_time = thread.total_time
    start = []
    start << "Thread:#{thread.id}"
    start << "Fiber:#{thread.fiber_id}" unless thread.id == thread.fiber_id
    thread.methods.each do |m|
      next unless m.root?
      m.call_infos.each do |ci|
        next unless ci.root?
        print_stack start, ci
      end
    end
  end
end

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby-prof/printers/flame_graph_printer.rb', line 38

def print_stack(prefix, call_info)

  total_time = call_info.total_time
  percent_total = (total_time/@overall_time)*100
  return unless percent_total > min_percent
  return unless total_time >= min_time

  kids = call_info.children
  current = prefix + [name(call_info)]
  @output.puts "#{current.join(';')} #{number call_info.self_time * 1e3}"

  kids.each do |child|
    print_stack current, child
  end

end