Posted over 9 years ago. Visible to the public.

Profiling Ruby with ruby-prof

require 'ruby-prof' # you don't need this if you have ruby-prof in your Gemfile

You can set one of the things you want to measure by using (default is PROCESS_TIME most useful ones are PROCESS_TIME, MEMORY, CPU_TIME):
RubyProf.measure_mode = RubyProf::PROCESS_TIME
RubyProf.measure_mode = RubyProf::WALL_TIME
RubyProf.measure_mode = RubyProf::CPU_TIME
RubyProf.measure_mode = RubyProf::ALLOCATIONS
RubyProf.measure_mode = RubyProf::MEMORY
RubyProf.measure_mode = RubyProf::GC_RUNS
RubyProf.measure_mode = RubyProf::GC_TIME

then kick off the profiler by

result = RubyProf.profile do # whatever code / method you want to profile here end

To simply print stuff in your console and be overwhelmed (especially if you have activerecord/railsy objects)

printer = printer.print(STDOUT)

Other useful printers available are:

RubyProf::FlatPrinter RubyProf::FlatPrinterWithLineNumbers RubyProf::GraphPrinter RubyProf::GraphHtmlPrinter RubyProf::CallTreePrinter RubyProf::CallStackPrinter RubyProf::MultiPrinter

I find GraphHtmlPrinter useful, use it as:

printer = # to save results to tmp/profile_data.html"tmp/profile_data.html", 'w') { |file| printer.print(file) }

open the file 'tmp/profile_data.html'

To dig deeper please RTFM

Owner of this card:

Nasir Jamal
Last edit:
over 9 years ago
Posted by Nasir Jamal to HouseTrip Deck
This website uses short-lived cookies to improve usability.
Accept or learn more