gpt4 book ai didi

ruby-on-rails - 如果我调用 Factory.build 以使我的 Controller 测试快速,我怎样才能让 Factory Girl 永远不会访问数据库?

转载 作者:数据小太阳 更新时间:2023-10-29 06:30:19 25 4
gpt4 key购买 nike

我正在寻求使我的 Rails 测试更快。我只有 520 个测试,但它们在 bash 中运行需要 62 秒,在 Rubymine 中运行需要 82 秒。

作为典型 Controller 测试的示例,我使用此代码以@user 身份登录并在 CommentsController 中为我的 RSpec Controller 测试创建基本的@comment:

before(:each) do
@user = Factory.create(:user)
sign_in @user

@comment = Factory.create(:comment)
end

您可能会意识到...这很慢。它构建了一个 @user,但也为该用户构建了关联。 @comment 也是如此。

所以我认为调用 Factory.build(:user) 可以解决问题……但我遇到了奇怪的错误。例如,current_user 返回 nil

所以...我决定使用 Factory.build() 并在我的父 Controller 中清除所有之前的过滤器。然而,当我之后检查 RSPec 日志时,我的 rspec 日志仍然说大量插入正在访问数据库(我们正在谈论仅 3 次测试的数百行代码!)

  before(:each) do
@user = Factory.build(:user)
#sign_in @user

controller.stub(:authenticate_user!) #before_filter
controller.stub(:add_secure_model_data) #before_filter
controller.stub(:current_user).and_return(@user)

@comment = Factory.build(:comment)
end

可悲的是,上面的 before(:each) block 对测试性能的影响为零。正如我发现的那样,调用 Factory.build() 仍将在内部调用子关联上的 Factory.create()

这是一个 before(:each) block ,它有效地删除了 RSpec 日志中产生的垃圾。它使我的测试性能提高了 35-40%

  before(:each) do
@user = Factory.build(:user, :role => Factory.build(:role))
#sign_in @user

controller.stub(:authenticate_user!)
controller.stub(:add_secure_model_data)
controller.stub(:current_user).and_return(@user)

# both of these are still super slow. WTF?!
@site_update = Factory.build(:site_update, :id => 5, :author => Factory.build(:user, :role => Factory.build(:role)))

@comment = Factory.build(:comment,
:author => Factory.build(:user, :role => Factory.build(:role)),
:commentable => @site_update)
end

这使测试运行得更快,但它也很丑陋。我们不能认真地为每个测试都写这个……是吗?太疯狂了。我不会这样做。

我还想指出,这些 Factory.build() 行中的任何一行仍然需要大约 0.15 秒,即使它们没有访问数据库!

仅运行 3 次测试仍然会导致 factory_girl PER 测试占用大约 0.3 到 0.35 秒的时间!我认为这是完全不能接受的。如果删除 Factory.build() 行,测试将在 0.00001 秒内运行。

我认为陪审团的意见是:factory_girl 是一个非常慢的库。不使用它是唯一的解决方案吗?

这是我的factories.rb:

Factory.define :role do |f|
f.name "Admin"
end

Factory.define :user do |f|
f.first_name "Banoo"
f.last_name "Smith"
f.sequence(:email) { |n| "Banoo.Smith#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
end

Factory.define :admin do |f|
f.first_name "Banoo"
f.last_name "Smith"
f.sequence(:email) { |n| "admin#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
end

Factory.define :course_provider do |f|
f.first_name "Josh"
f.last_name "Bolson"
f.sequence(:email) { |n| "josh.bolson#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
end

Factory.define :director do |f|
f.first_name "Director"
f.last_name "Dude"
f.sequence(:email) { |n| "director#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
end

Factory.define :instructor do |f|
f.first_name "Instructor"
f.last_name "Dude"
f.sequence(:email) { |n| "instructor#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
end

Factory.define :trainee do |f|
f.first_name "Trainee"
f.last_name "Dude"
f.sequence(:email) { |n| "trainee#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
end

Factory.define :private_message do |f|
f.subject "Subject"
f.content "content"
f.is_deleted_by_sender false
f.association :sender, :factory => :user
end

Factory.define :recipient do |f|
f.is_read false
f.is_deleted false
f.association :receiver, :factory => :user
f.association :private_message
end

Factory.define :course_template do |f|
f.name "name"
f.description "description"
f.association :course_provider
end

Factory.define :site_update do |f|
f.subject "Subject"
f.intro "intro"
f.content "content"
f.association :author, :factory => :user
end

Factory.define :comment do |f|
f.content "content"
f.association :author, :factory => :user
f.association :commentable, :factory => :site_update
end

Factory.define :country do |f|
f.name "Liberty"
end

Factory.define :province do |f|
f.name "Freedom"
f.association :country
end

Factory.define :payment_plan do |f|
f.name "name"
f.monthly_amount 79
f.audience "Enterprises"
f.active_courses "500-2000"
end

Factory.define :company do |f|
f.name "name"
f.phone_number "455-323-2132"
f.address "address"
f.postal_code "N7G-5F4"
f.association :province
f.association :payment_plan
end

Factory.define :company_user do |f|
f.first_name "Dan"
f.last_name "Grayson"
f.sequence(:email) { |n| "dan.grayson#{n}@gmail.com" }
f.password "secretpassword"
f.association :role
f.association :company
end

Factory.define :course do |f|
f.notes "notes"
f.difficulty 100
f.association :course_template
f.association :instructor, :factory => :company_user
end

Factory.define :study_group do |f|
f.name "name"
end

Factory.define :help_category do |f|
f.name "name"
end

Factory.define :help_document do |f|
f.question "question"
f.content "content"
f.association :category, :factory => :help_category
end

Factory.define :tag do |f|
f.name "name"
end

Factory.define :partial_mapping do |f|
f.from_suffix "ing"
f.to_suffix "ing"
end

Factory.define :newsletter do |f|
f.subject "subject"
f.content "content"
end

Factory.define :press_contact do |f|
f.full_name "Banoo Smith"
f.email 'Banoo.Smith@gmail.com'
f.phone_number "455-323-2132"
f.address "address"
f.postal_code "N9B-3W5"
f.association :province
end

Factory.define :press_release do |f|
f.headline "Headline"
f.origin "origin"
f.intro "intro"
f.body "body"
f.association :contact, :factory => :press_contact
end

Factory.define :theme do |f|

end

和有趣的基准。调用 Factory.create(:user) 平均需要 0.1 到 0.14 秒:

$ rails runner 'Benchmark.bm {|x| x.report { 100.times { Factory.create(:user) } } }' 
user system total real
9.940000 0.080000 10.020000 ( 14.872736)

即使是 Factory.build(:user) 也需要很长时间...而这是在打开 :default_strategy => :build 的情况下!

$ rails runner 'Benchmark.bm {|x| x.report { 100.times { Factory.build(:user) } } }'
user system total real
9.350000 0.030000 9.380000 ( 11.798339)

很明显,这证明 factory_girl 有问题。解决方案是摆脱它或确保它正在使用 Factory.build。这就是答案。

既然基本解决了我自己的问题,我想知道为什么Factory_girl这么受欢迎,为什么是“常识”?可以客观地得出结论,无论使用 Factory Girl 可以获得什么好处——它有很多好处——都不值得付出性能代价。我确信可以开发出性能更高的更好的 factory gem……但遗憾的是,factory_girl 不是。

我下面的解决方案使用了基本的对象实例化和 stub ,并且测试继续通过。我认为在每次测试的基础上使用基本的 Ruby、 stub 和手动填充对象值是“正确”的做法,如果你想避免固定装置并在运行测试时获得高性能的话。

最佳答案

好吧,我想我会回答我自己的问题。我认为这是正确的答案,也许其他人可以从中学习,因为我不得不花几个小时来学习它。

以下是我获得 2000%(或 20 倍)速度提升的方法:

before(:each) do
@user = User.new
controller.stub(:authenticate_user!)
controller.stub(:current_user).and_return(@user)
controller.stub(:add_secure_model_data)

@site_update = SiteUpdate.new
@comment = Comment.new
end

解决方案就是不使用任何类型的工厂来进行 Controller 测试(或许还有其他类型的测试)。我建议仅在实在太痛苦而无法使用 Factory 时才使用 Factory。

所有 3 个测试现在都在 0.07 秒内运行!在 1.4 秒之前运行所有 3 个测试。

Factory_girl 只是一个非常慢的库。我不知道它到底在做什么,但它的配置文件不正确。

是的,我知道它做的不仅仅是简单的 MyClass.new 语句...但即使对于像 Ruby 这样较慢的脚本语言,性能也比基本类实例化慢很多数量级.它需要进行一些大规模优化,以便 Factory.build(:my_class)MyClass.new

更加一致

我建议 Factory_girl 的实现者尝试获取它,这样它的开销不会比基本的 MyClass.new 调用慢很多(不包括数据库开销......那不可能避免)。它应该提供一种构建对象的好方法,您不必为了获得此好处而付出 20 倍的性能损失。这不是一个可以接受的权衡。

这一切都太糟糕了,因为当您在 Controller 规范中打开 render_views 时,Factory.build 在 Controller 中会很好。应该有很大的动机来纠正这个问题。

与此同时,只需使用基本的 Ruby/Rails 类。我想您会惊讶于它们实际上有多快....

关于ruby-on-rails - 如果我调用 Factory.build 以使我的 Controller 测试快速,我怎样才能让 Factory Girl 永远不会访问数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6128476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com