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

ruby-prof-flamegraph

> 编程语言
开源

轻松找到 Ruby 代码中的瓶颈。一个支持 FlameGraph 的 ruby-prof 打印机

288 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

轻松找到 Ruby 代码中的瓶颈。一个支持 FlameGraph 的 ruby-prof 打印机

ruby-prof-flamegraph

Easily find the bottleneck in your Ruby app.

RubyProf::FlameGraphPrinter is a ruby-prof printer that outputs a fold stack file that's compatible with FlameGraph. It is created based on RubyProf::CallStackPrinter.

The result can be passed to FlameGraph to generate an interactive stack trace visualization (click on the image to see the demo).

Demo

Awesomeness

FlameGraph is a way to visualize stack trace, making it very obvious where in the program takes the longest time. It is a Perl script takes a "fold stack" file and generates a nice, interactive SVG. The fold stack is usually generated from DTrace or Prof data using stackcollapse.pl, which is included with FlameGraph.

I created this gem because I want to find out where the bottleneck is in SlimWiki's specs, but I don't know DTrace and just want the result quick.

To learn more about Flame Graphs, check these out:

  • Official Flame Graphs Website by Brendan Gregg
  • Node.js in Flames, which is the article that introduced me to flame graph (via Node Weekly Issue #62)
  • Blazing Performance with Flame Graphs talk at USENIX/LISA13 (slideshare) (video)

Installation

ruby
gem 'ruby-prof-flamegraph'

Usage

Obtaining the Fold Stack Trace

Just require 'ruby-prof-flamegraph' and use RubyProf::FlameGraphPrinter as your printer for ruby-prof. For vanilla ruby-prof, see example.rb.

For rspec-prof, RSpecProf.printer_class = RubyProf::FlameGraphPrinter

Generating the Image

Then, to generate an image, pipe the output to FlameGraph's flamegraph.pl. Here's how the example SVG is generated:

bash
bundle exec ruby example.rb | \
    ~/GitHub/FlameGraph/flamegraph.pl --countname=ms --width=728 > example.svg

Output Format

Taken from [flamegraph.pl]:

The input is stack frames and sample counts formatted as single lines. Each frame in the stack is semicolon separated, with a space and count at the end of the line.

Each line in the output looks like this:

Thread:ID;Fiber:ID;Class#method1 (call_count) time_taken_in_milliseconds

Here's an example:

Thread:123;Fiber:123;Capybara::Server#boot (1) 0.163681
Thread:123;Fiber:123;Capybara::Server#boot (1);#server (1) 0.099445
Thread:123;Fiber:123;Capybara::Server#boot (1);#server (1);Kernel#block_given? (1) 0.044838
Thread:123;Fiber:123;Capybara::Server#boot (1);Proc#call (1) 0.019682
Thread:123;Fiber:123;Capybara::Server#boot (1);Proc#call (1);#run_default_server (1) 0.147611

The call count is included so that the number of calls is shown in the graph.

Post-Processing

Sometimes, you may want to post process the data before passing it to flamegraph.pl, such as...

  • Removing the Thread and Fiber ID

    Capybara spawns new WEBrick thread each time, putting each HTTP server instance in a different stack. We can strip the Thread ID and Fiber ID from the output first:

    ruby
    processed = 0
    
    while data = gets
      data.gsub! %r{^Thread:\d+;Fiber:\d+;}, ''
      puts data
      processed += 1
      if processed % 1000 == 0
        $stderr.puts "Processed #{processed} lines"
      end
    end
    
    $stderr.puts "Finished processing #{processed} lines"
    bash
    ruby remove-thread-id.rb  profile2.txt
  • Removing call counts

    Sometimes, you may want to concatenate multiple profiles. This causes the stack to separate based on the call count. To put calls to same method on the same stack, the call count should be removed.

    ruby
    processed = 0
    
    while data = gets
      data.gsub! %r{\s\(\d+\);}, ';'
      puts data
      processed += 1
      if processed % 1000 == 0
        $stderr.puts "Processed #{processed} lines"
      end
    end
    
    $stderr.puts "Finished processing #{processed} lines"
    bash
    ruby remove-call-count.rb  profile3.txt

I am relying on external scripts to do this processing for maximum flexibility (so that I always have the raw data to adjust it as needed).

Issues· 2 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Ruby

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

> 工具信息

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

> 相关工具

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