Module: QProf

Defined in:
lib/qprof.rb,
lib/qprof/version.rb

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.call(title = 'Flame Graph') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/qprof.rb', line 12

def call(title = 'Flame Graph')
  FileUtils.mkdir_p('/tmp/qprof')
  run = "#{Time.now.to_i}-#{SecureRandom.hex(4)}"
  run_txt_path = "/tmp/qprof/#{run}.txt"
  run_svg_path = "/tmp/qprof/#{run}.svg"
  flamegraph_pl_path = '/tmp/qprof/FlameGraph/flamegraph.pl'

  # pull flamegraph if it's needed
  `git clone https://github.com/brendangregg/FlameGraph.git #{File.dirname(flamegraph_pl_path)}` unless File.exist?(flamegraph_pl_path)

  # run block
  @benchmark = Benchmark.measure do
    @result = RubyProf.profile do
      @value = yield
    end
  end

  # generate graph
  subtitle = i[utime stime real].map { |x| "#{x} = #{@benchmark.send(x).round(4)}s" }.join('; ')
  printer = RubyProf::FlameGraphPrinter.new(@result)
  File.open(run_txt_path, 'w') { |file| printer.print(file) }
  `cat #{run_txt_path} | #{flamegraph_pl_path} --title \"#{title}\" --subtitle \"#{subtitle}\" > #{run_svg_path}`

  # open in browser
  Launchy.open(run_svg_path)

  # return any wrapped value
  @value
end