我正在尝试将性能测试合并到非 Rails 应用程序的测试套件中,但遇到了一些问题。
- 我不需要每次都运行性能测试,如何排除它们?注释和取消注释
config.filter_run_excluding :perf => true
似乎不是个好主意。 - 如何报告基准测试结果?我认为 RSpec 对此有一些机制。
最佳答案
我创建了 rspec-benchmark用于在 RSpec 中编写性能测试的 Ruby gem。它对测试速度、资源使用和可扩展性有很多期望。
例如,测试你的代码有多快:
expect { ... }.to perform_under(60).ms
或者与另一个实现进行比较:
expect { ... }.to perform_faster_than { ... }.at_least(5).times
或者测试计算复杂度:
expect { ... }.to perform_logarithmic.in_range(8, 100_000)
或者查看分配了多少对象:
expect {
_a = [Object.new]
_b = {Object.new => 'foo'}
}.to perform_allocation({Array => 1, Object => 2}).objects
要过滤您的测试,您可以将规范分离到一个performance
目录中并添加一个 rake 任务
require 'rspec/core/rake_task'
desc 'Run performance specs'
RSpec::Core::RakeTask.new(:perf) do |task|
task.pattern = 'spec/performance{,/*/**}/*_spec.rb'
end
然后在需要时运行它们:
rake perf
关于ruby - 使用 RSpec 进行性能测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11178530/