Module: Flamegraph

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

Defined Under Namespace

Classes: FastStackSampler, Renderer, Sampler, StackProfSampler

Constant Summary collapse

VERSION =
"0.9.5"

Class Method Summary collapse

Class Method Details

.generate(filename = nil, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/flamegraph.rb', line 25

def self.generate(filename=nil, opts = {})
  fidelity = opts[:fidelity]  || 0.5

  backtraces =
    if defined? StackProf
      require "flamegraph/stackprof_sampler" unless defined? StackProfSampler
      StackProfSampler.collect(fidelity) do
        yield
      end
    elsif defined? FastStack
      FastStack.profile(fidelity) do # , opts[:mode] || :ruby) do
        yield
      end
    else
      Sampler.collect(fidelity) do
        yield
      end
    end

  embed_resources = (filename && !opts.key?(:embed_resources)) || opts[:embed_resources]

  renderer = Flamegraph::Renderer.new(backtraces)
  rendered = renderer.graph_html(embed_resources)

  if filename
    File.open(filename,"w") do |f|
      f.write(rendered)
    end
  end
  rendered
end