简体   繁体   English

为什么rails bootstrap这么慢,我该怎么办呢?

[英]Why is rails bootstrap so slow and what can I do about it?

Rails 3.0 is a serious dog. Rails 3.0是一只严肃的狗。 I've been developing on Rails for 5 years, and it's never been slower to startup. 我已经在Rails上开发了5年,而且启动时从未如此慢。 In particular, tests take forever to bootstrap on a top notch MacBook with SSD, so iteration cycles suffer severely. 特别是,测试需要永远在带有SSD的顶级MacBook上进行自举,因此迭代周期会受到严重影响。 I need to read an article every time I start a test. 每次开始测试我都需要阅读一篇文章。 It's insane and not "agile". 这是疯狂而不是“敏捷”。 I might as well be compiling. 我不妨编译。

This is the major motivation for me to finally move on from Rails - when I have the chance. 这是我最终从Rails继续前进的主要动机 - 当我有机会的时候。

If anyone has a solution, please offer it. 如果有人有解决方案,请提供。 I know many people suffer from this problem. 我知道很多人都患有这个问题。

I don't use rspec - I know there is a solution to help with tests for rspec. 我不使用rspec - 我知道有一个解决方案来帮助测试rspec。

I'm using faster_require and rails-dev-boost, but there is no significant impact. 我正在使用faster_require和rails-dev-boost,但没有重大影响。

I'm on ruby 1.9.2 and need to be. 我在ruby 1.9.2并且需要。 A single pretty simple controller test takes 26 seconds on a dual core 2.13 Ghz MacBook Air with 4Gb RAM and SSD! 在具有4Gb RAM和SSD的双核2.13 Ghz MacBook Air上进行一次非常简单的控制器测试需要26秒! Why!? 为什么!?

Check out spork . 看看spork It essentially runs a small server which runs the bootstrapping and then waits for test runs, allowing you to bootstrap once and then test many times from that state. 它本质上运行一个小型服务器,它运行引导,然后等待测试运行,允许你引导一次,然后从该状态多次测试。 You will occasionally have to restart it if you do certain kinds of changes to the configuration, but for the most part it lets you have really fast red-green-refactor iterations. 如果对配置进行某些更改,您偶尔会重新启动它,但在大多数情况下,它可以让您进行非常快速的红绿重构迭代。

To quote Yehuda Katz: 引用Yehuda Katz:

There are things that the C require code does in 1.9 that slow things down. 有些事情需要C代码在1.9中做的事情会减慢速度。 One such example is re-checking $LOAD_PATH to make sure it is all expanded on every require. 一个这样的例子是重新检查$ LOAD_PATH以确保它在所有需求上都被扩展。 This is something that should be addressed by ruby-core. 这是ruby-core应该解决的问题。 I'll open a ticket on redmine if there isn't one already. 如果没有redmine,我会在redmine上打开一张票。

I am also experiencing this problem and a $LOAD_PATH issue seems like a potential cause. 我也遇到了这个问题,$ LOAD_PATH问题似乎是一个潜在的原因。 Lets hope it gets fixed soon. 让我们希望很快得到修复。

So it sounds to me like you'll just need to suffer through this and hopefully it will be fixed in Ruby 1.9.3. 所以听起来像你只需要忍受这一点,希望它将在Ruby 1.9.3中修复。

the problem seem to be the slowness of require, if you do some minimal profiling like 问题似乎是需求的缓慢,如果你做一些最小的分析,如

CPUPROFILE=/tmp/my_app_profile RUBYOPT="-r`gem which perftools | tail -1`" ruby -I lib:test test/unit/user_test.rb
pprof.rb --text /tmp/my_app_profile

you should probably see a lot of time being spent in require (another option is seeing a lot of it in loading fixture but I guess you already know that is not the issue). 你可能应该看到很多时间花在require上(另一个选择是在装载夹具中看到很多但是我猜你已经知道这不是问题)。

In my box (osx on a macbook pro 2011 13", with core i5 and a spinning hard drive), the time is more in the order of 5 seconds, which is still a lot but way more tolerable, so maybe it's worth investigating differences. 在我的盒子里(osx在macbook pro 2011 13“上,核心i5和旋转硬盘驱动器),时间大约为5秒,这仍然很多,但更容易忍受,所以也许值得调查差异。

How many gems do you have installed in your system? 你在系统中安装了多少颗宝石? Have you tried running the tests/booting without bundler/rubygems? 您是否尝试在没有bundler / ruby​​gems的情况下运行测试/启动?

It is a tradeoff to pay for not having to write 'require' statements. 支付不必编写“要求”声明是一种权衡。 Rails is trying to do the right thing, and therefore preloads everything. Rails正在尝试做正确的事情,因此预先加载一切。 Basically you have two solutions: 基本上你有两个解决方案:

  1. Use preloading library that spawns Rails process ( spork for example) 使用产生Rails进程的预加载库(例如spork
  2. Don't use Rails in your tests, extract domain that you can test outside of Rails (Destroy All Software has one screencast about this). 不要在测试中使用Rails,提取可以在Rails之外测试的域(Destroy All Software有一个关于此的截屏视频)。 It boils down to skipping loading Rails , when possible. 如果可能的话,它归结为跳过加载Rails

With all that said I am, using solution 1). 尽管如此,使用解决方案1)。 and having my suite run under half a second (only 70 tests, but waitng for Rails to load is minimal) 让我的套件运行不到半秒(只有70次测试,但是等待Rails加载的次数很少)

Yes, it's rediculously slow. 是的,它的速度非常缓慢。 Oh wait, mvn install in my java days never took less than a minute. 哦等等,在我的java天mvn install永远不会花不到一分钟。 With mvn clean install it was crazy slow. 使用mvn clean install它很疯狂。 Happy days, remembering always gets my spirit up. 快乐的日子,记住总会让我的精神振作起来。

+1 spork , as @hammar suggests 正如@hammar建议的那样,+1 spork

combine with guard https://github.com/guard/guard-test for auto-starting the tests and you have a killer combo. 结合guard https://github.com/guard/guard-test自动启动测试,你有一个杀手组合。 Have it running in it's own window and/or screen and see your productivity go up :) 让它在自己的窗口和/或屏幕上运行,看看你的工作效率上升:)

Update/addition to answer: 更新/补充回答:

Optimization tip: put a to_s on paths added to autoload_paths, in application.rb, avoiding it to manually do this over and over again runtime. 优化提示:在application.rb中添加到autoload_paths的路径上放置to_s ,避免它一遍又一遍地手动执行此操作。 Might shave off a few seconds... 可能会刮掉几秒钟......

As @user185374 says, you're paying the tradeoff of the require . 正如@ user185374说,你付出的权衡require

I stumbled upon an interesting resource to speed up Rails tests without using Rails ... The example is given with Rspec but you'll have the logic. 我偶然发现了一个有趣的资源来加速Rails测试而不使用Rails ......这个例子是用Rspec给出的,但你会有逻辑。

Here is how the writer sums up: 以下是作者总结的方式:

I can't tell you what it means to run all of my 150+ specs within 2 seconds. 我无法告诉你在2秒内运行我所有的150多个规格意味着什么。 I think it's a little bit of an extra work, but it's well worth the effort! 我认为这是一项额外的工作,但值得付出努力!

Another solution I just found: Hydra parallel testing: 我刚发现的另一个解决方案: Hydra并行测试:

http://logicalfriday.com/2011/05/18/faster-rails-tests-with-hydra/ http://logicalfriday.com/2011/05/18/faster-rails-tests-with-hydra/

This is a known problem, especially on Ruby 1.9.2. 这是一个已知问题,特别是在Ruby 1.9.2上。

The good news is that there's a patch to speed up Ruby start-up ( require ) immensely. 好消息是,有一个补丁可以极大地加速Ruby的启动( require )。

Good news. 好消息。 Rails starts within a tolerable amount of time after upgrading to Ruby 1.9.3. 升级到Ruby 1.9.3后,Rails在一段可容忍的时间内启动。

Check out the zeus gem . 看看宙斯宝石

This should speed things up quite significantly. 这应该会显着加快速度。

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

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