Rails 3 Cli执行命令真的很慢吗? [英] Rails 3 Cli executes commands really slow?

查看:109
本文介绍了Rails 3 Cli执行命令真的很慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么我的rails 3.0.7 cli这么慢吗?当我运行 rails s rails g 时,要花5秒钟才能真正执行命令...

somebody knows why my rails 3.0.7 cli is so slow? when i run rails s or rails g it takes like 5sec till he actually executes the command...

有什么建议吗?谢谢

推荐答案

更新:我将我的建议从rrails切换为rails-sh,支持REPL,这不是rrails的用例。修补程序与ruby环境变量结合使用时确实可以提高性能,现在已经在答案中得到了体现。

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.

一个可能的原因可能是红宝石中的性能错误只要在Ruby代码中使用 require,它就会调用一些代码(更多详细信息此处)。使用Rails进行开发时,我也在开发箱中遇到了这个问题(目前我在ruby 1.9.3p194上使用rails 3.2.6)。开发环境可以在Ubuntu上运行,但是由于它是基于解释器的,因此在其他操作系统上可能会发生此问题。

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 ). 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). The development environment runs on Ubuntu but this problem may happen on other operating systems because it's based on the interpreter.

虽然该错误未修复,但我做了两件事从我的ruby CLI节省时间。第一个是预加载rails-sh,第二个是使用流行的红宝石性能增强补丁来构建更快的MRI红宝石。

While that bug is not fixed, there are two things I did to shave time from my 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.

有两个可以很好地预加载的库: rrails rails-sh 。两者都很棒,但我将讨论 rails-sh ,因为它为终端中的 rails console 之类的命令提供REPL支持。代码中的binding.pry / debugger

There are two libraries that do preloading nicely: rrails and rails-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 / rake命令并且需要速度的地方。

I put it in my development group since that's where I use rails/rake commands often and need speed.

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

然后安装它:

bundle install --binstubs=./bundler_stubs

(我使用binstubs避免在命令中使用 bundle exec,但这是可选的)

(I use binstubs to avoid 'bundle exec' in commands but that's optional)

现在只需打开一个备用终端到您的rails项目并运行rails-sh(添加捆绑执行程序(如果需要):

Now just open a spare terminal to your rails project and run the rails-sh (add bundle exec if you need to):

$ 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)>

现在您可以在该提示下使用rake and rails命令

Now you can use rake and rails command in that prompt

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

或运行类似db的rake任务:test:prepare:

Or to run a rake task like db:test:prepare:

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

但是很快吗?好吧,在我的机器(具有8gig RAM的核心i5笔记本电脑)上,相同的 rake db:test:prepare 在rails-sh命令(请参见上文)中花费了12.5秒到没有它的34秒:

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:

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

22秒的区别是rails-sh外部的rake命令必须在进入数据库之前加载rails环境,这是浪费的,因为它没有更改。这同样适用于 rails 命令。

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. The same applies for rails commands.

另一种适用于MRI红宝石的解决方案,它与预加载兼容,并在中建议rails性能指南,是在ruby 1.9上安装一个流行的补丁程序( falcon ,railsexpress)。并添加一些使用它的环境变量。

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.

我在rvm安装上分别测试了falcon和railsexpress补丁,并在两种情况下均获得了类似的性能。

I tested out the falcon and railsexpress patches (separately) on an rvm install and picked up similar performance in both cases.

$ 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

要使用补丁

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

您可以在rvm中跟踪哪些补丁可用于哪些红宝石此处

You can track which patches are available for which rubies in rvm here.

这篇关于Rails 3 Cli执行命令真的很慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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