如何加快 Ruby/Rake 任务的速度
rake --tasks 运行大约需要 18 秒。这只是加载所有任务所需的时间,因此我定义的任何任务都将至少花费这个时间来运行:
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
我的 Rakefile:
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
知道为什么 rake 需要很多时间吗? 谢谢
rake --tasks takes about 18s to run. This is just the time it takes to load all the tasks, as a result any task I define will take at least this amount of time to run :
$time rake --tasks
rake db:clean # Cleaning up database
rake passenger:restart # Restart Application
rake spec # Run specs
real 0m18.816s
user 0m7.306s
sys 0m5.665s
My Rakefile :
$: << "."
require "rubygems"
require "rspec/core/rake_task"
desc "Run those specs"
task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = %w{--colour --format progress}
t.pattern = 'spec/*_spec.rb'
end
end
task :default => :spec
Any idea why rake takes to much times ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试 spring
命令行将如下所示:
第一次运行将花费更多时间,但后续运行将非常快。
Try spring
Command line will look like:
It will take more time running the first time, but subsequent runs will be very fast.
这个解决方案对我有用:Rails 中的更快的 rake 任务。
我必须做一些改变,创建一个 lib/tasks/no_rails 目录,并将所有不需要 Rails 的 Rake 文件放入其中,并仅加载使用上述方法的文件。
This solution worked for me: Faster rake tasks in Rails.
I had to do a little variation where I created a
lib/tasks/no_rails
directory and put all the Rake files which do not need Rails in there and loaded only those using the above method.我喜欢 Pratik 提到的解决方案,该解决方案适用于为需要它的任务而不是为那些不需要的任务加载导轨的一般情况,对于任何耙子任务而无需事先记住。
运行不需要 Rails 的 rake 任务的侵入性较小的方法是使用 -f rake 选项告诉 rake 使用特定的 Rakefile。这样,rake 就不会在所有的 Rails 中寻找 rake 任务。
例如,假设上面的任务位于项目顶层名为 Rakefile 的文件中,并且 Rakefile 没有执行任何加载 Rails 的操作,例如
require File.expand_path('../config/application', __FILE__ )
,你可以这样做:并且它应该更快地运行你的规范任务。尝试 $ time rake -f Rakefile -T ;我用我的独立于 Rails 的 Rakefile 执行此操作并得到:
缺点是您必须记住每次都指定此选项,如果您想从 Rails 运行 rake 任务(如 rake db),则不要指定它:迁移。
I like the solution Pratik mentions for the general case of loading rails for tasks that need it and not for those that don't, for any rake task without having to remember beforehand.
A less-invasive method to run a rake task that doesn't need rails is to use the
-f
rake option to tell rake to use a particular Rakefile. This way, rake won't go looking for rake tasks in all of rails.For example, assuming your task above is in a file called Rakefile at the top level of your project and your Rakefile doesn't do anything that loads Rails like
require File.expand_path('../config/application', __FILE__)
, you can do:and it should run your spec task much faster. Try
$ time rake -f Rakefile -T
; I did this with a rails-independent Rakefile of mine and got:The downside is you have to remember to specify this option every time, and not to specify it if you want to run a rake task from rails like
rake db:migrate
.必须加载整个 Rails 环境,因此即使是简单的 rake 任务(例如
rake --tasks
)也需要一段时间。使用rails console
或script/console
打开控制台需要类似的时间。您可以尝试破解 Ruby 或 Rails 加速 rake,但如果您想稍后切换到较新的版本,过多的优化可能会很糟糕。由于必须加载rails环境,清理路线也可能有帮助。The entire rails environment has to be loaded, therefore even simple rake tasks such as
rake --tasks
take a while. Opening a console withrails console
orscript/console
takes a similar time. You may try to hack Ruby or Rails to speed up rake, but too much optimization can be bad if you want to switch to a newer version later. Since the rails environment must be loaded, cleaning up routes may also help.