I am using rails asset pipeline feature in production environment.I have written some setting in nginx to send files in the gzip format and files are coming properly in gzip format.I guess the browser is automatically decoding it so hence i am not able to find out whether the js files are comming in gizp format or not.
我正在生产环境中使用rails资产管道特性。我已经在nginx中编写了一些设置,以以gzip格式发送文件,并且文件以gzip格式正确地出现。我猜浏览器正在自动解码它,因此我无法知道js文件是否以gizp格式混合。
I have put the following command and i am getting "content-encoding: zip" in the response.
我已经输入以下命令,并在响应中获得“内容编码:zip”。
curl -v -H 'Accept-Encoding: gzip' -o /dev/null http://www.example.com/assets/style.css 2>&1 | grep -i Content-Encoding
I have written below setting in nginx to send files in the gzip format and files are coming properly in gzip format.
我已经在下面用nginx写了设置,以gzip格式发送文件,并且文件以gzip格式正确地出现。
location ~ ^/(assets)/ {
root /home/webuser/app/woa/public;
gzip_static on;
expires max;
add_header Cache-Control public;
# access_log /dev/null;
}
How will i come to know that files are coming in gzip format or not??
我怎么知道文件是gzip格式还是不是?
Also please suggest any other options which can be helpful to improve the performance of the site.
也请建议任何其他可能有助于提高网站性能的选项。
1 个解决方案
#1
3
Not a direct answer to your first question(if you solved it please do explain), but for improving site performance remember the Perfomance Golden Rule:
不是对你的第一个问题的直接回答(如果你解决了,请解释),但是为了提高网站性能记住性能黄金法则:
80-90% of the end-user response time is spent on the front-end. Start there.
80% -90%的最终用户响应时间用于前端。从这里开始。
Below is a non-exhaustive list areas of improvement for increasing performance in a Rails app:
以下是一个非详尽的改进Rails应用程序性能的领域:
Diagnosing the Problem:
YSlow / Google Page Speed
A useful diagonsis tool for identifying perfomance issues is Yslow or Google Page Speed They are browser extensions that diagnoses and identifies common issues slowing down your app (particularly on the front end).
识别性能问题的一个有用的对角线工具是Yslow或谷歌页面速度,它们是浏览器扩展,可以诊断和识别减慢应用程序速度的常见问题(尤其是前端)。
Back-end Tools
For your Rails back-end I recommend incorporating tools such as Bullet & NewRelic directly into your development processes, so that while you're developing you can spot bad queries immediately while they are still easy to fix.
对于Rails后端,我建议将Bullet和NewRelic这样的工具直接集成到开发过程中,这样在开发过程中,您就可以在很容易修复的同时立即发现糟糕的查询。
Check Server Console Logs
Checking your server logs is an effective method for diagnosing what components of your Rails app is taking the longest. E.g. below are sample logs from two unrelated production Rails apps running in my local development environment:
检查服务器日志是诊断Rails应用程序哪个组件花费时间最长的有效方法。例如,下面是在我的本地开发环境中运行的两个不相关的产品Rails应用程序的示例日志:
# app1: slowish
Rendered shared/_header.html.erb (125.9ms)
Rendered clients/details.html.erb within layouts/application (804.6ms)
Completed 200 OK in 3655ms (Views: 566.9ms | ActiveRecord: 1236.9ms)
# app2: debilitatingly slow
Rendered search/_livesearch_division_content.js.erb (5390.0ms)
Rendered search/livesearch.js.haml (34156.6ms)
Completed 200 OK in 34173ms (Views: 31229.5ms | ActiveRecord: 2933.4ms)
App1 & App2 both suffer from performance issues, but App2's performance issues are clearly debilitatingly slow. (34 seconds!) But with these server logs, I know for App1 that I should look into clients/details.html.erb
, and that for App2 I absolutely need to investigate search/livesearch.js.haml
.
App1和App2都受到性能问题的困扰,但是App2的性能问题明显是缓慢的。(34秒!)但是有了这些服务器日志,我知道对于App1,我应该查看客户端/details.html。对于App2,我绝对需要调查搜索/生活搜索。
Improving Front-end Performance
Budget your page size strictly
To maintain fast load times you need reduce the amount/size of your page assets (JS/CSS/Images). So think about your page size like a budget. For example, Hootesuite recently declared that their home page now has a strict page-size budget of 1 mb. No exceptions. Now check out their page. Pretty fast isn't it?
为了保持快速加载时间,您需要减少页面资产的数量/大小(JS/CSS/Images)。所以考虑你的页面大小,比如预算。例如,Hootesuite最近声明他们的主页现在有一个1 mb的严格页面大小预算。现在看看他们的页面。很快不是吗?
Easy wins for reducing your page size include stripping out unused JS or CSS files, including them only where needed, and changing static images into much smaller vectors.
减少页面大小的简单方法包括去掉未使用的JS或CSS文件(只在需要的地方包含它们),并将静态图像更改为更小的向量。
Serve smaller image resolutions based on screen width
Image loading is a large cause of slow page loading times. A large 5mb image used in the background of your splash page can easily be brought down to 200kb-400kb in size, and still be high quality enough to be hardly indistinguishable from the higher resolution original. The difference in page load times will be dramatic.
图像加载是导致页面加载速度慢的主要原因。在启动页面的背景中使用的大的5mb图像可以很容易地缩小到200kb-400kb大小,并且仍然是高质量的,很难与高分辨率的原始图像区分开来。页面加载时间的差异将是巨大的。
You should do these same improvements to user uploaded images as well. E.g. if your website's user avatars are 50px by 50px in size, but a user uploads a 5mb image for his avatar, then it's essential that you serve the image with lower file sizes and resolutions to fit exactly how it will be shown on your site.
您还应该对用户上传的图像进行同样的改进。例如,如果你网站的用户头像大小是50px * 50px,但是用户上传了5mb的头像,那么你必须提供文件大小和分辨率更低的图片,以准确地显示在你的网站上。
Carrierwave, Fog, and rmagick are popular gems used with Amazon S3 to achieve better image loading. With that collection of packages you can dynamically serve smaller image resolutions based upon the screen size of each user. You can then use media queries so that mobile device get served smaller resolution sizes of images compared to your users with Retina screens.
Carrierwave、Fog和rmagick是Amazon S3中流行的宝石,用于实现更好的图像加载。有了这些包,您可以根据每个用户的屏幕大小动态地提供更小的图像分辨率。然后你可以使用媒体查询,这样移动设备就可以获得比视网膜屏幕用户更小的图像分辨率。
Use a Content Delivery Network to speed up asset loading
Adding on to the last point, you can speed up asset/image loading times by using a Content Delivery Network (CDN's) such as Cloudfront. CDN's distribute assets across many servers, then serve assets to your users via servers that are located the closest to the user making the request.
添加到最后一点,您可以使用内容交付网络(CDN)(例如Cloudfront)来加快资产/映像加载时间。CDN的跨多个服务器分发资产,然后通过位于离发出请求的用户最近的服务器向用户提供资产。
Fingerprint Static Assets
When static assets are fingerprinted, when a user visits your page their browser will cache a copy of these assets, meaning that they no longer need to be reloaded again for the next request.
当静态资产被指印时,当用户访问您的页面时,他们的浏览器将缓存这些资产的副本,这意味着他们不再需要再次为下一个请求重新加载。
Move Javascript files to the bottom of the page
Javascript files placed at the bottom of the page will load after the page loads. If javascript assets are placed on the top of the page, then the page will remain blank as a user's browser attempts to load your javascript files. Fortunately Rails will automatically place javascript files to the bottom of your page if you use the asset pipeline or specify javascript files using the javascript_include_tag
.
放置在页面底部的Javascript文件将在页面加载后加载。如果将javascript资产放在页面顶部,那么当用户的浏览器试图加载javascript文件时,页面将保持空白。幸运的是,如果您使用资产管道或使用javascript_include_tag指定javascript文件,Rails将自动将javascript文件放在页面底部。
EDIT: Most modern browsers now optimize Javascript loading automatically so you can mostly ignore this advice.
编辑:现在大多数浏览器都自动优化Javascript加载,因此您可以忽略这个建议。
Improving Back-end Performance
Cache, Cache, Cache!
Among all backend performance optimizations, caching is among the most effective for producing dramatic performance gains. A well implemented caching regime can greatly minimize the damage of inefficient queries within your backend during periods of high scalability. Content that is accessed frequently, yet changes relatively infrequently, benefits the most from caching.
在所有后端性能优化中,缓存是产生显著性能收益的最有效方法之一。一个良好的实现的缓存机制可以极大地减少在高可伸缩性期间在您的后端中低效查询的损坏。经常访问的内容,但相对不经常更改的内容,从缓存中获益最大。
Caching is so powerful that it brought down the page load times of App2 mentioned above from 34 seconds to less than a second in production. There is simply no other performance enhancement on the back-end that can come even close to what we got from caching.
缓存功能非常强大,它将上面提到的App2的页面加载时间从34秒降低到不到1秒。在后端没有其他性能增强,甚至可以接近我们从缓存中得到的。
Overall, when doing performance optimization with caching, start high then go low. The gains you will get will be greater for less effort.
总的来说,在使用缓存进行性能优化时,先从高开始,然后再从低开始。你将会得到更大的收获,更少的努力。
From high to low, some types of caching available to you are:
从高到低,您可以使用的一些缓存类型是:
- HTTP caching (does not cache your server, but involves a user's browser caching content locally by reading HTTP headers)
- HTTP缓存(不缓存您的服务器,但通过读取HTTP头,涉及到用户的浏览器缓存内容)
- Page caching (memcache)
- 页面缓存(memcache)
- Action Caching (memcache)
- 动作缓存(memcache)
- Fragment caching (memcache) or Russian doll caching (a favoured technique for caching with fragments)
- 片段缓存(memcache)或Russian doll缓存(使用片段缓存的一种常用技术)
- Model caching (memcache)
- 模型缓存(memcache)
To learn more about caching, a good place to start is here: http://guides.rubyonrails.org/caching_with_rails.html
要了解更多关于缓存的信息,可以从这里开始:http://guides.rubyonrails.org/caching_with_rails.html
Index Everything
If you are using SQL for your database layer, make sure that you specify indexes on join tables for faster lookups on large associations used frequently. You must add them during migrations explicitly since indexing is not included by default in Rails.
如果您正在为数据库层使用SQL,请确保在连接表上指定索引,以便更快地查找经常使用的大型关联。必须在迁移期间显式地添加它们,因为在Rails中默认不包含索引。
N+1 queries
A major performance killer for Rails apps using relational (SQL) databases are N+1 queries. If you see in your logs that your app is making many database read/writes for a single request, then it's often a sign you have N+1 queries. N+1 queries are easy to miss during development but can rapidly cripple your app as your database grows (I once dealt with an that had twelve N+1 queries. After accumulating only ~1000 rows of production data, some pages began taking over a minute to load).
使用关系(SQL)数据库的Rails应用程序的一个主要性能杀手是N+1查询。如果你在你的日志中看到你的应用正在为一个请求做许多的数据库读/写,那么它通常是你有N+1查询的标志。在开发过程中,N+1查询很容易被忽略,但随着数据库的增长,它会迅速影响应用程序(我曾经处理过一个有12个N+1查询的查询)。在只积累了大约1000行生产数据之后,一些页面开始花费一分钟来加载)。
Bullet is a great gem for catching N+1 queries early as you develop your app. A simple method for resolving N+1 queries in your Rails app is to eager load the associated Model where necessary. E.g. Post.all
changes to Post.includes(:comments).all
if you are loading all the comments of each post on the page.
在开发应用程序时,Bullet是一个很好的捕获N+1查询的gem。在Rails应用程序中,一个简单的解决N+1查询的方法就是在必要时加载相关的模型。例如文章。所有更改Post.includes(:评论)。所有,如果你正在加载所有的评论,每个帖子在页面上。
Upgrade to Rails 4 and/or Ruby 2.1.x or higher
The newer version of Rails contains numerous performance improvements that can speed up your app (such as Turbolinks.)
新版本的Rails包含了大量的性能改进,可以加速应用程序(例如Turbolinks)。
Ruby 2.1.x+ contain much better garbage collection over older versions of Ruby. So far reports of people upgrading have found notable performance increases from upgrading.
Ruby 2.1。与旧版本的Ruby相比,x+包含了更好的垃圾收集。到目前为止,有关人员升级的报告发现,升级带来了显著的性能提升。
I am missing many improvements here, but these are a few performance improvements that I can recommend. I will add more when I have time.
我在这里漏掉了许多改进,但是我可以推荐一些性能改进。有时间我再加一些。
#1
3
Not a direct answer to your first question(if you solved it please do explain), but for improving site performance remember the Perfomance Golden Rule:
不是对你的第一个问题的直接回答(如果你解决了,请解释),但是为了提高网站性能记住性能黄金法则:
80-90% of the end-user response time is spent on the front-end. Start there.
80% -90%的最终用户响应时间用于前端。从这里开始。
Below is a non-exhaustive list areas of improvement for increasing performance in a Rails app:
以下是一个非详尽的改进Rails应用程序性能的领域:
Diagnosing the Problem:
YSlow / Google Page Speed
A useful diagonsis tool for identifying perfomance issues is Yslow or Google Page Speed They are browser extensions that diagnoses and identifies common issues slowing down your app (particularly on the front end).
识别性能问题的一个有用的对角线工具是Yslow或谷歌页面速度,它们是浏览器扩展,可以诊断和识别减慢应用程序速度的常见问题(尤其是前端)。
Back-end Tools
For your Rails back-end I recommend incorporating tools such as Bullet & NewRelic directly into your development processes, so that while you're developing you can spot bad queries immediately while they are still easy to fix.
对于Rails后端,我建议将Bullet和NewRelic这样的工具直接集成到开发过程中,这样在开发过程中,您就可以在很容易修复的同时立即发现糟糕的查询。
Check Server Console Logs
Checking your server logs is an effective method for diagnosing what components of your Rails app is taking the longest. E.g. below are sample logs from two unrelated production Rails apps running in my local development environment:
检查服务器日志是诊断Rails应用程序哪个组件花费时间最长的有效方法。例如,下面是在我的本地开发环境中运行的两个不相关的产品Rails应用程序的示例日志:
# app1: slowish
Rendered shared/_header.html.erb (125.9ms)
Rendered clients/details.html.erb within layouts/application (804.6ms)
Completed 200 OK in 3655ms (Views: 566.9ms | ActiveRecord: 1236.9ms)
# app2: debilitatingly slow
Rendered search/_livesearch_division_content.js.erb (5390.0ms)
Rendered search/livesearch.js.haml (34156.6ms)
Completed 200 OK in 34173ms (Views: 31229.5ms | ActiveRecord: 2933.4ms)
App1 & App2 both suffer from performance issues, but App2's performance issues are clearly debilitatingly slow. (34 seconds!) But with these server logs, I know for App1 that I should look into clients/details.html.erb
, and that for App2 I absolutely need to investigate search/livesearch.js.haml
.
App1和App2都受到性能问题的困扰,但是App2的性能问题明显是缓慢的。(34秒!)但是有了这些服务器日志,我知道对于App1,我应该查看客户端/details.html。对于App2,我绝对需要调查搜索/生活搜索。
Improving Front-end Performance
Budget your page size strictly
To maintain fast load times you need reduce the amount/size of your page assets (JS/CSS/Images). So think about your page size like a budget. For example, Hootesuite recently declared that their home page now has a strict page-size budget of 1 mb. No exceptions. Now check out their page. Pretty fast isn't it?
为了保持快速加载时间,您需要减少页面资产的数量/大小(JS/CSS/Images)。所以考虑你的页面大小,比如预算。例如,Hootesuite最近声明他们的主页现在有一个1 mb的严格页面大小预算。现在看看他们的页面。很快不是吗?
Easy wins for reducing your page size include stripping out unused JS or CSS files, including them only where needed, and changing static images into much smaller vectors.
减少页面大小的简单方法包括去掉未使用的JS或CSS文件(只在需要的地方包含它们),并将静态图像更改为更小的向量。
Serve smaller image resolutions based on screen width
Image loading is a large cause of slow page loading times. A large 5mb image used in the background of your splash page can easily be brought down to 200kb-400kb in size, and still be high quality enough to be hardly indistinguishable from the higher resolution original. The difference in page load times will be dramatic.
图像加载是导致页面加载速度慢的主要原因。在启动页面的背景中使用的大的5mb图像可以很容易地缩小到200kb-400kb大小,并且仍然是高质量的,很难与高分辨率的原始图像区分开来。页面加载时间的差异将是巨大的。
You should do these same improvements to user uploaded images as well. E.g. if your website's user avatars are 50px by 50px in size, but a user uploads a 5mb image for his avatar, then it's essential that you serve the image with lower file sizes and resolutions to fit exactly how it will be shown on your site.
您还应该对用户上传的图像进行同样的改进。例如,如果你网站的用户头像大小是50px * 50px,但是用户上传了5mb的头像,那么你必须提供文件大小和分辨率更低的图片,以准确地显示在你的网站上。
Carrierwave, Fog, and rmagick are popular gems used with Amazon S3 to achieve better image loading. With that collection of packages you can dynamically serve smaller image resolutions based upon the screen size of each user. You can then use media queries so that mobile device get served smaller resolution sizes of images compared to your users with Retina screens.
Carrierwave、Fog和rmagick是Amazon S3中流行的宝石,用于实现更好的图像加载。有了这些包,您可以根据每个用户的屏幕大小动态地提供更小的图像分辨率。然后你可以使用媒体查询,这样移动设备就可以获得比视网膜屏幕用户更小的图像分辨率。
Use a Content Delivery Network to speed up asset loading
Adding on to the last point, you can speed up asset/image loading times by using a Content Delivery Network (CDN's) such as Cloudfront. CDN's distribute assets across many servers, then serve assets to your users via servers that are located the closest to the user making the request.
添加到最后一点,您可以使用内容交付网络(CDN)(例如Cloudfront)来加快资产/映像加载时间。CDN的跨多个服务器分发资产,然后通过位于离发出请求的用户最近的服务器向用户提供资产。
Fingerprint Static Assets
When static assets are fingerprinted, when a user visits your page their browser will cache a copy of these assets, meaning that they no longer need to be reloaded again for the next request.
当静态资产被指印时,当用户访问您的页面时,他们的浏览器将缓存这些资产的副本,这意味着他们不再需要再次为下一个请求重新加载。
Move Javascript files to the bottom of the page
Javascript files placed at the bottom of the page will load after the page loads. If javascript assets are placed on the top of the page, then the page will remain blank as a user's browser attempts to load your javascript files. Fortunately Rails will automatically place javascript files to the bottom of your page if you use the asset pipeline or specify javascript files using the javascript_include_tag
.
放置在页面底部的Javascript文件将在页面加载后加载。如果将javascript资产放在页面顶部,那么当用户的浏览器试图加载javascript文件时,页面将保持空白。幸运的是,如果您使用资产管道或使用javascript_include_tag指定javascript文件,Rails将自动将javascript文件放在页面底部。
EDIT: Most modern browsers now optimize Javascript loading automatically so you can mostly ignore this advice.
编辑:现在大多数浏览器都自动优化Javascript加载,因此您可以忽略这个建议。
Improving Back-end Performance
Cache, Cache, Cache!
Among all backend performance optimizations, caching is among the most effective for producing dramatic performance gains. A well implemented caching regime can greatly minimize the damage of inefficient queries within your backend during periods of high scalability. Content that is accessed frequently, yet changes relatively infrequently, benefits the most from caching.
在所有后端性能优化中,缓存是产生显著性能收益的最有效方法之一。一个良好的实现的缓存机制可以极大地减少在高可伸缩性期间在您的后端中低效查询的损坏。经常访问的内容,但相对不经常更改的内容,从缓存中获益最大。
Caching is so powerful that it brought down the page load times of App2 mentioned above from 34 seconds to less than a second in production. There is simply no other performance enhancement on the back-end that can come even close to what we got from caching.
缓存功能非常强大,它将上面提到的App2的页面加载时间从34秒降低到不到1秒。在后端没有其他性能增强,甚至可以接近我们从缓存中得到的。
Overall, when doing performance optimization with caching, start high then go low. The gains you will get will be greater for less effort.
总的来说,在使用缓存进行性能优化时,先从高开始,然后再从低开始。你将会得到更大的收获,更少的努力。
From high to low, some types of caching available to you are:
从高到低,您可以使用的一些缓存类型是:
- HTTP caching (does not cache your server, but involves a user's browser caching content locally by reading HTTP headers)
- HTTP缓存(不缓存您的服务器,但通过读取HTTP头,涉及到用户的浏览器缓存内容)
- Page caching (memcache)
- 页面缓存(memcache)
- Action Caching (memcache)
- 动作缓存(memcache)
- Fragment caching (memcache) or Russian doll caching (a favoured technique for caching with fragments)
- 片段缓存(memcache)或Russian doll缓存(使用片段缓存的一种常用技术)
- Model caching (memcache)
- 模型缓存(memcache)
To learn more about caching, a good place to start is here: http://guides.rubyonrails.org/caching_with_rails.html
要了解更多关于缓存的信息,可以从这里开始:http://guides.rubyonrails.org/caching_with_rails.html
Index Everything
If you are using SQL for your database layer, make sure that you specify indexes on join tables for faster lookups on large associations used frequently. You must add them during migrations explicitly since indexing is not included by default in Rails.
如果您正在为数据库层使用SQL,请确保在连接表上指定索引,以便更快地查找经常使用的大型关联。必须在迁移期间显式地添加它们,因为在Rails中默认不包含索引。
N+1 queries
A major performance killer for Rails apps using relational (SQL) databases are N+1 queries. If you see in your logs that your app is making many database read/writes for a single request, then it's often a sign you have N+1 queries. N+1 queries are easy to miss during development but can rapidly cripple your app as your database grows (I once dealt with an that had twelve N+1 queries. After accumulating only ~1000 rows of production data, some pages began taking over a minute to load).
使用关系(SQL)数据库的Rails应用程序的一个主要性能杀手是N+1查询。如果你在你的日志中看到你的应用正在为一个请求做许多的数据库读/写,那么它通常是你有N+1查询的标志。在开发过程中,N+1查询很容易被忽略,但随着数据库的增长,它会迅速影响应用程序(我曾经处理过一个有12个N+1查询的查询)。在只积累了大约1000行生产数据之后,一些页面开始花费一分钟来加载)。
Bullet is a great gem for catching N+1 queries early as you develop your app. A simple method for resolving N+1 queries in your Rails app is to eager load the associated Model where necessary. E.g. Post.all
changes to Post.includes(:comments).all
if you are loading all the comments of each post on the page.
在开发应用程序时,Bullet是一个很好的捕获N+1查询的gem。在Rails应用程序中,一个简单的解决N+1查询的方法就是在必要时加载相关的模型。例如文章。所有更改Post.includes(:评论)。所有,如果你正在加载所有的评论,每个帖子在页面上。
Upgrade to Rails 4 and/or Ruby 2.1.x or higher
The newer version of Rails contains numerous performance improvements that can speed up your app (such as Turbolinks.)
新版本的Rails包含了大量的性能改进,可以加速应用程序(例如Turbolinks)。
Ruby 2.1.x+ contain much better garbage collection over older versions of Ruby. So far reports of people upgrading have found notable performance increases from upgrading.
Ruby 2.1。与旧版本的Ruby相比,x+包含了更好的垃圾收集。到目前为止,有关人员升级的报告发现,升级带来了显著的性能提升。
I am missing many improvements here, but these are a few performance improvements that I can recommend. I will add more when I have time.
我在这里漏掉了许多改进,但是我可以推荐一些性能改进。有时间我再加一些。