Skip to content

Instantly share code, notes, and snippets.

@hiltmon
Created February 28, 2012 03:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hiltmon/1929287 to your computer and use it in GitHub Desktop.
Save hiltmon/1929287 to your computer and use it in GitHub Desktop.
Simple class to wrap a profile run around some code
class DevelopmentProfiler
def self.prof(file_name)
RubyProf.start
yield
results = RubyProf.stop
# Print a flat profile to text
File.open "#{Rails.root}/tmp/performance/#{file_name}-graph.html", 'w' do |file|
RubyProf::GraphHtmlPrinter.new(results).print(file)
end
File.open "#{Rails.root}/tmp/performance/#{file_name}-flat.txt", 'w' do |file|
# RubyProf::FlatPrinter.new(results).print(file)
RubyProf::FlatPrinterWithLineNumbers.new(results).print(file)
end
File.open "#{Rails.root}/tmp/performance/#{file_name}-stack.html", 'w' do |file|
RubyProf::CallStackPrinter.new(results).print(file)
end
end
end
@benoittgt
Copy link

Thanks for it. You just have to create the folder /tmp/performance/ before

@rjswenson
Copy link

module DevelopmentProfiler
  def self.prof(file_name)
    # create dir automatically
    profiling_dir = File.join(Rails.root, 'tmp', 'profiler' )
    FileUtils.mkdir_p profiling_dir unless File.exist? profiling_dir

    require 'ruby-prof'
    RubyProf.start
    yield
    results = RubyProf.stop

    # Print a flat profile to text
    File.open(File.join(profiling_dir, "#{file_name}-#{Time.now.to_i}-graph.html"), 'w') do |file|
      RubyProf::GraphHtmlPrinter.new(results).print(file)
    end

    File.open(File.join(profiling_dir, "#{file_name}-#{Time.now.to_i}-flat.txt"), 'w') do |file|
      # RubyProf::FlatPrinter.new(results).print(file)
      RubyProf::FlatPrinterWithLineNumbers.new(results).print(file)
    end

    File.open(File.join(profiling_dir, "#{file_name}-#{Time.now.to_i}-stack.html"), 'w') do |file|
      RubyProf::CallStackPrinter.new(results).print(file)
    end
  end
end

#  DevelopmentProfiler::prof("import") do
#    Your slow code here
#  end

@lukes
Copy link

lukes commented Feb 28, 2018

# This version keeps output files together in the same directory suffixed with the time profiling completed,
# and also handles stopping the profiler if exceptions are raised within the code being profiled.
#
# E.g.:
#   tmp/profilers/my-profiled-code-1519860540/graph.html
#                                             flat.html
#                                             stack.html
#
# You can also pass a `false` argument to toggle when the profiler runs.
#
# E.g.: 
#   RubyProfiler.prof('slow-method', id == 1) do ...

module RubyProfiler

  DIR = File.join(Rails.root, 'tmp', 'profiler' )

  def self.prof(file_name, enabled=true)
    return yield unless enabled

    # create dir automatically
    FileUtils.mkdir_p DIR unless File.exist? DIR

    require 'ruby-prof'

    RubyProf.start

    begin
      yield
    rescue => e
      RubyProf.stop
      raise e
    end

    results = RubyProf.stop

    subdir = File.join(DIR, "#{file_name}-#{Time.now.to_i}")
    FileUtils.mkdir_p subdir unless File.exist? subdir

    # Print a flat profile to text
    File.open(File.join(subdir, "graph.html"), 'w') do |file|
      RubyProf::GraphHtmlPrinter.new(results).print(file)
    end

    File.open(File.join(subdir, "flat.txt"), 'w') do |file|
      # RubyProf::FlatPrinter.new(results).print(file)
      RubyProf::FlatPrinterWithLineNumbers.new(results).print(file)
    end

    File.open(File.join(subdir, "stack.html"), 'w') do |file|
      RubyProf::CallStackPrinter.new(results).print(file)
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment