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

发布于 2024-11-24 02:16:03 字数 129 浏览 7 评论 0原文

有人知道为什么我的 Rails 3.0.7 cli 这么慢?当我运行 rails srails 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...

any advice? thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2024-12-01 02:16:04

更新:我将我的建议从 rrails 切换到rails-sh,因为前者支持 REPL,这不是 rrails 的用例。而且,现在与 ruby​​ 环境变量结合使用时,修补似乎确实会增加性能反映在答案中。


一个可能的原因可能是 ruby 中的性能错误,它会在任何时候调用一些代码ruby 代码中使用了“require”(更多详细信息此处< /a> )。在使用 Rails 进行开发时,我的开发盒也遇到了这个问题(目前我在 ruby​​ 1.9.3p194 上使用 Rails 3.2.6)。开发环境运行在Ubuntu上,但在其他操作系统上也可能出现此问题,因为它是基于解释器的。

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

有两个库可以很好地进行预加载: rrailsrails-sh。两者都很棒,但我将讨论 rails-sh,因为它为终端中的 rails consolebinding.pry/< 等命令提供 REPL 支持。代码中的 code>debugger 。

设置rails-sh

我把它放在我的开发组中,因为那是我经常使用rails/rake 命令并且需要速度的地方。

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

然后安装它:(

bundle install --binstubs=./bundler_stubs

我使用 binstubs 来避免命令中的“bundle exec”,但这是可选的)

现在只需打开一个备用终端到您的 Rails 项目并运行 Rails-sh(如果需要,请添加 bundle exec 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 和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

或者运行像 db:test:prepare: 这样的 rake 任务,

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

但是它快吗?嗯,在我的机器(具有 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
$

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

Patched ruby

​​ MRI rubies 的另一种解决方案,它与预加载兼容,并在 中建议Rails性能指南,就是安装一个流行的补丁(falcon, Railsexpress)在您的 ruby​​ 1.9 上并添加一些使用它的环境变量。

我在 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

使用补丁

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 中的哪些 rubies

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.


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.

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.

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.

Setup rails-sh

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

Then install it :

bundle install --binstubs=./bundler_stubs

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

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

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

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

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:

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

Patched 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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文