Skip to content

Instantly share code, notes, and snippets.

@lporras
Last active August 7, 2019 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lporras/317d25bb0641ad3426851ea06eb8c3ea to your computer and use it in GitHub Desktop.
Save lporras/317d25bb0641ad3426851ea06eb8c3ea to your computer and use it in GitHub Desktop.
Profiling with Ruby-Prof

Add gem in Gemfile:

group :test, :development do
  gem 'ruby-prof', '0.18.0'
end

add example file in: lib/scripts/test_provider.rb

require_relative '../../config/environment'

document = Document.where("name IS NOT NULL").last

DevelopmentProfiler.prof('junk') do
  100.times do
    puts StorageProvider::settings(document)
  end
end

add lib file in: lib/development_profiler.rb

class DevelopmentProfiler

  def self.prof(file_name)
    if Rails.env.development?
      RubyProf.start
      yield
      results = RubyProf.stop
      RubyProf::CallTreePrinter.new(results).print({print_file: true})
    else
      yield
    end

  end

end

Run in console:

ruby lib/scripts/storage_provider_script.rb

This will generate a profile files, example:

profile.callgrind.out.84797
profile.callgrind.out.84797.70349423984460
profile.callgrind.out.84797.70349436691660

You can now open the file profile.callgrind.out.84797 using command line tool: qcachegrind Installed using brew install qcachegrind in mac

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