简体   繁体   English

耙子测试运行速度很慢

[英]Rake tests running very slow

After running some tests, I'm convinced there has to be something wrong with my setup (windows, rubymine and latest ruby versions). 经过一些测试后,我确信我的设置(窗口,rubymine和最新的ruby版本)都有问题。 My times right now are: 我现在的时代是:

Finished tests in 14.289817s, 0.0700 tests/s, 0.3499 assertions/s.

1 tests, 5 assertions, 0 failures, 0 errors, 0 skips

Process finished with exit code 0

With 5 VERY easy tests (just checking if validation on empty fields works). 有5个非常简单的测试(只检查空字段上的验证是否有效)。 The total time for these 5 unit tests is 160 seconds, over 2 minutes. 这5个单元测试的总时间为160秒,超过2分钟。

What could I do to improve this speed? 我该怎么做才能提高这个速度?

Here are the tests: 以下是测试:

require 'test_helper'

class ItemTest < ActiveSupport::TestCase
  test 'item attributes must not be empty' do
    item = Item.new
    assert item.invalid?
    assert item.errors[:name].any?
    assert item.errors[:description].any?
    assert item.errors[:image_url].any?
    assert item.errors[:rating].any?
  end
end

Your problem is Windows. 你的问题是Windows。 We use JRuby on Windows and it actually runs faster than RubyInstaller(mingw) ruby on Windows but we do see very slow results when running test suites or starting a rails server. 我们在Windows上使用JRuby,它实际上比Windows上的RubyInstaller(mingw)ruby运行得更快但是在运行测试套件或启动rails服务器时我们确实看到非常慢的结果。 About 1 minute for a single test run due to the loading of the Rails environment. 由于加载了Rails环境,单次测试运行大约需要1分钟。 You have a few options: 你有几个选择:

  1. Switch to linux / osx 切换到linux / osx
  2. Use spork to keep a couple rails environments pre-loaded for your tests. 使用spork可以为测试预先加载几个rails环境。 Note that this isn't perfect but it will reduce your times substantially. 请注意,这并不完美,但会大大减少您的时间。 With this option you'll probably want to use minitest or rspec, I had trouble getting spork to work on Windows with testunit. 使用此选项,您可能希望使用minitest或rspec,我无法使用testunit在Windows上使用spork。 With spork you should be able to get your single test run time down to about 10 seconds. 使用spork,您应该能够将单次测试运行时间降低到大约10秒。
  3. Write as many of your tests to run outside of Rails, in other words to not require the Rails stack. 编写尽可能多的测试以在Rails之外运行,换句话说不需要Rails堆栈。 This will be very fast, you should be able to run a test in only a few seconds but as you could guess, it's hard to test a lot of things (controllers, views) outside of rails. 这将是非常快的,您应该能够在几秒钟内运行测试,但正如您猜测的那样,很难在rails之外测试很多东西(控制器,视图)。 Works perfectly though for functions you've broken out into modules that already do not require anything from rails. 虽然对于已经分解成已经不需要任何东西的模块的功能,但工作正常。

Good luck! 祝好运!

What's the rest of your gem stack? 什么是你的宝石堆栈的其余部分? Sometimes third-party gems are initialized by rails and will try to phone home (New Relic, Airbrake) which can inflate your test times (though probably not by this much). 有时第三方宝石由rails初始化并将尝试打电话回家(New Relic,Airbrake),这可能会使你的测试时间膨胀(尽管可能不是这么多)。 If something isn't strictly required for your test suite, you should try to pull it into the proper env group, or set require :false via bundler: 如果您的测试套件没有严格要求的东西,您应该尝试将其拉入正确的env组,或通过bundler设置require :false

group :production do
  gem 'newrelic_rpm'
end

Startup time appears to be killing you, so you and I are probably in the same boat. 启动时间似乎在扼杀你,所以你和我可能在同一条船上。 Between Jodell and Dark Castle a lot of this is covered already, but here's my nearly-whole list of things that helped, in descending order of efficacy. 在Jodell和Dark Castle之间已经有很多这方面的内容了,但是这里有几乎所有有用的东西,按功效降序排列。

  1. Get a patched 1.9.3 with the 2.0 filesystem improvements backported. 获得补丁1.9.3,并对2.0文件系统进行了反向移植。 The first gets 2x better numbers but I'm using the second because I felt the first was unstable 第一个获得2倍更好的数字,但我使用第二个因为我觉得第一个是不稳定的
  2. Set your GC options 设置GC选项
  3. Turn off collecting coverage data (My IDE kept volunteering this) 关闭收集覆盖数据(我的IDE继续志愿服务)
  4. Run Spork, and set SPEC_OPTS=--drb 运行Spork,并set SPEC_OPTS=--drb
  5. Turn off virus scanner (don't actually do this, it's only worth 10% for me anyway) 关闭病毒扫描程序(实际上不要这样做,无论如何它只值10%)
  6. Double-check your Gems, delaying the loading of gems with require: false 仔细检查你的宝石,用require:false延迟加载宝石

6 didn't actually buy me very much. 6实际上并没有给我买单。 The biggest thing we had wrong was loading Thin unconditionally (which pulls in Eventmachine, which 21 megabytes installed), but the next 3 on the stack were actually being used by RSpec. 我们遇到的最大问题是无条件地加载Thin(它安装了Eventmachine,安装了21兆字节),但是堆栈中接下来的3个实际上正被RSpec使用。 I have not looked at network traffic, but Jodell is probably on to something there. 我没有看过网络流量,但Jodell可能正在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM