加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在侧面设置RSpec进行性能测试

发布时间:2020-12-16 19:25:30 所属栏目:百科 来源:网络整理
导读:我们在rails项目中使用RSpec进行单元测试.我想在RSpec中设置一些性能测试,但这样做是为了不破坏“常规”功能和固定装置. 理想情况下,我能够以某种方式标记我的性能规格,以便它们不会默认运行. 然后,当我明确指定运行这些规范时,它将加载一组不同的灯具(使用
我们在rails项目中使用RSpec进行单元测试.我想在RSpec中设置一些性能测试,但这样做是为了不破坏“常规”功能和固定装置.

理想情况下,我能够以某种方式标记我的性能规格,以便它们不会默认运行.
然后,当我明确指定运行这些规范时,它将加载一组不同的灯具(使用更大,更像’类似生产’的数据集进行性能测试是有意义的.

这可能吗?看起来应该是这样.

有没有人设置这样的东西?你是怎么做到的?

解决方法

我设法通过以下方式获得了我想要的东西:
# Exclude :performance tagged specs by default
config.filter_run_excluding :performance => true

# When we're running a performance test load the test fixures:
config.before(:all,:performance => true) do
  # load performance fixtures
  require 'active_record/fixtures'
  ActiveRecord::Fixtures.reset_cache
  ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures',File.basename("products.yml",'.*'))
  ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures',File.basename("ingredients.yml",'.*'))
end

# define an rspec helper for takes_less_than
require 'benchmark'
RSpec::Matchers.define :take_less_than do |n|
  chain :seconds do; end
  match do |block|
    @elapsed = Benchmark.realtime do
      block.call
    end
    @elapsed <= n
  end
end

# example of a performance test
describe Api::ProductsController,"API Products controller",:performance do
  it "should fetch all the products reasonably quickly" do
    expect do
      get :index,:format => :json
    end.to take_less_than(60).seconds
  end
end

但我倾向于同意Marnen的观点,即这不是性能测试的最佳选择.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读