百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

perftools.rb

> 编程语言
开源

用于 Ruby 代码的 gperftools

1.0K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于 Ruby 代码的 gperftools

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:

bash
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:

bash
$ 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

  • Simple require 'rubygems' profile

  • Comparing redis-rb with and without SystemTimer based socket timeouts

  • Sinatra vs. Merb vs. Rails

  • C-level profile of EventMachine + epoll + Ruby threads before and after a 6 line EM bugfix

  • C-level profile of a ruby/rails vm

    • 12% time spent in re_match_exec because of excessive calls to rb_str_sub_bang by Date.parse

Installation

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

bash
sudo gem install perftools.rb

Or build your own gem:

bash
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:

bash
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:

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

Compile:

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

Profile:

bash
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

  • GoRuCo 2009 Lightning Talk on perftools.rb @ 21:52

  • Ilya Grigorik's introduction to perftools.rb

  • Google Perftools

  • Analyzing profiles and interpreting different output formats

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言