gpt4 book ai didi

mysql - Rails 测试数据库比开发数据库快 10 倍

转载 作者:行者123 更新时间:2023-11-29 08:09:27 27 4
gpt4 key购买 nike

我有一些种子数据,可以在我的开发设置中创建 1000 个用户。有时,我会在开发过程中使用以下命令重置数据库:

rake db:reset

它以大约 10 个用户/秒的速度插入我的用户。我认为这很慢,但学会了忍受它。

我最近在测试环境中运行db:reset,同时使用此命令调试一些 rspec 测试:

rake db:reset RAILS_ENV=test

它以大约100 个用户/秒的速度插入用户!我可以重现它并在环境之间切换,开发环境很慢,而测试环境很快。

它在database.yml中使用完全相同的mysql设置:

开发

development:
adapter: mysql2
encoding: utf8
database: mydb
username: mydb
password: password
host: 127.0.0.1
port: 3306

测试

development:
adapter: mysql2
encoding: utf8
database: mydb_test
username: mydb_test
password: password
host: 127.0.0.1
port: 3306

这就是我为用户播种的方式(两种环境都相同):

ActiveRecord::Base.transaction do
1000.times do |i|
User.create :first_name => Faker::Name.first_name, :last_name => Faker::Name.last_name, :email => Faker::Internet.email, :username => Faker::Internet.user_name, :password => '123456'
end
end

有谁知道 Rails 正在做什么让测试环境变得如此快?我想在开发环境中实现这些设置,并将我的播种过程加快 10 倍。

最佳答案

如果您使用的是 Devise,则可能是由密码拉伸(stretch)引起的。在 config/initializers/devise.rb 中:

# Limiting the stretches to just one in testing will increase the performance of
# your test suite dramatically. However, it is STRONGLY RECOMMENDED to not use
# a value less than 10 in other environments.
config.stretches = Rails.env.test? ? 1 : 10

这似乎是您正在寻找的开发环境和测试环境之间的差异。尝试使用这条线

关于mysql - Rails 测试数据库比开发数据库快 10 倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21918907/

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