所以我使用的是mini-profiler,它给了我一些不错的统计数据。
然而,我注意到的一件事是我已经将很多SQL调用降到最低,现在最重要的是渲染各种部分和HTML。
例如,以下是我面临的两个不同的问题示例:
迷你概述
GET http://localhost:3000/ 14.0 +0.0
Executing action: index 9.5 +9.0
Rendering: home/index 7.8 +16.0
Rendering: home/_row 7.9 +22.0
Rendering: layouts/application 1038.7 +32.0
Rendering: layouts/_navigation 6.0 +586.0
Development.log
Started GET "/" for 127.0.0.1 at 2013-05-17 18:00:26 -0500
Processing by HomeController#index as HTML
Item Load (0.6ms) SELECT "items".* FROM "items" WHERE "items"."is_approved" = 't'
ActsAsTaggableOn::Tag Load (0.6ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = 13 AND "taggings"."taggable_type" = 'Item' AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)
Rendered home/_row.html.erb (8.0ms)
Rendered home/index.html.erb within layouts/application (15.7ms)
Rendered layouts/_social_media.html.erb (0.5ms)
(0.4ms) SELECT items.id FROM "items"
ActsAsTaggableOn::Tag Load (0.7ms) SELECT tags.*, taggings.tags_count AS count FROM "tags" JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM "taggings" INNER JOIN items ON items.id = taggings.taggable_id WHERE (taggings.taggable_type = 'Item' AND taggings.context = 'tags') AND (taggings.taggable_id IN(13)) GROUP BY taggings.tag_id HAVING COUNT(taggings.tag_id) > 0) AS taggings ON taggings.tag_id = tags.id ORDER BY count LIMIT 5
Rendered layouts/_navigation.html.erb (6.1ms)
Rendered layouts/_site_nav.html.erb (0.6ms)
Rendered layouts/_messages.html.erb (0.2ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 1069ms (Views: 1066.0ms | ActiveRecord: 2.3ms)
Mini-Profiler顶部的输出显示主application.html.erb
显着增加了加载时间。
以下是另一个应用程序的示例,其中视图是花费最多时间呈现的视图:
Started GET "/" for 127.0.0.1 at 2013-05-17 17:55:01 -0500
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Processing by HomeController#index as HTML
Category Load (0.2ms) SELECT "categories".* FROM "categories" LIMIT 6
Banner Load (0.2ms) SELECT "banners".* FROM "banners" INNER JOIN "banner_types" ON "banner_types"."id" = "banners"."banner_type_id" WHERE (banner_types.name = 'Featured')
Banner Load (0.2ms) SELECT "banners".* FROM "banners" INNER JOIN "banner_types" ON "banner_types"."id" = "banners"."banner_type_id" WHERE (banner_types.name = 'Side')
Product Load (0.3ms) SELECT "products".* FROM "products"
Vendor Load (0.3ms) SELECT "vendors".* FROM "vendors" WHERE "vendors"."id" IN (12, 11, 10)
Vendor Load (0.2ms) SELECT "vendors".* FROM "vendors"
User Load (0.2ms) SELECT "users".* FROM "users"
Rendered home/_popular_products.html.erb (16.1ms)
Rendered home/_popular_stores.html.erb (2.4ms)
Rendered home/index.html.erb within layouts/application (26.4ms)
Piggybak::Sellable Load (0.2ms) SELECT "sellables".* FROM "sellables" WHERE "sellables"."id" = 1 LIMIT 1
Rendered layouts/_login_nav.html.erb (8.2ms)
Rendered layouts/_navigation.html.erb (0.8ms)
CACHE (0.0ms) SELECT "vendors".* FROM "vendors"
Rendered layouts/_store_dropdown.html.erb (2.2ms)
Rendered layouts/_header.html.erb (18.1ms)
Rendered layouts/_messages.html.erb (0.3ms)
Rendered layouts/_footer.html.erb (0.9ms)
Completed 200 OK in 242ms (Views: 209.8ms | ActiveRecord: 1.9ms)
当然,这个特定时间只是209.8ms
,但在不同的加载时间内它一直高到5,000ms
。
如何优化这些视图的渲染?谐音?或者我可以使用什么工具来至少弄清楚导致长时间加载的原因,以便我可以慢慢地消除它?
答案 0 :(得分:1)
您正在开发中对此进行分析。请注意,由于config.cache_classes设置,您的应用程序将重新加载该环境中的每个请求。将该设置设置为true的分析可能会给您不同(通常更快)的结果。
在您的一条评论中,您提到了一个Vendor.all.each
代码段。我会检查实际需要多长时间。只需将其打包在results = Benchmark.measure do
块中,然后查看results.real
为您提供的内容。假设它在50毫秒内完成。
您有三种选择,通常是:
如果50ms不能令人满意,请从以特定间隔重新生成的静态文件中读取部分渲染结果。这可以通过使用rails缓存机制或将其放入使用cronjob重新生成的文件中来完成。无论哪种方式,这都涉及到可能在间隔之间创建的新供应商的某种延迟。
或者,如果50ms适合你,你可以将它们转换为JSON(这很快,看看https://github.com/ohler55/oj)并按照之前的建议,用AJAX拉它并渲染html JavaScript的。这具有以下优点:html的实际呈现在浏览器内完成。浏览器做得更快,通常负载较少。
如果您对50毫秒不满意并且您无法忍受页面上稍微过时的信息,则必须开始执行选项2但不使用ActiveRecord,因此直接使用mysql ruby适配器。或者,您确保无论何时创建新供应商或更新旧供应商,您都可以将JSON添加到相应供应商的文本列中,也可以使用一些after_validation回调。这样你至少可以做到
vendors = Vendor.select(:json_cache).map{|v| v.json_cache}.join(',')
vendors = "[#{vendors}]"
render :json => vendors
......不是很漂亮但很快。
我希望,这是你想知道的。
答案 1 :(得分:0)
在您的第二个示例中,我看到了以下查询:
如果你的部分必须遍历大量的对象,那么它将大大减慢渲染时间。
注意调用Vendor.all.each do |v|
之类的集合的迭代器。永远不应在视图中调用模型类方法。在所需对象的控制器中构建集合,然后在实例变量中发送到视图。