渲染更多部分会降低 Rails Web 应用程序的性能吗?

Posted

技术标签:

【中文标题】渲染更多部分会降低 Rails Web 应用程序的性能吗?【英文标题】:Is rendering more partials reduce performance in Rails web application? 【发布时间】:2017-04-19 09:31:16 【问题描述】:

我已经创建了 Rails(版本 - 5.0.0.1)应用程序。我使用了部分来减少重复的代码。我已经完成了该功能,在查看特定请求的控制台日志后,至少呈现了 100 个部分,并且可能达到 200 个。这会影响应用程序性能吗?

以下是处理请求时的控制台日志。

Started GET “/******/98747259/edit" for 127.0.0.1 at 2017-04-19 14:51:50 +0530
Processing by *********Controller#edit as html
  Parameters: 
  Rendering triggers/edit.html.erb within layouts/application
  Rendered layouts/_navigation_menu.html.erb (18.8ms)
  Rendered layouts/_flash_errors.html.erb (1.5ms)
  Rendered triggers/trigger/_campaign.html.erb (5.2ms)
  Rendered triggers/trigger/_campaign.html.erb (2.3ms)
  Rendered triggers/trigger/_campaign.html.erb (2.1ms)
  Rendered triggers/trigger/_campaign.html.erb (1.7ms)
  Rendered triggers/trigger/_campaign.html.erb (2.0ms)
  Rendered triggers/trigger/_campaign.html.erb (2.3ms)
  Rendered triggers/trigger/_campaign.html.erb (1.8ms)
  Rendered triggers/trigger/_campaign.html.erb (94.5ms)
  Rendered triggers/trigger/_campaign.html.erb (2.2ms)
  Rendered triggers/trigger/_campaign.html.erb (2.5ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (1.5ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.8ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.8ms)
  Rendered triggers/actions/_tag.html.erb (15.9ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.7ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.8ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.8ms)
  Rendered triggers/actions/_tag.html.erb (9.2ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (2.4ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (1.8ms)
  Rendered triggers/actions/_campaign.html.erb (103.3ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (2.0ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (1.7ms)
  Rendered triggers/actions/_campaign.html.erb (7.7ms)
  Rendered triggers/actions/_add_multiple_campaigns_elements.html.erb (8.3ms)
  Rendered triggers/actions/_add_multiple_campaigns_elements.html.erb (76.3ms)
  Rendered triggers/actions/_multiple_campaigns.html.erb (91.8ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (1.7ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (3.0ms)
  Rendered triggers/actions/_campaign.html.erb (8.7ms)
  Rendered triggers/actions/_add_email_elements.html.erb (6.8ms)
  Rendered triggers/actions/_add_email_elements.html.erb (2.1ms)
  Rendered triggers/actions/_add_email_elements.html.erb (2.1ms)
  Rendered triggers/actions/_email.html.erb (18.4ms)
  Rendered triggers/actions/_add_custom_field_elements.html.erb (1.2ms)
  Rendered triggers/actions/_custom_field.html.erb (4.3ms)
  Rendered triggers/actions/_delete_subscriber.html.erb (1.2ms)
  Rendered triggers/actions/_add_unsubscribe_elements.html.erb (1.4ms)
  Rendered triggers/actions/_add_unsubscribe_elements.html.erb (0.6ms)
  Rendered triggers/actions/_unsubscribe.html.erb (12.4ms)
  Rendered triggers/actions/_modal_for_mail.html.erb (1.0ms)
  Rendered triggers/_add_new_action.html.erb (1.4ms)
  Rendered triggers/actions/_unsubscribe_from_campaign.html.erb (3.2ms)
  Rendered triggers/perform_actions/_apply_or_remove_tags.html.erb (1.0ms)
  Rendered triggers/perform_actions/_campaign.html.erb (0.9ms)
  Rendered triggers/perform_actions/_move_from_one_campaign_to_another.html.erb (1.4ms)
  Rendered triggers/perform_actions/_send_manual_mail.html.erb (2.5ms)
  Rendered triggers/perform_actions/_set_custom_field.html.erb (0.9ms)
  Rendered triggers/perform_actions/_record_an_event.html.erb (5.1ms)
  Rendered layouts/_flash_errors.html.erb (0.6ms)
  Rendered triggers/perform_actions/_unsubscribe.html.erb (1.4ms)
  Rendered triggers/perform_actions/_send_notification_mail.html.erb (5.7ms)
  Rendered triggers/trigger_details/_campaign.html.erb (2.2ms)
  Rendered triggers/trigger_details/_apply_or_remove_tags.html.erb (1.2ms)
  Rendered triggers/trigger_details/_form.html.erb (4.3ms)
  Rendered triggers/trigger_details/_mail.html.erb (4.4ms)
  Rendered triggers/trigger_details/_clicked_link.html.erb (1.0ms)
  Rendered triggers/trigger_details/_visited_page.html.erb (0.9ms)
  Rendered triggers/trigger_details/_update_custom_field.html.erb (0.8ms)
  Rendered triggers/trigger/_tag.html.erb (1.0ms)
  Rendered triggers/trigger/_campaign.html.erb (1.7ms)
  Rendered triggers/trigger/_form.html.erb (2.1ms)
  Rendered triggers/trigger/_link.html.erb (0.9ms)
  Rendered triggers/trigger/_page.html.erb (0.8ms)
  Rendered triggers/trigger/_email.html.erb (2.8ms)
  Rendered triggers/trigger/_custom_field.html.erb (1.1ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.6ms)
  Rendered triggers/actions/_tag.html.erb (2.7ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (1.7ms)
  Rendered triggers/actions/_campaign.html.erb (3.6ms)
  Rendered triggers/actions/_add_multiple_campaigns_elements.html.erb (2.1ms)
  Rendered triggers/actions/_multiple_campaigns.html.erb (4.1ms)
  Rendered triggers/actions/_add_email_elements.html.erb (28.6ms)
  Rendered triggers/actions/_email.html.erb (30.7ms)
  Rendered triggers/actions/_add_custom_field_elements.html.erb (0.8ms)
  Rendered triggers/actions/_custom_field.html.erb (3.2ms)
  Rendered triggers/actions/_add_unsubscribe_elements.html.erb (0.7ms)
  Rendered triggers/actions/_unsubscribe.html.erb (2.7ms)
  Rendered triggers/actions/_notification_email.html.erb (0.9ms)
  Rendered triggers/actions/_delete_subscriber.html.erb (0.5ms)
  Rendered triggers/actions/_add_tag_elements.html.erb (0.6ms)
  Rendered triggers/actions/_add_campaign_elements.html.erb (1.7ms)
  Rendered triggers/actions/_add_multiple_campaigns_elements.html.erb (2.0ms)
  Rendered triggers/actions/_add_email_elements.html.erb (2.4ms)
  Rendered triggers/actions/_add_custom_field_elements.html.erb (0.8ms)
  Rendered triggers/actions/_add_unsubscribe_elements.html.erb (0.7ms)
  Rendered triggers/actions/_notification_email.html.erb (0.4ms)
  Rendered triggers/_form.html.erb (804.3ms)
  Rendered triggers/edit.html.erb within layouts/application (825.7ms)
Completed 200 OK in 1610ms (Views: 1562.3ms | ActiveRecord: 16.1ms)

【问题讨论】:

【参考方案1】:

它可能会减慢您的应用程序,因为渲染一个部分需要一些时间,它可能会非常少,但它确实需要一些时间,因为渲染一个部分调用一个渲染方法,所以有第二个方法调用。完全取决于您,您希望应用程序的性能如何,因为它确实减少了重复代码并增强了代码可读性

【讨论】:

以上是关于渲染更多部分会降低 Rails Web 应用程序的性能吗?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 AWS OpsWork 部署 2 个或更多 Rails Web 应用程序?

使用 Rails 助手渲染部分

通过渲染 Rails 部分创建 jquery html 对象

如何在 Rails ERB 文件的渲染部分 url 中访问 java 脚本变量?

来自 application.html.erb 的 Rails 渲染实例变量

Rails - 为部分渲染布局