淺析Ruby中的Profiling工具的用法_ruby專題

來源:互聯網
上載者:User

內建的profiler實現的很簡單,在ruby2.2中只有150行代碼,大家可以看看它的實現profile.rb 。內建的profiler使用起來非常的方便,只需要加上-rprofile參數即可。例如:

執行:

ruby -rprofile test.rb

輸出結果為:

通過列印出的結果能夠很明顯的看出耗時的方法。內建的profiler很簡單,只能列印出這樣的結果,沒有 其他輸出格式的選項,下面介紹的其他幾種都有豐富的格式輸出。
ruby-prof

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

ruby-prof具有C擴充,所以它能啟動並執行更快,同時它支援豐富的輸出格式,方便我們去尋找效能瓶頸。 ruby-prof支援輸出GraphViz支援的dot格式,兩者的安裝方法如下:

gem install ruby-profubuntu | sudo apt-get install graphvizMac  | brew install graphviz

執行命令很簡單,如下:

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查看程式內的方法調用佔比了。


perftools.rb

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

perftools.rb是google-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 }enddef m2 1_000_000.times{ 1+2+3+4+5 }endStackProf.run(mode: :cpu, out: 'tmp/stackprof.dump') do m1 m2end

我們執行這個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,在頁面上的左上方會看到回應時間的毫秒數。如下圖所示:

點擊query time(ms)列中的1 sql則可以查看到執行的sql語句及耗時:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.