问题描述
我们在项目中使用Mongodb和RSpec.在我的OS X机器上,单个控制器的RSpec运行在:
We use Mongodb and RSpec for our project. On my OS X machine RSpec for a single controller runs in:
Finished in 0.24996 seconds (files took 25.39 seconds to load)
另一方面,我的朋友有Ubuntu,对他来说,相同的规格可以在其中运行:
on the other hand my friend has Ubuntu and for him the same spec runs in:
Finished in 0.27996 seconds (files took 4.05 seconds to load)
在我的计算机上,加载时间延长了6倍以上.可能是什么原因?
Loading time is more than 6 times longer on my machine. What could be the reason?
P.S.不使用OS X不是解决方案:/
P.S. Not using OS X is not a solution :/
有关我们的设置的更多信息:
More information about our setup:
我们都使用ruby 2.2.我们使用cmd: bundle exec rspec
We both use ruby 2.2. We run specs via guardfile with cmd: bundle exec rspec
我的朋友使用rbenv并且which rspec
返回
My friend uses rbenv and which rspec
returns
"/home/dan/.rbenv/shims/rspec`
"/home/dan/.rbenv/shims/rspec`
我使用rvm,而which rspec
返回:
I use rvm and which rspec
returns:
$ which rspec
rspec: aliased to bundled_rspec
$ which bundled_rspec
bundled_rspec () {
_run-with-bundler rspec $@
更新2:
我刚刚克隆了 https://github.com/eliotsykes/rspec-rails-examples并运行rspec.加载文件花费了36多秒钟的时间.因此它与mongodb不相关.我只是注意到Rails服务器的加载时间也很长.
Update 2:
I just cloned https://github.com/eliotsykes/rspec-rails-examples and ran rspec. It took over 36s to load files. So it is not mongodb related. I just noticed that rails server is also loading very long.
> time rspec -v
3.3.2
real 0m2.539s
user 0m1.067s
sys 0m0.185s
推荐答案
捆绑器
对我来说似乎有一些捆扎机的装载问题.我建议进行更多测量.您是否在每个项目中使用1个gemset或将所有内容都存储在1个gemset中(如果您不使用任何东西,那是真的)?如果您在1个目录中有很多gem(即所有的1个gemset),由于它需要遍历更多路径来完成工作,因此最终会大大降低捆绑程序的速度.
Bundler
Looks like some bundler loading issue for me. I'd recommend to make some more measurements. Do you use 1 gemset per project or store everything in 1 gemset (it's true if you don't use any)? If you have lots of gems in 1 directory (i.e. 1 gemset for all) it will eventually slows down bundler a lot, since it needs to traverse more paths to make its job.
bundle | wc -l # how many gems bundler uses in your current project
gem list -q | wc -l # how many gems in your gemset
如果gem list -q | wc -l
报告非常大的价值(我有237
,对我来说一切似乎都很正常),也许您需要将每个项目中已安装的gem拆分成单独的gemset.
If gem list -q | wc -l
reports quite large value (I have 237
and everything seems normal for me), maybe you need to split installed gems into a separate gemset per project.
使用time
命令进行更多测量,查找real
值,即总和.
Make some more measurements with time
command, look for the real
value, it's total sum.
首先,删除您的bundled_rspec
包装,最新的RVM 版本不需要.
First, remove your bundled_rspec
wrapper, it's not needed with latest RVM versions.
然后在有和没有Bundler的情况下测量您的rspec加载:
Then measure your rspec loading with and without Bundler:
time rspec -v # with implicit bundler loading, rubygems-bundler gem is in use
time NOEXEC_DISABLE=1 rspec -v # without bundler, disable rubygems-bundler gem for this call`
如果time rspec -v
即使对于Gemfile较小的项目也能提供大量数字,则这是捆绑程序的问题.
If time rspec -v
gives you big numbers even for project with relatively small Gemfile, it's a bundler issue.
下一个瓶颈通常是Rails本身.尝试测量一个不会加载Rails的测试(即仅spec_helper
),然后使用rails(即使用rails_helper
)进行测试.
Next bottleneck is usually Rails itself. Try measuring one test that doesn't load Rails (i.e. just spec_helper
) and then test with rails (i.e. with rails_helper
).
当您开始看到数字上的巨大差异时,您就会知道问题出在哪里.
As soon you start seeing big difference in numbers, you'll know where you have a problem.
使用 spring
gem 作为提高Rails性能的快速解决方案.如果使用Rails 4.1+,则Spring已启用.
As a quick-fix solution to improve rails performance is a usage of spring
gem. If you use Rails 4.1+ Spring is already enabled.
要为rspec
启用Spring,请添加到您的Gemfile
To enable Spring for rspec
add to your Gemfile
gem 'spring-commands-rspec', group: :development
并运行
$ bundle install
$ spring binstub --all
Last命令将为项目的bin
文件夹中所有受Spring支持的二进制文件生成包装器(在此处查看,不要忘记提交它们).之后,您应该使用bin/rspec
运行rspec
.第一次运行仍然会很慢,但是所有后续运行都应该足够快,因为Rails已被加载.
Last command will generate wrappers for all spring supported binaries in your project's bin
folder (take a look there and don't forget to commit them). After that you should run rspec
with bin/rspec
. First run will be still slow, but all consequent runs should be fast enough since Rails will be already loaded.
这篇关于RSpec的加载时间在OS X上令人难以置信的长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!