独立Ruby代码(gem)的性能和资源测试

|| 我有一个难以处理数字的宝石:它从图像中裁剪出“有趣的”部分。为此,我设置了几种算法。总体而言,它的性能很差;显然,我想改进:)。 我想测试和衡量三件事: 内存使用情况 CPU使用率 在方法/例程中花费的总时间。 我想对此进行调查,并比较各种算法,参数和设置的值。 是否有一些Ruby功能,gem或类似的功能,这些功能将允许我运行代码,更改一些参数或少量代码,再次运行它,然后比较结果? 顺便说一下,我已经有test:unit和shoulda了,所以如果有使用这些测试框架的工具,那就很好了。     
已邀请:
您可以使用标准的\'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!
但是请小心,因为该库会阻碍您的应用程序的性能。从中获得的测量值仅与与使用相同方法获得的其他测量值进行比较时才有用。它们不能用于评估绝对测量值。     
我在使用ruby-prof方面有很好的经验: http://ruby-prof.rubyforge.org/ 在网络上的某处也有关于各种性能分析的很好的演示,但是我不记得标题和作者,现在很难找到它... :-(     
我对JRuby感到惊喜。如果您的代码无需更改就可以在该实现上运行,并且您熟悉Java基准测试软件,则应该看看(并告诉我您的情况)。 http://www.engineyard.com/blog/2010/monitoring-memory-with-jruby-part-1-jhat-and-visualvm/ http://danlucraft.com/blog/2011/03/built-in-profiler-in-jruby-1.6/ 现在,已经更加仔细地阅读了您的问题,我意识到这不具备使流程自动化的能力。     

要回复问题请先登录注册