Sha256: 58505ae93c99fabcc8dae9e9cefafb7581de1c9c848a6520c75d7bfd632f38ea

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

require "json"

if RUBY_VERSION >= "2.1.0".freeze
  begin
    require "stackprof"
    require "flamegraph/stackprof_sampler"
  rescue
    STDERR.puts "Please require the stackprof gem falling back to fast_stack"
    require "fast_stack"
  end
else
  begin
    require "fast_stack"
  rescue
    STDERR.puts "Please require the fast_stack gem, note flamegraph is only supported on Ruby 2.0 and above"
  end
end

require "flamegraph/version"
require "flamegraph/renderer"

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

    backtraces =
      if defined? StackProf
        StackProfSampler.collect(fidelity) do
          yield
        end
      else
        FastStack.profile(fidelity) do # , opts[:mode] || :ruby) 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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flamegraph-0.0.7 lib/flamegraph.rb
flamegraph-0.0.6 lib/flamegraph.rb