为什么 Test::Unit 测试用例启动如此缓慢?
>rails -v
Rails 1.2.6
>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
当我像这样运行测试装置(测试 Rails 模型类)时,需要 20-30 秒才能开始执行这些测试(显示“已加载套件...”)。 是什么赋予了?
>ruby test\unit\category_test.rb
require File.dirname(__FILE__) + '/../test_helper'
class CategoryTest < Test::Unit::TestCase
def setup
Category.delete_all
end
def test_create
obCategoryEntry = Category.new({:name=>'Apparel'})
assert obCategoryEntry.save, obCategoryEntry.errors.full_messages.join(', ')
assert_equal 1, Category.count
assert_not_nil Category.find(:all, :conditions=>"name='Apparel'")
end
#.. 1 more test here
end
这是使用没有固定装置的 MySql DB 的 Rails。 这次启动时间超过 30 秒。
>rails -v
Rails 1.2.6
>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]
When I run a test fixture (that tests a rails model class) like this, it takes 20-30 secs to start executing these tests (show the "Loaded suite..."). What gives?
>ruby test\unit\category_test.rb
require File.dirname(__FILE__) + '/../test_helper'
class CategoryTest < Test::Unit::TestCase
def setup
Category.delete_all
end
def test_create
obCategoryEntry = Category.new({:name=>'Apparel'})
assert obCategoryEntry.save, obCategoryEntry.errors.full_messages.join(', ')
assert_equal 1, Category.count
assert_not_nil Category.find(:all, :conditions=>"name='Apparel'")
end
#.. 1 more test here
end
This one is Rails using a MySql DB with no fixtures. This time it clocked 30secs+ to startup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看看这个 Rails 测试服务器。
引用作者的话:
(这是上周 Rails Envy 播客 上的专题报道,我就是在那里找到的。)
Take a look at this Rails Test Server.
A quote from the author:
(This was featured on the Rails Envy Podcast this past week which is where I found this.)
当开始任何测试时,Rails 首先加载您拥有的任何装置(在测试/装置中)并用它们重新创建数据库。
不过 20-30 秒听起来非常很慢。 在测试运行之前您是否有很多需要加载的装置,或者您的数据库运行缓慢?
When starting any tests, Rails first loads any fixtures you have (in test/fixtures) and recreates the database with them.
20-30 seconds sounds very slow though. Do you have a lot of fixtures that need to be loaded before your tests run, or is your database running slow?
Ruby 的 gem 工具遵循路径发现算法,该算法显然对 Windows 不友好(正如我从您的 ruby -v 中看到的那样)。
例如,如果您跟踪使用 加载的 Rails 应用程序,您可以获得清晰的图像。过程周一。 每个(我的意思是每个)
require
都会开始扫描 Ruby 路径中的所有目录以及所有 gem 目录。 在普通机器上,典型的require
需要 20 毫秒。 由于 Rails 会产生数百个require
,因此每次启动 Rails 环境时,这 20 毫秒很容易就会达到几秒。 花点时间初始化数据库中的装置,您就会更好地了解为什么需要这么多时间才开始运行测试用例。也许由于每种文件系统架构和实现(路径缓存等)的原因,这在 Linux 中比在 Windows 中问题要小。 但我不知道你应该责怪谁。 看起来 NTFS 文件系统可以通过更好的路径缓存实现来改进,但是很明显,gem 工具可以实现缓存本身,并且其性能不太依赖于平台。
Ruby's gem tool follows a path discovery algorithm which, apparently, is not Windows (as I see from your
ruby -v
) friendly.You can get a clear picture if you trace, for example, a Rails application loading with ProcMon. Every (I really mean every)
require
starts a scan over all directories in Ruby's path plus all gem directories. A typicalrequire
takes 20 ms on an average machine. Since Rails makes hundreds ofrequire
s, those 20 ms easily sum up to seconds every time you launch the Rails environment. Take in the time to initialize the fixtures in the database and you get a better idea of why it takes so much time to just begin running the test-cases.Perhaps because of each file-system architecture and implementation (path caching etc.), this is less of a problem in Linux than in Windows. I don't know who you should blame, though. It looks like the NTFS file-system could be improved with a better path caching implementation, but clearly the gem tool could implement the caching itself and have its performance not so dependent on the platform.
看起来 Test::Unit 是使用 Ruby 进行单元测试的最简单但也是最慢的方法之一。 替代方案之一是 ZenTest。
It seems like Test::Unit is the simplest, but also one of the slowest ways to do unit testing with Ruby. One of alternatives is ZenTest.
测试装置的启动速度并不是特别慢,远不到 20 秒。
这可能是您在测试中正在做的事情。 你在做一些复杂的事情吗? 设置数据库? 从互联网上检索一些东西?
Test unit startup isn't particularly slow, and nowhere near 20 seconds.
It's probably something you're doing in your tests. Are you doing anything complicated? Setting up a database? Retrieving something from the internet?
完全是在黑暗中拍摄的,但大多数时候我看到启动时间很长,这通常是由于沿途某个 TCP 套接字通信发生了某种反向 DNS 查找。
添加:。
尝试在测试文件顶部的其他
require
行之后Complete shot in the dark, but the majority of the time I see long startup times on things, it is usually due to some sort of reverse DNS lookup happening with some TCP socket communication somewhere along the way.
Try adding:
at the top of your test file after your other
require
line.你的 test_helper.rb 是什么样的? 您正在使用实例化的装置吗?
[编辑]
如果将此设置为 true,请尝试将其设置为 false。
What does your test_helper.rb look like? Are you using instantiated fixtures?
[edit]
If this is set to true try setting it to false.