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'
`git clone https://github.com/brendangregg/FlameGraph.git #{File.dirname(flamegraph_pl_path)}` unless File.exist?(flamegraph_pl_path)
@benchmark = Benchmark.measure do
@result = RubyProf.profile do
@value = yield
end
end
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}`
Launchy.open(run_svg_path)
@value
end
|