2011-04-15 53 views
2

我有一個很難處理數字的gem:它從圖像中剪出「有趣」的部分。爲此,我設置了幾個算法。總的來說,它表現糟糕。顯然,我想改善:)。獨立Ruby代碼(寶石)的性能和資源測試

我想測試和測量三樣東西:

  • 內存使用
  • CPU使用率
  • 的方法/例行花費的總時間。

我想調查這一點,並比較各種算法,參數和設置的值。

是否有一些Ruby功能,一個寶石或類似的東西,這將允許我運行我的代碼,更改一些參數或一些代碼,再次運行它,然後比較結果?

我有測試:單位和應該已經到位,所以如果有東西使用這些測試框架,那很好。

回答

3

您可以使用標準的'profiler'庫。它報告你在每種方法中花費的時間。

require 'profiler' 

def functionToBeProfiled 
    a = 0 
    1000.times do |i| 
    a = a + i * rand 
    end 
end 

Profiler__::start_profile 
functionToBeProfiled 
Profiler__::stop_profile 
Profiler__::print_profile($stdout) 

這將產生以下輸出:

% cumulative self    self  total 
time seconds seconds calls ms/call ms/call name 
16.58  0.03  0.03  1 31.00 93.00 Integer#times 
16.58  0.06  0.03  1000  0.03  0.03 Kernel.rand 
    8.56  0.08  0.02  3  5.33 10.33 RubyLex#identify_identifier 
    8.56  0.09  0.02  10  1.60  6.20 IRB::SLex::Node#match_io 
    8.56  0.11  0.02  84  0.19  0.19 Kernel.=== 
    8.56  0.13  0.02  999  0.02  0.02 Float#+ 
    8.56  0.14  0.02  6  2.67  5.33 String#gsub! 

不過要小心,因爲這個庫會阻礙你的應用程序的性能。您從中獲得的測量結果只有與使用相同方法獲得的其他測量結果相比才有用。它們不能用於評估絕對測量值。

+0

在這裏找到了一個不錯的howto:http://ruby.about.com/od/advancedruby/a/profile.htm – berkes 2011-04-18 15:02:34

0

我做了很好的經驗與紅寶石教授:

http://ruby-prof.rubyforge.org/

還有在網絡上某處分析的各種方法好介紹的,但我不記得書名和作者,並有現在很難找到它... :-(

+0

我用它和它一起玩,但它有很多小故障和錯誤,而真正有趣的部分(對我而言)需要修補和重新編譯的ruby,對於快速測試來說,相當多的工作:) – berkes 2011-04-18 15:00:08