现在,我的大部分测试都是针对模型运行的。目前我有大约 100 个测试。运行它们时,我看到:
在 3.99 秒内完成(文件加载时间为 17.97 秒)
在 3.93 秒内完成(文件加载时间为 17.51 秒)
在 2.03 秒内完成(文件加载时间为 2.36 秒)<<< 现在看起来好多了
在 4.27 秒内完成(文件加载时间为 17.45 秒)
在 1.98 秒内完成(文件加载时间为 2.4 秒)<<< 现在看起来好多了
总计 20 秒的延迟对我来说似乎很多,而且我是随机看到的。这是我应该接受的东西,还是有更好的方法?随着测试数量的增加,我担心我的开发周期会变得很麻烦。我知道我可以运行单个或一组规范文件。如果可能的话,我想优化整个集合的运行。
不确定要添加哪些项目,但这里有一些:
- RSpec v3.0.4
- Spring 版本 1.1.3
- rails 4.1.5
- Mac OS X 10.9.4
Spring 状态报告:
..$ Spring 状态
Spring 正在运行:
17524 Spring 服务器 |应用名称 | 137 小时前开始
22503 Spring 应用 |应用名称 | 77 小时前开始 |发展模式
最佳答案
首先 - 确保您正在使用 spring 运行您的规范套件:
spring rspec
这将确保环境由 spring 预加载并且 RSpec 使用预加载的环境。
如果您还没有使用 rspec 设置 spring,只需将 spring-commands-rspec
添加到您的 Gemfile 中:
gem "spring-commands-rspec", group: :development
然后停止 spring 以便它获取新的配置:
spring stop
如果您正在这样做,请不要相信 RSpec 报告的加载时间。您看到的加载时间数字可能非常不准确。用 spring 运行 RSpec 时,加载时间似乎指的是 spring 第一次启动的时间:
> time spring rspec
Finished in 9.12 seconds (files took 1 minute 11.36 seconds to load)
345 examples, 0 failures
spring rspec 0.45s user 0.06s system 4% cpu 10.304 total
如您所见,RSpec 报告加载时间为 1 分 11.36 秒,但实际上整个套件的加载和运行时间为 10.3 秒!
如果我在 2 分钟后再次运行测试:
> time spring rspec
Finished in 9.01 seconds (files took 3 minutes 18.5 seconds to load)
345 examples, 0 failures
spring rspec 0.44s user 0.06s system 4% cpu 10.209 total
更新
不准确报告的加载时间存在一个 Unresolved 问题:
https://github.com/jonleighton/spring-commands-rspec/issues/18
这个问题有一个解决方法,就是在 bin/rspec
的 binstub 中添加 Rspec.configuration.start_time = Time.now
。有了它,我得到一致的低于 1 秒的加载时间:
time spring rspec
Finished in 8.46 seconds (files took 0.53673 seconds to load)
345 examples, 0 failures
spring rspec 0.46s user 0.06s system 5% cpu 9.214 total
关于ruby-on-rails - 运行 RSpec 时我的负载和 "Finished in"次不同。我可以加快他们的速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26023468/