有类似的线程关于Rails在开发模式下速度较慢,但是那些线程中的解决方案对我没有任何帮助。我已经尝试安装可以提高性能的gem并且玩弄配置文件,但是都没有成功。
我刚开始学习Rails,所以我正在运行“Getting Started with Rails”指南中的启动应用程序,这是一个小博客。我已经按照建议安装了Ruby 1.9.3和Rails 3.2.13。我正在运行OS / X 10.7.5。
当加载教程应用程序的起始页面时,它需要20-40秒钟,这只包含1行文本和1个链接。每个后续请求到任何页面都需要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
您可以看到,初始的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,这些警告第一次出现。但是,dev模式下的Rails仍然很慢。
谢谢, Dave
更新: 为了帮助缩小问题范围,我禁用了资产管道,它确实加快了速度。现在只需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'
更新: 这篇文章对我帮助很大:Diagnosing the cause of slow view rendering
基本上,问题的原因是development.rb文件中config.assets.debug = true导致的延迟。我将其设置为false,看起来速度更快了。
Rails开发人员表示,启用debug模式会减慢复杂应用程序的运行速度,但这只是一个简单的教程应用程序,仅有1个模型/控制器/视图。无论如何,我希望这些性能提升能持续下去,但至少解决了我眼前的问题。
application.rb
中添加config.assets.enabled = false
并检查响应。通常浏览器在加载<head>
中的所有脚本/ CSS之前不会呈现页面,因此这可能是您未看到页面的原因。 - Subhas