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...
any advice? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:我将我的建议从 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。
有两个库可以很好地进行预加载: rrails 和 rails-sh。两者都很棒,但我将讨论 rails-sh,因为它为终端中的
rails console
和binding.pry
/< 等命令提供 REPL 支持。代码中的 code>debugger 。设置rails-sh
我把它放在我的开发组中,因为那是我经常使用rails/rake 命令并且需要速度的地方。
然后安装它:(
我使用 binstubs 来避免命令中的“bundle exec”,但这是可选的)
现在只需打开一个备用终端到您的 Rails 项目并运行 Rails-sh(如果需要,请添加
bundle exec
to):现在您可以在该提示符中使用 rake 和rails 命令
或者运行像 db:test:prepare: 这样的 rake 任务,
但是它快吗?嗯,在我的机器(具有 8gig RAM 的核心 i5 笔记本电脑)上,相同的
rake db:test:prepare
在rails-sh命令中花费了 12.5 秒(见上文),而没有它则花费了 34 秒:22 秒的区别在于,rails-sh 外部的 rake 命令必须在访问数据库之前加载 Rails 环境,这是浪费的,因为它没有更改。这同样适用于
rails
命令。Patched ruby
MRI rubies 的另一种解决方案,它与预加载兼容,并在 中建议Rails性能指南,就是安装一个流行的补丁(falcon, Railsexpress)在您的 ruby 1.9 上并添加一些使用它的环境变量。
我在 rvm 安装上(分别)测试了 falcon 和 Railsexpress 补丁,并在两种情况下获得了相似的性能。
使用补丁
您可以在 此处跟踪哪些补丁可用于 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 andbinding.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.
Then install it :
(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):Now you can use rake and rails command in that prompt
Or to run a rake task like db:test:prepare:
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: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.
To make use of the patch
You can track which patches are available for which rubies in rvm here.