module Flameboyant

rubocop:disable all

Constants

GRAPHER

ensure directory exists

VERSION

Public Instance Methods

create_html_page(dst_svg) click to toggle source
# File lib/flameboyant.rb, line 49
  def create_html_page(dst_svg)
    basename = File.basename(dst_svg)
    <<-HTML
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1">
    <title>Flame: #{basename}</title>

    <style type="text/css">
      html, body {
        padding: 0;
        margin: 0;
      }

      .full-width {
        max-width: 100%;
        width: 100%;
        height: auto;
      }
    </style>
  </head>
  <body>
  <object class="full-width" type="image/svg+xml" data="#{URI.encode(basename)}">Your browser does not support SVGs</object>
  </body>
</html>
    HTML
  end
dest_dir() click to toggle source
# File lib/flameboyant.rb, line 78
def dest_dir
  if defined? Rails
    Rails.root.join('tmp', 'flames')
  else
    Dir.pwd
  end
end
log(msg) click to toggle source
# File lib/flameboyant.rb, line 90
def log(msg)
  full_msg = "[flame] #{msg}"
  Rails.logger.info full_msg if defined? Rails
  $stderr.puts full_msg
end
profile(name: nil, width: 1920, &block) click to toggle source
# File lib/flameboyant.rb, line 12
def profile(name: nil, width: 1920, &block)
  base_name = "#{name}_#{timestamp}"

  block_result = nil
  log 'starting profile'
  result = RubyProf.profile do
    block_result = block.call
  end

  # print a graph profile to text
  printer = RubyProf::FlameGraphPrinter.new(result)

  FileUtils.mkdir_p(dest_dir)

  dst_data = dest_dir.join("#{base_name}.txt")
  dst_svg = dest_dir.join("#{base_name}.svg")
  dst_html = dest_dir.join("#{base_name}.html")

  log "writing: #{dst_data}"
  File.open(dst_data, 'w') do |f|
    printer.print(f, {})
  end

  log 'generating SVG'
  if system("#{GRAPHER} --countname=ms --width=#{width} #{dst_data} > #{dst_svg}")
    log "created: #{dst_svg}"
    log "removing: #{dst_data}"
    FileUtils.rm(dst_data)
  end
  # write html contents
  File.open(dst_html, 'w') {|f| f << create_html_page(dst_svg)}
  log "created: #{dst_html} <⌘ - Click> to open in your browser."

  # return original results
  block_result
end
timestamp() click to toggle source
# File lib/flameboyant.rb, line 86
def timestamp
  '%.04f' % Time.now.to_f
end