• Stars
    star
    1,027
  • Rank 43,327 (Top 0.9 %)
  • Language
    C
  • Created almost 15 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

gperftools for ruby code

perftools.rb

gperftools for ruby code
(c) 2012 Aman Gupta (tmm1)
http://www.ruby-lang.org/en/LICENSE.txt

gperftools (formerly known as google-perftools): http://gperftools.googlecode.com

Usage (in a webapp)

Use rack-perftools_profiler:

require 'rack/perftools_profiler'
config.middleware.use ::Rack::PerftoolsProfiler, :default_printer => 'gif'

Simply add profile=true to profile a request:

curl -o 10_requests_to_homepage.gif "http://localhost:3000/homepage?profile=true&times=10"

Usage (from Ruby)

Run the profiler with a block:

require 'perftools'
PerfTools::CpuProfiler.start("/tmp/add_numbers_profile") do
  5_000_000.times{ 1+2+3+4+5 }
end

Start and stop the profiler manually:

require 'perftools'
PerfTools::CpuProfiler.start("/tmp/add_numbers_profile")
5_000_000.times{ 1+2+3+4+5 }
PerfTools::CpuProfiler.stop

Usage (externally)

Profile an existing ruby application without modifying it:

$ CPUPROFILE=/tmp/my_app_profile \
  RUBYOPT="-r`gem which perftools | tail -1`" \
  ruby my_app.rb

Profiler Modes

The profiler can be run in one of many modes, set via an environment variable before the library is loaded:

  • CPUPROFILE_REALTIME=1

    Use walltime instead of cputime profiling. This will capture all time spent in a method, even if it does not involve the CPU.

    For example, sleep() is not expensive in terms of cputime, but very expensive in walltime. walltime will also show functions spending a lot of time in network i/o.

  • CPUPROFILE_OBJECTS=1

    Profile object allocations instead of cpu/wall time. Each sample represents one object created inside that function.

  • CPUPROFILE_METHODS=1

    Profile method calls. Each sample represents one method call made inside that function.

The sampling interval of the profiler can be adjusted to collect more (for better profile detail) or fewer samples (for lower overhead):

  • CPUPROFILE_FREQUENCY=500

    Default sampling interval is 100 times a second. Valid range is 1-4000

Reporting

pprof.rb --text /tmp/add_numbers_profile

pprof.rb --pdf /tmp/add_numbers_profile > /tmp/add_numbers_profile.pdf

pprof.rb --gif /tmp/add_numbers_profile > /tmp/add_numbers_profile.gif

pprof.rb --callgrind /tmp/add_numbers_profile > /tmp/add_numbers_profile.grind
kcachegrind /tmp/add_numbers_profile.grind

pprof.rb --gif --focus=Integer /tmp/add_numbers_profile > /tmp/add_numbers_custom.gif

pprof.rb --text --ignore=Gem /tmp/my_app_profile

For more options, see pprof documentation

Examples

pprof.rb --text

Total: 1735 samples
    1487  85.7%  85.7%     1735 100.0% Integer#times
     248  14.3% 100.0%      248  14.3% Fixnum#+

pprof.rb --gif

Installation

Just install the gem, which will download, patch and compile gperftools for you:

sudo gem install perftools.rb

Or build your own gem:

git clone git://github.com/tmm1/perftools.rb
cd perftools.rb
gem build perftools.rb.gemspec
gem install perftools.rb

Use via a Gemfile:

gem 'perftools.rb', :git => 'git://github.com/tmm1/perftools.rb.git'

You'll also need graphviz to generate call graphs using dot:

brew    install graphviz ghostscript # osx
sudo apt-get install graphviz ps2pdf      # debian/ubuntu

If graphviz fails to build on OSX Lion, you may need to recompile libgd, see here

Advantages over ruby-prof

  • Sampling profiler

    • perftools samples your process using setitimer() so it can be used in production with minimal overhead.

Profiling the Ruby VM and C extensions

To profile C code, download and build an unpatched perftools (libunwind or ./configure --enable-frame-pointers required on x86_64).

Download:

wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.8/gperftools-2.8.tar.gz
tar zxvf gperftools-*.tar.gz
cd gperftools-*

Compile:

./configure --prefix=/opt
make
sudo make install

Profile:

export LD_PRELOAD=/opt/lib/libprofiler.so                 # for linux
export DYLD_INSERT_LIBRARIES=/opt/lib/libprofiler.dylib   # for osx
CPUPROFILE=/tmp/ruby_interpreter.profile ruby -e' 5_000_000.times{ "hello world" } '

Report:

pprof `which ruby` --text /tmp/ruby_interpreter.profile

TODO

  • Add support for heap profiling to find memory leaks (PerfTools::HeapProfiler)
  • Allow both C and Ruby profiling
  • Add setter for the sampling interval

Resources

More Repositories

1

stackprof

a sampling call-stack profiler for ruby 2.2+
Ruby
2,051
star
2

rbtrace

like strace, but for ruby code
Ruby
1,691
star
3

rblineprof

line-profiler for ruby
C
771
star
4

test-queue

parallel test runner for CI environments
Ruby
572
star
5

emoji-extractor

extracts high-resolution emoji pngs from /System/Library/Fonts/Apple Color Emoji.ttf
Ruby
548
star
6

ripper-tags

fast, accurate ctags generator for ruby source code using Ripper
Ruby
544
star
7

gctools

profiler/logger/oobgc for rgengc in ruby 2.1
C
258
star
8

hotspots

a graphical view into your rails app's performance
JavaScript
199
star
9

brew2deb

homebrew + fpm = debian packages
Ruby
109
star
10

sinbook

simple sinatra facebook extension in 300 lines of ruby
Ruby
84
star
11

graphite

git-bzr mirror of graphite trunk
Python
81
star
12

ruby-hacking-guide

ruby hacking guide english translation
Ruby
77
star
13

em-mysql

Async MySQL driver for Ruby/Eventmachine
Ruby
76
star
14

flyapp-mastodon

mastodon on fly.io
Dockerfile
68
star
15

jssocket

generic javascript socket API
JavaScript
57
star
16

em-spec

Simple BDD API for testing asynchronous Ruby/EventMachine code
Ruby
56
star
17

xmpp4em

EventMachine based XMPP client
Ruby
54
star
18

taxsim.app

US Income Tax simulator, built on NBER TAXSIM and taxsim.js
JavaScript
45
star
19

rmongo

Ruby/Eventmachine driver for 10gen's object database Mongo
Ruby
36
star
20

cycript

my personal git fork of http://svn.saurik.com/repos/cycript/trunk/
C++
27
star
21

taxsim.js

JS/WebAssembly version of NBER TAXSIM
HTML
21
star
22

taxes.cue

tax organizer and calculator in #cuelang
CUE
18
star
23

fiber18

API compatible Thread based Fiber implementation for Ruby 1.8
Ruby
18
star
24

ruby_posix_mq

Unofficial ruby_posix_mq Mirror. Updated semi-regularly.
Ruby
18
star
25

mailfactory

Create MIME email messages with multiple body parts and attachments in Ruby
Ruby
17
star
26

ruby187

fork of ruby 1.8.7p72 with branches for various gc and thread related patches
Ruby
16
star
27

apthunter

greasemonkey-style chrome extension to add keyboard navigation, stars, notes and inline images/maps to craigslist apartment listings
JavaScript
16
star
28

raindrops

my personal fork of http://repo.or.cz/w/raindrops.git
Ruby
14
star
29

nginxr

Nginxr is a ruby wrapper for nginx config file by oleganza
Ruby
12
star
30

memprof.com

memprof.com
JavaScript
11
star
31

ruby-collectd

Send collectd statistics from your Ruby script
Ruby
8
star
32

rvm.deb

debian package for rvm
8
star
33

ctags.rb

ruby wrapper for exuberant-ctags
Ruby
7
star
34

macvim_vimshell

macvim + vimshell patch
C
6
star
35

lingo

5
star
36

tablo-for-channels

M3U generator optimized for Channels' custom channels.
Go
5
star
37

async_sequel

asynchronous sequel datasets and models
5
star
38

debugproxy

TCP man-in-the-middle proxy
Ruby
5
star
39

nzbget

my git-svn fork of nzbget
C++
5
star
40

haproxy

my copy of haproxy-1.4.git
C
4
star
41

matzruby

A Git mirror of http://svn.ruby-lang.org/repos/ruby.
4
star
42

libmemcached.rb

simple, clean ruby wrapper for libmemcached 1.0
C
3
star
43

tmm1.github.com

tmm1.net
CSS
2
star
44

node-fflush

fflush(3)
C++
2
star
45

TomatoTent

A discreet Home Grow Box
C++
1
star
46

renderapp-mastodon

mastodon on render.com
Dockerfile
1
star
47

haproxy-dev

my copy of haproxy.git (1.5-dev)
C
1
star
48

androidhdmi-for-channels

androidhdmi-for-channels
Go
1
star