简体   繁体   English

Rails 3 - 加快控制台加载时间

[英]Rails 3 - Speed up Console Loading Time

I am wondering if there is any relatively easy way to speed up my console load time, which is starting to approach 30 seconds.我想知道是否有任何相对简单的方法可以加快我的控制台加载时间,它开始接近 30 秒。 I have a lot of subclasses whose methods don't seem to be affected by reload!我有很多子类的方法似乎不受reload! so I end up opening and closing the console a lot.所以我最终打开和关闭控制台很多。 IRB loads lightning quick. IRB 加载快如闪电。

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?我如何 go 关于定时加载任务,以便我可以看到占用最多时间的内容? As you can see, I've already tried the dev-boost gem to no avail.如您所见,我已经尝试过 dev-boost gem 无济于事。 The app is fine in Passenger, it's just the console loading that bugs the crap out of me.该应用程序在Passenger中很好,只是控制台加载使我大吃一惊。 Running on MBP OSX 10.6.6 with 2.4GHz and 4GB RAM.在具有 2.4GHz 和 4GB RAM 的 MBP OSX 10.6.6 上运行。 Not using RVM.不使用 RVM。

Versions:版本:

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

Memory: Memory:

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)

Gemfile:宝石文件:

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

Thank you very much!非常感谢!

I finally found my startup bottlenecks using Benchmark.我终于使用 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:特别是,导航到 bundler gem 并在 lib/bundler/runtime.rb 中找到执行 Kernel.require 的行并将其包装如下:

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

You may have to add require 'benchmark' somewhere in your app, like in config/boot.rb.你可能需要在你的应用程序的某个地方添加 require 'benchmark',比如在 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, eg capistrano, shoulda.一些是我不需要开发的gem,但我确实需要在开发环境中执行一些任务,例如 capistrano、should。 I benchmarked other areas of startup (initializers, etc), but couldn't find any significant bottlenecks.我对启动的其他领域(初始化程序等)进行了基准测试,但找不到任何明显的瓶颈。

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.可能,我可以创建一个名为 :speedy 的环境,并在我知道不需要这些 gem 时使用 RAILS_ENV=speedy rails s/c 进行启动。 Then in Gemfile, I could use group :speedy to exclude those gems in certain cases.然后在 Gemfile 中,我可以使用 group :speedy 在某些情况下排除这些 gem。

All that said, the biggest startup annoyance for me is having to load the entire environment to run a rake task.尽管如此,对我来说最大的启动烦恼是必须加载整个环境来运行 rake 任务。 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.我可能会为此排除大多数 gem,但是 Gemfile 会开始变得混乱,所以我不知道它是否值得。

Slightly adapted form that is copy-pastable, wraps all requires, and provides sortable output:稍微改编的形式,可复制粘贴,包装所有要求,并提供可排序的输出:

# Add this to the top of boot.rb
require 'benchmark'
def require(file)
  puts Benchmark.measure("") {
    super
  }.format("%t require #{file}")
end

Then you can execute no-op to see them:然后你可以执行 no-op 来查看它们:

rails runner 1

Or sort them and show the top 50:或者对它们进行排序并显示前 50 个:

rails runner 1 | sort -nr | head -n 50

You can speed it up by adding :require => nil to the slow Gemfile entries and require them manually.您可以通过将 :require => nil 添加到缓慢的 Gemfile 条目并手动要求它们来加快速度。 eg例如

gem 'jammit', :require => nil

I also addressed this issue in a meetup i've had.我也在我的一次聚会中解决了这个问题。 This seems to be a bug in ruby 1.9.2 (see comments of this patch: https://gist.github.com/1008945 )这似乎是 ruby​​ 1.9.2 中的一个错误(请参阅此补丁的评论: https : //gist.github.com/1008945

You can fix it by patching your 1.9.2 by the gist i just linked or upgrading to 1.9.2-head or 1.9.3-head .您可以通过我刚刚链接或升级到1.9.2-head1.9.3-head的要点修补您的1.9.2来修复它。

This is definitely about cleaning up your code and identifying bottlenecks, but once you've made those savings it's worth looking at something like Zeus to speed up your dev times.这绝对是关于清理您的代码和识别瓶颈,但是一旦您节省了这些费用,就值得考虑像 Zeus 这样的东西来加快您的开发时间。

gem install zeus

https://github.com/burke/zeus (docs) https://github.com/burke/zeus (文档)

It's not without bugs and does sometimes require a reboot but I am still seeing an overall increase in development times by fast server and console reboots after small code changes.它并非没有错误,有时确实需要重新启动,但我仍然看到在小代码更改后快速服务器和控制台重新启动,开发时间总体增加。

I can only suggest putting on your lab coat and bisecting the issue.我只能建议穿上你的实验服并将问题一分为二。 See if commenting out all your gem requirements speeds things up (presumably that'll also involve commenting out pieces of code that rely on those gems).看看注释掉所有 gem 要求是否会加快速度(大概这还涉及注释掉依赖于这些 gem 的代码段)。 If so, comment out half at a time and so on.如果是这样,一次注释掉一半,依此类推。

Sorry this isn't a real answer.. You could try ruby-prof I suppose, for example by invoking it with rails runner and a no-op script.抱歉,这不是一个真正的答案.. 我想您可以尝试使用 ruby​​-prof,例如通过使用rails runner和无操作脚本调用它。

I tried ruby-prof script/rails runner 'nil' on my mac but it appears to have just crashed :-)我在我的 mac 上尝试了ruby-prof script/rails runner 'nil' ,但它似乎刚刚崩溃:-)

EDIT编辑

If you're using git for your app you could try it's bisect command too and see if there's a specific point in time when things got slow, rather than just general bloat.如果你正在为你的应用程序使用 git,你也可以尝试它的 bisect 命令,看看是否有一个特定的时间点变得缓慢,而不仅仅是一般的膨胀。

Reload!重新加载! has been an issue for some time now.已经有一段时间了。 Have a look at this .看看这个 There are some patches which you could use and some tips on how you maybe able to get around your issue.您可以使用一些补丁以及一些有关如何解决问题的提示。

The reload method itself looks like this. reload 方法本身看起来像这样。

# reloads the environment
def reload!(print=true)
  puts "Reloading..." if print
  ActionDispatch::Callbacks.new(lambda {}, false).call({})
  true
end

You could always add you environment to this method to override it function and force the reload you require.您始终可以将环境添加到此方法以覆盖它的功能并强制您需要重新加载。

This has worked for me so let us know if it works for you.这对我有用,所以让我们知道它是否适合你。 All the best.一切顺利。

Very old question & answers, but it's still a useful one...很老的问题和答案,但它仍然是一个有用的...

Somehow have an error with jqr answer, which seems related to autoload (rails 5.2...).不知何故,jqr 答案有错误,这似乎与自动加载有关(rails 5.2 ...)。

-e:1:in `<main>'                                                                                                                                                                                           
0.002514 require i18n/config                                                                                                                                                                               
.../vendor/bundle-dev/ruby/2.7.0/gems/i18n-1.8.7/lib/i18n.rb:19:in `<module:I18n>': uninitialized constant I18n::Config                                           
Did you mean?  RbConfig (NameError)                                                 

Fixed with this :用这个固定:

# Add this to the top of boot.rb
require 'benchmark'
def require(file)
  mod = nil
  puts Benchmark.measure("") {
    mod = super
  }.format("%t require #{file}")
  mod
end

The Bumbler gem is a great little tool for determining gem load speed and identifying slow-loading gems. Bumbler gem 是一个很棒的小工具,用于确定 gem 加载速度和识别加载缓慢的 gem。

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

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