简体   繁体   English

Rails 3 Cli 执行命令真的很慢吗?

[英]Rails 3 Cli executes commands really slow?

somebody knows why my rails 3.0.7 cli is so slow?有人知道为什么我的 rails 3.0.7 cli 这么慢吗? when i run rails s or rails g it takes like 5sec till he actually executes the command...当我运行rails srails g时,他需要 5 秒才能真正执行命令......

any advice?有什么建议吗? thanks谢谢

Update: I'm switching my recommendation from rrails to rails-sh as the former supports REPL which is not the use case for rrails.Also patching does seem to add performance when combined with ruby environment variables, now reflected in the answer.更新:我将我的建议从 rrails 切换到 rails-sh,因为前者支持 REPL,这不是 rrails 的用例。当与 ruby 环境变量结合使用时,修补似乎确实可以提高性能,现在反映在答案中。


One possible reason may be this performance bug in ruby that has it call some code whenever "require" is used in the ruby code (more details here ).一个可能的原因可能是 ruby 中的这个性能错误,只要在 ruby 代码中使用“require”,它就会调用一些代码(更多细节在这里)。 I ran in to this issue as well on my development box when developing with Rails (I use rails 3.2.6 on ruby 1.9.3p194 at the moment).我在使用 Rails 开发时也遇到了这个问题(我目前在 ruby 1.9.3p194 上使用 rails 3.2.6)。 The development environment runs on Ubuntu but this problem may happen on other operating systems because it's based on the interpreter.开发环境在 Ubuntu 上运行,但是这个问题可能在其他操作系统上发生,因为它是基于解释器的。

While that bug is not fixed, there are two things I did to shave time from my ruby CLI.虽然该错误尚未修复,但我做了两件事来节省 ruby CLI 的时间。 The first is to preload with rails-sh and the second is to use a popular ruby performance boosting patch to build an MRI ruby that is faster.第一个是使用 rails-sh 预加载,第二个是使用流行的 ruby 性能提升补丁来构建更快的 MRI ruby。

There are two libraries that do preloading nicely: rrails and rails-sh .有两个库可以很好地进行预加载: rrailsrails-sh Both are great but I will discuss rails-sh because it offers REPL support for commands like rails console in the terminal and binding.pry / debugger in the code.两者都很棒,但我将讨论rails-sh ,因为它为终端中的rails console和代码中的binding.pry / debugger等命令提供 REPL 支持。

Setup rails-sh设置 rails-sh

I put it in my development group since that's where I use rails/rake commands often and need speed.我把它放在我的开发组中,因为那是我经常使用 rails/rake 命令并且需要速度的地方。

group :development do
#...
  gem 'rails-sh'
end

Then install it:然后安装它:

bundle install --binstubs=./bundler_stubs

(I use binstubs to avoid 'bundle exec' in commands but that's optional) (我使用 binstubs 来避免命令中的“bundle exec”,但这是可选的)

Now just open a spare terminal to your rails project and run the rails-sh (add bundle exec if you need to):现在只需为您的 rails 项目打开一个备用终端并运行 rails-sh(如果需要,添加bundle exec ):

$ rails-sh

.     ....    ...      ..  .......    ............    ...  ..  .
.  ..  ..  ..  ....  ....  ......  ..............  ......  ..  .
.     ...      ....  ....  .......    ...      ...    ...      .
.  ..  ..  ..  ....  ....  ..........  ..............  ..  ..  .
.  ..  ..  ..  ..      ..      ...    ............    ...  ..  .
................................................................
                                                         v1.5.2

# require /home/yuvilio/ws/site/config/boot
# require /home/yuvilio/ws/site/config/application
# Rails.application.require_environment!
Rails.env: development
type `help` to print help
rails-sh(site)>

Now you can use rake and rails command in that prompt现在您可以在该提示中使用 rake 和 rails 命令

rails-sh(site)> rails s
$ rails s
=> Booting Thin
=> Rails 3.2.6 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop

Or to run a rake task like db:test:prepare:或者运行像 db:test:prepare 这样的 rake 任务:

rails-sh(site)> rake db:test:prepare
12.471001136sec
rails-sh(site)> 

But is it fast?但它快吗? Well, on my machine (a core i5 laptop with 8gig RAM), the same rake db:test:prepare took 12.5 seconds inside the rails-sh command (see above) compared to 34 seconds without it:好吧,在我的机器上(一台具有 8gig RAM 的核心 i5 笔记本电脑),相同的rake db:test:prepare在 rails-sh 命令(见上文)内需要 12.5 秒,而没有它则需要 34 秒:

$ time rake db:test:prepare
real  0m34.796s
user  0m21.057s
sys 0m1.144s
$

The 22 second difference was that the rake command outside rails-sh had to load the rails environment before getting to the database, which is wasteful since it hadn't changed. 22 秒的区别在于 rails-sh 外部的 rake 命令必须在访问数据库之前加载 rails 环境,因为它没有更改,所以这是一种浪费。 The same applies for rails commands.这同样适用于rails命令。

Patched ruby已修补 ruby

Another solution for MRI rubies, which is compatible with preloading and suggested in the rails performance guide , is to install a popular patch ( falcon , railsexpress) on your ruby 1.9 and add some environment variables that use it. MRI rubies 的另一个解决方案与预加载兼容并在rails 性能指南中建议,是在 ruby 1.9 上安装流行的补丁( falcon 、railsexpress)并添加一些使用它的环境变量。

I tested out the falcon and railsexpress patches (separately) on an rvm install and picked up similar performance in both cases.我在 rvm 安装上(分别)测试了 falcon 和 railsexpress 补丁,并在两种情况下都获得了相似的性能。

$ rvm get head
$ rvm cleanup sources
$ rvm install ruby-1.9.3-p194 --reconfigure  --name falcon  --patch falcon --force-autoconf -j 3
$ rvm use ruby-1.9.3-p194-falcon

To make use of the patch使用补丁

export RUBY_HEAP_MIN_SLOTS=1000000
export RUBY_HEAP_SLOTS_INCREMENT=1000000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=1000000000
export RUBY_HEAP_FREE_MIN=500000

You can track which patches are available for which rubies in rvm here .您可以在此处跟踪 rvm 中哪些红宝石可用的补丁。

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

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