Tro*_*nic 6 ruby performance ruby-on-rails command-line-interface execution
有人知道为什么我的rails 3.0.7 cli这么慢?当我跑步rails s或者rails g需要5秒时才实际执行命令......
任何建议?谢谢
更新:我正在将我的建议从rrails切换到rails-sh,因为前者支持REPL,这不是rrails的用例.而且,当与ruby环境变量结合时,修补似乎确实增加了性能,现在反映在答案中.
一个可能的原因可能是ruby中的这个性能错误,只要在ruby代码中使用"require"就会调用一些代码(这里有更多细节).在使用Rails进行开发时,我在开发框中也遇到了这个问题(我现在在ruby 1.9.3p194上使用rails 3.2.6).开发环境在Ubuntu上运行,但是这个问题可能在其他操作系统上发生,因为它基于解释器.
虽然这个bug没有修复,但我做了两件事来从我的ruby CLI中节省时间.第一个是使用rails-sh进行预加载,第二个是使用流行的红宝石性能增强补丁来构建更快的MRI红宝石.
有两个库可以很好地进行预加载:rrails和rails-sh.两者都很棒,但我会讨论rails-sh,因为它为rails console终端和binding.pry/ debugger和代码中的命令提供了REPL支持.
我把它放在我的开发组中,因为我经常使用rails/rake命令并需要速度.
group :development do
#...
gem 'rails-sh'
end
Run Code Online (Sandbox Code Playgroud)
然后安装它:
bundle install --binstubs=./bundler_stubs
Run Code Online (Sandbox Code Playgroud)
(我使用binstubs来避免命令中的'bundle exec',但这是可选的)
现在只需打开一个备用终端到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)>
Run Code Online (Sandbox Code Playgroud)
现在,您可以在该提示中使用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
Run Code Online (Sandbox Code Playgroud)
或者运行像db这样的rake任务:test:prepare:
rails-sh(site)> rake db:test:prepare
12.471001136sec
rails-sh(site)>
Run Code Online (Sandbox Code Playgroud)
但它快吗?好吧,在我的机器上(带有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
$
Run Code Online (Sandbox Code Playgroud)
22秒的区别在于rails-sh之外的rake命令必须在到达数据库之前加载rails环境,这是浪费的,因为它没有改变.这同样适用于rails命令.
MRI红宝石的另一种解决方案是与预加载兼容并在导轨性能指南中提出,是在ruby 1.9上安装一个流行的补丁(falcon,railsexpress)并添加一些使用它的环境变量.
我在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
Run Code Online (Sandbox Code Playgroud)
要使用补丁
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
Run Code Online (Sandbox Code Playgroud)
您可以在此处跟踪rvm中哪些红宝石可用于哪些补丁.