ruby-prof: a code profiler for MRI rubies

https://github.com/ruby-prof/ruby-prof

1. Overview

ruby-prof is a fast code profiler for MRI Ruby. Its features include:

Speed – it is a C extension and therefore many times faster than the standard Ruby profiler.

Modes – Ruby prof can measure a number of different parameters, including call times, memory usage and object allocations.

  • Reports – can generate text and cross-referenced html reports
  • Flat Profiles – similar to the reports generated by the standard Ruby profiler
  • Graph profiles – similar to GProf, these show how long a method runs, which methods call it and which methods it calls.
  • Call tree profiles – outputs results in the calltree format suitable for the KCacheGrind profiling tool.
  • Many more – see reports section of this README.
  • Threads – supports profiling multiple threads simultaneously

2. Install

The easiest way to install ruby-prof is by using Ruby Gems. To install:

gem install ruby-prof

If you’re on windows then please install the devkit first so that it can compile.

3. Usage

There are three major options for running ruby-prof: via the command line, via its convenience API or via its core API.

3.1 ruby-prof Executable

The first is to use ruby-prof to run the Ruby program you want to profile. For more information refer to the documentation of the ruby-prof command: $ ruby-prof -h.

3.2 ruby-prof Convenience API

The second way is to use the ruby-prof convenience API to profile particular segments of code.

require 'ruby-prof'

# profile the code
RubyProf.start
# ... code to profile ...
result = RubyProf.stop

# print a flat profile to text
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)

Alternatively, you can use a block to tell ruby-prof what to profile:

require 'ruby-prof'

# profile the code
result = RubyProf.profile do
  # ... code to profile ...
end

# print a graph profile to text
printer = RubyProf::GraphPrinter.new(result)
printer.print(STDOUT, {})

ruby-prof also supports pausing and resuming profiling runs.

require 'ruby-prof'

# profile the code
RubyProf.start
# ... code to profile ...

RubyProf.pause
# ... other code ...

RubyProf.resume
# ... code to profile ...
result = RubyProf.stop

Note that resume will only work if start has been called previously. In addition, resume can also take a block:

require 'ruby-prof'

RubyProf.resume do
  # ... code to profile...
end

result = RubyProf.stop

With this usage, resume will automatically call pause at the end of the block.

References

ruby-prof/ruby-prof: ruby-prof: a code profiler for MRI rubies
https://github.com/ruby-prof/ruby-prof

打赏

Leave a Reply

Your email address will not be published. Required fields are marked *