gpt4 book ai didi

ruby-on-rails - 为什么在将 rails 从 3.1.0 升级到 3.2.0 后我的测试速度很慢?

转载 作者:行者123 更新时间:2023-12-04 02:23:37 25 4
gpt4 key购买 nike

我遵循了以下升级说明:http://railscasts.com/episodes/318-upgrading-to-rails-3-2

这是我的三个小的升级更改:

(1) Gemfile

-gem 'rails', '3.1.0'
+gem 'rails', '3.2.0'

-gem 'rack', '1.3.3'
+#gem 'rack', '1.3.3'

group :assets do
- gem 'sass-rails', ' ~> 3.1.0'
- gem 'coffee-rails', '~> 3.1.0'
- gem 'uglifier'
+ gem 'sass-rails', ' ~> 3.2.3'
+ gem 'coffee-rails', '~> 3.2.1'
+ gem 'uglifier', ' >=1.0.3'
gem 'asset_sync'
end

(2) config/environments/development.rb
+  config.active_record.mass_assignment_sanitizer = :strict
+ config.active_record.auto_explain_threshold_in_seconds = 0.5

(3) config/environments/test.rb
-  config.assets.allow_debugging = true
+ config.active_record.mass_assignment_sanitizer = :strict

在升级之前,我的测试如下(每个测试不到一秒):
...
StockroomTest:
PASS stockroom must have a name (0.03s)
PASS stockroom name must be unique (0.01s)
PASS stockroom with name is valid (0.00s)
...
Finished in 1.604118 seconds.
29 tests, 90 assertions, 0 failures, 0 errors, 0 skips
...
StockroomsControllerTest:
PASS should create stockroom (0.04s)
PASS should destroy stockroom (0.02s)
PASS should get edit (0.14s)
PASS should get index (0.11s)
PASS should get new (0.03s)
PASS should not destroy stockroom (0.04s)
PASS should show stockroom (0.13s)
PASS should update stockroom (0.02s)
...
Finished in 12.572911 seconds.
115 tests, 166 assertions, 0 failures, 0 errors, 0 skips
...
MiscellaneousTest:
PASS get campaigns#index should redirect to newsletters#index (1.83s)
PASS get /campaigns should redirect to / when logged out (0.06s)
Finished in 1.793070 seconds.
2 tests, 3 assertions, 0 failures, 0 errors, 0 skips

之后(每个测试需要超过 1 秒):
StockroomTest:
PASS stockroom must have a name (1.29s)
PASS stockroom name must be unique (1.30s)
PASS stockroom with name is valid (1.27s)
...
Finished in 41.135808 seconds.
29 tests, 90 assertions, 0 failures, 0 errors, 0 skips
...
StockroomsControllerTest:
PASS should create stockroom (1.30s)
PASS should destroy stockroom (1.29s)
PASS should get edit (1.33s)
PASS should get index (1.43s)
PASS should get new (1.41s)
PASS should not destroy stockroom (1.31s)
PASS should show stockroom (1.36s)
PASS should update stockroom (1.31s)
...
Finished in 161.803235 seconds.
115 tests, 166 assertions, 0 failures, 0 errors, 0 skips
...
MiscellaneousTest:
PASS get /campaigns should redirect to /newsletters when logged in (5.27s)
PASS get /campaigns should redirect to / when logged out (1.67s)
Finished in 7.034593 seconds.
2 tests, 3 assertions, 0 failures, 0 errors, 0 skips

以下是上述单元测试之一的示例。现在(升级后)运行大约需要 1.3s,而之前不到 0.01s。

测试/单元/stockroom_test.rb
require 'test_helper'

class StockroomTest < ActiveSupport::TestCase
fixtures :stockrooms

test "stockroom with name is valid" do
assert stockrooms(:wine_cellar).valid?, 'tried new wine_cellar'
end

我知道固定装置不受欢迎,我确实打算认真调查工厂,但目前这是我的困境。这是相关的夹具:

测试/夹具/stockrooms.yml
wine_cellar:
id: 1
name: wine cellar
Stockroom 上仅有的两个验证是 presenceuniqueness .

注意:我正在同一台机器上运行另一个 rails 应用程序,尽管它正在运行 rails 3.2.5 ,并且几乎相同的单元测试(对相同的两个验证的相同断言)在 0.465489 秒(不到半秒)内完成。

对于上述“名称为有效的仓库”测试,测试日志的相关部分如下所示:
 (0.9ms)  SET FOREIGN_KEY_CHECKS = 1
(0.2ms) BEGIN
(84.8ms) BEGIN
(82.3ms) BEGIN
(83.4ms) BEGIN
(79.2ms) BEGIN
(82.1ms) BEGIN
Stockroom Load (0.4ms) SELECT `stockrooms`.* FROM `stockrooms` WHERE `stockrooms`.`id` = 1 LIMIT 1
Stockroom Exists (0.6ms) SELECT 1 AS one FROM `stockrooms` WHERE (`stockrooms`.`name` = BINARY 'wine cellar' AND `stockrooms`.`id` != 1) LIMIT 1
(0.1ms) ROLLBACK
(90.9ms) ROLLBACK
(85.7ms) ROLLBACK
(90.7ms) ROLLBACK
(81.4ms) ROLLBACK
(85.4ms) ROLLBACK

为了比较,这里是我的 rails 中的“等效”测试 3.2.5应用程序:
 (0.2ms)  SET FOREIGN_KEY_CHECKS = 1
(0.1ms) BEGIN
Email Load (0.4ms) SELECT `emails`.* FROM `emails` WHERE `emails`.`id` = 980190962 LIMIT 1
Email Exists (2.8ms) SELECT 1 FROM `emails` WHERE (`emails`.`email` = BINARY 'MyString' AND `emails`.`id` != 980190962) LIMIT 1
(0.2ms) ROLLBACK

最佳答案

我能想象到的唯一答案是您的测试在处理交易的方式上有所不同。

如果一切正常,rails 应该将每个测试包装在一个事务中并回滚事务。所以你基本上只“模拟”写入 Action ,不必每次测试后回滚数据库,从而节省大量时间。

如果是这样,您可能会在这里找到答案:
ActiveRecord Rollback does not work in Rails test

只有您可能会尝试显式打开该功能。

编辑:当您的输出显示您正在使用事务时,rails 环境的加载方式可能有所不同。请检查 spec_helper.rb 以了解奇怪的差异。

您可以查看这篇文章并检查在您的测试套件启动时是否发生了一些奇怪的事情:
Rails 3 - Speed up Console Loading Time

关于ruby-on-rails - 为什么在将 rails 从 3.1.0 升级到 3.2.0 后我的测试速度很慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805175/

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