Rails开发服务器很慢,加载一个简单的页面需要很长时间

Dav*_*man 29 ruby performance ruby-on-rails

有关Rails在开发模式下速度慢的类似线程,但这些线程中没有一个解决方案对我有任何影响.我已经尝试安装可以提升性能并使用配置文件进行处理的宝石,但是没有成功.

我刚开始使用Rails,所以我在"入门Rails"指南中运行启动应用程序,这是一个小博客.我按照建议安装了Ruby 1.9.3和Rails 3.2.13.我在OS/X 10.7.5上运行.

当加载教程应用程序的起始页面时,它只需要1行文本和1个链接,需要20-40秒.对任何页面的每个后续请求都需要20-40秒.但是,当我看一下服务器日志时,Rails所做的一切似乎都花费了很长时间.这是日志中始终占用的事件之间的时间.作为Rails的初学者,我不知道如何调试它.

例如:

Started GET "/posts/1" for 127.0.0.1 at 2013-05-24 17:39:35 -0400
Processing by PostsController#show as HTML
  Parameters: {"id"=>"1"}
  Post Load (36.9ms)  SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1  [["id", "1"]]
  Comment Load (24.3ms)  SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1
  Rendered comments/_comment.html.erb (0.9ms)
  Rendered comments/_form.html.erb (25.8ms)
  Rendered posts/show.html.erb within layouts/application (158.5ms)
Completed 200 OK in 274ms (Views: 201.0ms | ActiveRecord: 61.9ms)


Started GET "/assets/home.css?body=1" for 127.0.0.1 at 2013-05-24 17:39:52 -0400
RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request.
Served asset /home.css - 304 Not Modified (0ms)
[2013-05-24 17:39:52] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/posts.css?body=1" for 127.0.0.1 at 2013-05-24 17:40:09 -0400
RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request.
Served asset /posts.css - 304 Not Modified (0ms)
[2013-05-24 17:40:09] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2013-05-24 17:40:12 -0400
RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request.
Served asset /jquery.js - 304 Not Modified (0ms)
[2013-05-24 17:40:12] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2013-05-24 17:40:16 -0400
RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request.
Served asset /scaffolds.css - 304 Not Modified (0ms)
[2013-05-24 17:40:16] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2013-05-24 17:40:19 -0400
RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request.
Served asset /jquery_ujs.js - 304 Not Modified (0ms)
[2013-05-24 17:40:19] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/home.js?body=1" for 127.0.0.1 at 2013-05-24 17:40:21 -0400
RailsDevTweaks: Skipping ActionDispatch::Reloader hooks for this request.
Served asset /home.js - 304 Not Modified (0ms)
[2013-05-24 17:40:21] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,初始GET从17:39:35开始,Rails处理所有内容最多几百毫秒(有时甚至是0毫秒),但每个事件之间的时间戳上升了几秒钟.最后一次活动是在17:40:19,也就是最初的GET后44秒.在实践中,这意味着我的浏览器中没有任何内容显示超过40秒.我不知道如何让Rails加速.即使在开发模式下,我也不认为应该使用一个简单的教程应用程序来加载1或2个模型.

任何想法如何缩小和解决这个问题?

注意:有关内容长度的警告应该与问题无关.当我降级到Ruby 1.9.3时,它们出现了.我使用的是最新的Ruby(2.0.0),但认为这是Rails性能缓慢的原因,所以我切换到推荐的Ruby 1.9.3并且这些警告首次出现.但是开发模式下的Rails很慢.

谢谢,戴夫

更新:为了帮助缩小问题范围,我禁用了资产管道,但确实显着加快了速度.它现在是4-8秒而不是20-40但是有新的错误,我想我在禁用资产管道时失去了一些功能.有没有办法加速资产管道并保持启用?

ActionController::RoutingError (No route matches [GET] "/stylesheets/application.css"):
  actionpack (3.2.13) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.13) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.13) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.13) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.13) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.13) lib/action_dispatch/middleware/static.rb:63:in `call'
  railties (3.2.13) lib/rails/engine.rb:479:in `call'
  railties (3.2.13) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.13) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /Users/ss/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Users/ss/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Users/ss/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Run Code Online (Sandbox Code Playgroud)

更新:这篇文章帮助:诊断慢视图渲染的原因

基本上,事实证明延迟是由development.rb中的config.assets.debug = true引起的.我做错了,似乎更快.

Rails的人说启用调试会减慢真正复杂的应用程序,但这只是一个简单的教程应用程序,具有1个模型/控制器/视图.无论如何,我希望这些性能持续增长,但它确实解决了我的直接问题.

Dre*_*awn 42

复制评论(和编辑的问题正文)中的答案,以便从"未答复"过滤器中删除此问题:

我尝试了他们在这篇文章中推荐的内容(诊断慢视图渲染的原因),并且它有效.资产管道已启用,页面加载时间从20-40秒变为1秒.至少要好得多.

...

基本上,事实证明延迟是由development.rb中的config.assets.debug = true引起的.我做错了,似乎更快.

Rails的人说启用调试会减慢真正复杂的应用程序,但这只是一个简单的教程应用程序,具有1个模型/控制器/视图.无论如何,我希望这些性能持续增长,但它确实解决了我的直接问题.

〜每个戴夫鲍曼回答

  • 在将应用程序从Rails 3.2升级到4.2之后,这对我的开发环境没有帮助,但是在development.rb中设置`config.assets.digest = false`就可以了. (8认同)