Rails 3 - 加快控制台加载时间 [英] Rails 3 - Speed up Console Loading Time

查看:24
本文介绍了Rails 3 - 加快控制台加载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何相对简单的方法来加快我的控制台加载时间,它开始接近 30 秒.我有很多子类的方法似乎不受 reload! 的影响,所以我最终打开和关闭了很多控制台.IRB 加载闪电般快速.

I am wondering if there is any relatively easy way to speed up my console load time, which is starting to approach 30 seconds. I have a lot of subclasses whose methods don't seem to be affected by reload! so I end up opening and closing the console a lot. IRB loads lightning quick.

我的宝石太多了吗?我如何安排加载任务的时间,以便我可以看到什么占用了最多的时间?如您所见,我已经尝试过 dev-boost gem 无济于事.该应用程序在Passenger 中很好,只是控制台加载让我感到很糟糕.在具有 2.4GHz 和 4GB RAM 的 MBP OSX 10.6.6 上运行.不使用 RVM.

Do I have too many gems? How do I go about timing the load tasks so I can see what is taking up the most time? As you can see, I've already tried the dev-boost gem to no avail. The app is fine in Passenger, it's just the console loading that bugs the crap out of me. Running on MBP OSX 10.6.6 with 2.4GHz and 4GB RAM. Not using RVM.

版本:

Ovid$ rails -v
Rails 3.0.3
Ovid$ ruby -v
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10]

内存:

Ovid$ vm_stat
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free:                         118818.
Pages active:                       341320.
Pages inactive:                      99490.
Pages speculative:                  310576.
Pages wired down:                   112527.
"Translation faults":             23097323.
Pages copy-on-write:               1270961.
Pages zero filled:                13836659.
Pages reactivated:                      36.
Pageins:                            165761.
Pageouts:                                0.
Object cache: 28 hits of 760846 lookups (0% hit rate)

宝石文件:

source 'http://rubygems.org'

gem 'rails', '3.0.3'
gem 'mysql2'
gem 'foreigner'
gem 'haml'
gem 'capistrano'
gem 'nokogiri'

#web services
gem 'yammer4r'
gem 'ruby-freshbooks'

#authentication gems from nifty generator
gem "bcrypt-ruby", :require => "bcrypt"
gem "mocha", :group => :test
gem 'authlogic'

#dev
group :development do
  gem 'rails-dev-boost', :git => 'git://github.com/thedarkone/rails-dev-boost.git', :require => 'rails_development_boost'
end

#testing
group :test do
  gem 'database_cleaner'
  gem 'cucumber-rails'
  gem 'cucumber'
  gem 'rspec-rails'
  gem 'spork'
  gem 'launchy'
  gem 'machinist'
  gem 'faker'
  gem 'capybara'
end

非常感谢!

推荐答案

我终于使用 Benchmark 发现了我的启动瓶颈.特别是,导航到 bundler gem 并在 lib/bundler/runtime.rb 中找到执行 Kernel.require 的行并将其包装如下:

I finally found my startup bottlenecks using Benchmark. In particular, navigate to the bundler gem and in lib/bundler/runtime.rb, find the line that does Kernel.require and wrap it like this:

puts Benchmark.measure("require #{file}") {
  Kernel.require file
}.format("%n: %t %r")

您可能需要在应用程序的某处添加 require 'benchmark',例如在 config/boot.rb 中.这将告诉您需要多长时间才能需要每个 gem.我不能保证你的结果会和我的一样,但我有一些 gems 加载时间超过一秒钟,而大多数情况下是亚毫秒.有一些是我开发不需要的 gem,但我确实需要在开发环境中完成一些任务,例如卡皮斯特拉诺岛我对启动的其他领域(初始化程序等)进行了基准测试,但找不到任何明显的瓶颈.

You may have to add require 'benchmark' somewhere in your app, like in config/boot.rb. That will show you how long it takes to require each gem. I can't guarantee your results will match mine, but I had a few gems that were taking over a second to load compared with sub-millisecond for most. A few were gems that I didn't need for developing but I did need for some tasks in the development environment, e.g. capistrano, shoulda. I benchmarked other areas of startup (initializers, etc), but couldn't find any significant bottlenecks.

我还没有想出一种干净的方法来配置应用程序以仅加载那些真正需要它们的任务.可能,我可以创建一个名为 :speedy 的环境,并在我知道不需要这些 gem 时使用 RAILS_ENV=speedy rails s/c 进行启动.然后在 Gemfile 中,我可以使用 group :speedy 在某些情况下排除这些 gem.

I haven't yet figured out a clean way to configure the app to only load those for tasks where they are really needed. Possibly, I could create an environment called :speedy and use RAILS_ENV=speedy rails s/c for startup when I know I don't need those gems. Then in Gemfile, I could use group :speedy to exclude those gems in certain cases.

综上所述,对我来说最大的启动烦恼是必须加载整个环境来运行 rake 任务.我可能会为此排除大多数 gem,但是 Gemfile 会开始变得混乱,所以我不知道它是否值得.

All that said, the biggest startup annoyance for me is having to load the entire environment to run a rake task. I could probably exclude most gems for that, but Gemfile would start to get messy so I don't know if it's worth it.

这篇关于Rails 3 - 加快控制台加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆