Ruby 中的 Profiling 工具

看看如何調試Ruby的性能問題

Ruby 內置的 profiler

內置的 profiler 實現的很簡單,在 ruby2.2 中只有 150 行代碼,你們能夠看看它的實現profile.rb 。內置的 profiler 使用起來很是的方便,只須要加上-rprofile參數便可。例如:前端

執行:git

ruby -rprofile test.rb
輸出結果爲:github

%   cumulative   self              self     total
time   seconds   seconds    calls  ms/call  ms/call  name
24.24     0.16      0.16    10001     0.02     0.06  Object#m2
18.18     0.28      0.12        2    60.00   330.00  Integer#times
18.18     0.40      0.12    10001     0.01     0.06  Object#m1
15.15     0.50      0.10    10001     0.01     0.01  Class#new
10.61     0.57      0.07    10000     0.01     0.01  P1.new
 6.06     0.61      0.04    20000     0.00     0.00  Fixnum#to_s
 4.55     0.64      0.03    10000     0.00     0.00  Struct#initialize
 3.03     0.66      0.02    10000     0.00     0.00  P2#initialize
 0.00     0.66      0.00        1     0.00     0.00  TracePoint#enable
 0.00     0.66      0.00        1     0.00     0.00  Class#initialize
 0.00     0.66      0.00        1     0.00     0.00  nil#
 0.00     0.66      0.00        1     0.00     0.00  Struct.new
 0.00     0.66      0.00        7     0.00     0.00  Module#method_added
 0.00     0.66      0.00        3     0.00     0.00  BasicObject#singleton_method_added
 0.00     0.66      0.00        2     0.00     0.00  Class#inherited
 0.00     0.66      0.00        2     0.00     0.00  IO#set_encoding
 0.00     0.66      0.00        1     0.00     0.00  TracePoint#disable
 0.00     0.66      0.00        1     0.00   660.00  #toplevel

經過打印出的結果可以很明顯的看出耗時的方法。內置的profiler很簡單,只能打印出這樣的結果,沒有 其餘輸出格式的選項,下面介紹的其餘幾種都有豐富的格式輸出。web

ruby-prof

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

ruby-prof 具備 C 擴展,因此它能運行的更快,同時它支持豐富的輸出格式,方便咱們去查找性能瓶頸。 ruby-prof 支持輸出GraphViz 支持的 dot 格式,二者的安裝方法以下:ubuntu

gem install ruby-profruby

ubuntu | sudo apt-get install graphviz
Mac | brew install graphviz服務器

執行命令很簡單,以下:markdown

ruby-prof --mode=wall --printer=dot --file=output.dot test.rb 25
此命令的詳細使用方法請參考幫助信息 ruby-prof --help。工具

上面命令的執行結果會輸出一個graphviz的dot文件,graphviz提供一個格式轉換命令,能夠將此文件轉換爲一個pdf文件以方便查看,以下:

dot -T pdf -o output.pdf output.dot

這樣就能夠打開output.pdf查看程序內的方法調用佔比了。
cmd-markdown-logo

perftools.rb

repo: https://github.com/tmm1/perftools.rb

perftools.rbgoogle-perftools的 ruby 版本,不過它只支持ruby2.1如下版本,2.1及以上 版本就須要用到下面的stackprof了,這兩個工具都是一我的寫的。鑑於此,咱們略過 perftools.rb, 做者實現stackprof,就是爲了替代perftools.rb。若是有需求的話,就請參考其 github 主頁。

stackprof

repo: https://github.com/tmm1/stackprof

stackprof只支持 Ruby2.1+,不過如今 ruby 的版本發佈很快,每個版本都能帶來一些新東西,2.1 應該很快就能成爲很基礎的版本,咱們就在這個版本上來作一些測試。

安裝:

gem install stackprof

此次咱們直接在代碼中使用 stackprof 的方法:

require 'stackprof'

def m1
  5_000_000.times{ 1+2+3+4+5 }
end

def m2
  1_000_000.times{ 1+2+3+4+5 }
end

StackProf.run(mode: :cpu, out: 'tmp/stackprof.dump') do
  m1
  m2
end

咱們執行這個 ruby 程序,ruby test.rb,會在當前目錄的 tmp 目錄中產生一個文件stackprof.dump, 而後來分析如下這個文件,stackprof命令自己能夠解析這個文件,執行下面的命令:

stackprof tmp/stackprof.dump --text

則會產生下面的結果,結果應該是很清晰的,很明顯在代碼中 m1 方法要佔有絕大部分的運行時間。

Mode: cpu(1000)
   Samples: 75 (0.00% miss rate)
   GC: 0 (0.00%)
=====================================================================
     TOTAL    (pct)     SAMPLES    (pct)     FRAME
        62  (82.7%)          62  (82.7%)     block in Object#m1
        13  (17.3%)          13  (17.3%)     block in Object#m2
        75 (100.0%)           0   (0.0%)     <main>
        75 (100.0%)           0   (0.0%)     block in <main>
        75 (100.0%)           0   (0.0%)     <main>
        62  (82.7%)           0   (0.0%)     Object#m1
        13  (17.3%)           0   (0.0%)     Object#m2

其餘更加豐富的輸出方式和分析方式,就請參考 stackprof 的 github 主頁,講解的很全面。

若是你但願在web前端中展現相關信息,就請看看stackprof-webnav這個gem,它提供了比較全面的 展現,操做等等,適合在一些web應用中使用,github 地址:stackprof-webnav

rack-mini-profiler

repo: https://github.com/MiniProfiler/rack-mini-profiler

rack-mini-profiler專門應用於基於rack的web應用的性能調優,在rails中的使用方法以下:

首先將 gem 添加到 gemfile 中:

gem 'rack-mini-profiler'

執行:

bundle install

而後重啓你的服務器,訪問任意的 URl,在頁面上的左上角會看到響應時間的毫秒數。以下圖所示:
rack-mini-profiler

點擊query time(ms)列中的1 sql則能夠查看到執行的sql語句及耗時:
rack-mini-profiler-sql


本文做者系OneAPM 工程師李哲 ,想閱讀更多好的技術文章,請訪問OneAPM官方技術博客

相關文章
相關標籤/搜索