为什么资产管道在Rails 3.1中减慢了localhost的速度

时间:2012-01-15 15:59:38

标签: ruby-on-rails-3.1 asset-pipeline

我一直注意到Rails 3.1中资产管道的页面加载时间非常慢。每个资产(css / js / image)需要半秒到一秒才能加载。这真的开始阻碍发展。我的生产中没有同样的问题。

有什么解决方案吗?下面的日志文件显示了每个单独资产的加载速度为0.5 - 0.3秒:

# load of page itself...
...
Completed 200 OK in 1231ms (Views: 933.0ms | ActiveRecord: 15.6ms)


Started GET "/assets/blueprint/screen.css?body=1" for 127.0.0.1 at 2012-01-15 15:53:00 +0000
Served asset /blueprint/screen.css - 304 Not Modified (0ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-01-15 15:53:00 +0000
Served asset /application.css - 304 Not Modified (0ms)


Started GET "/assets/blueprint/print.css?body=1" for 127.0.0.1 at 2012-01-15 15:53:00 +0000
Served asset /blueprint/print.css - 304 Not Modified (0ms)


Started GET "/assets/universal/application.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:01 +0000
Served asset /universal/application.js - 304 Not Modified (2ms)


Started GET "/assets/universal/animate-color.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:01 +0000
Served asset /universal/animate-color.js - 304 Not Modified (0ms)


Started GET "/assets/universal/animate-shadow.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:02 +0000
Served asset /universal/animate-shadow.js - 304 Not Modified (0ms)


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:02 +0000
Served asset /jquery_ujs.js - 304 Not Modified (0ms)


Started GET "/assets/universal/jquery.placehold.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:02 +0000
Served asset /universal/jquery.placehold.js - 304 Not Modified (0ms)


Started GET "/assets/universal/autoresize.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:03 +0000
Served asset /universal/autoresize.js - 304 Not Modified (0ms)


Started GET "/assets/universal/utilities.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:03 +0000
Served asset /universal/utilities.js - 304 Not Modified (0ms)


Started GET "/assets/universal/validators.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:03 +0000
Served asset /universal/validators.js - 304 Not Modified (0ms)


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-01-15 15:53:04 +0000
Served asset /jquery.js - 304 Not Modified (0ms)


Started GET "/assets/whitelabel/pingpanel/pingpanel.css?body=1" for 127.0.0.1 at 2012-01-15 15:53:04 +0000
Served asset /whitelabel/pingpanel/pingpanel.css - 304 Not Modified (0ms)


Started GET "/assets/logo.png" for 127.0.0.1 at 2012-01-15 15:53:05 +0000
Served asset /logo.png - 304 Not Modified (0ms)


Started GET "/assets/application_bar_background.png" for 127.0.0.1 at 2012-01-15 15:53:05 +0000
Served asset /application_bar_background.png - 304 Not Modified (0ms)


Started GET "/assets/linen-new.jpg" for 127.0.0.1 at 2012-01-15 15:53:06 +0000
Served asset /linen-new.jpg - 304 Not Modified (0ms)

在Chrome网络分析仪中可以更清楚地看到减速的影响:

enter image description here

2 个答案:

答案 0 :(得分:3)

无论何时加载资产,都必须通过Rails环境传递,Rails将重新加载所有代码。如果您拥有大量资产,则会导致页面加载速度变慢。

尝试使用rails-dev-tweaks gem,它可让您更好地控制Rails何时重新加载代码。应该解决你的减速问题。

答案 1 :(得分:3)

使用rails-dev-tweak可能会获得有限的改进,因为Sprockets不使用自动加载器。链轮作为传统路线安装。

转到您的development.rb文件并设置:

config.assets.debug = false

这会将dev模式更改为仅提供一个文件而不是多个文件,这样可以加快速度。

如果您需要进行一些源代码调试,添加debug = 1作为URL参数将告诉Sprockets分割文件以获取该请求。