ASP. NET site performance improvement-compression

Source: Internet
Author: User
Document directory
  • Store compressed files in the output Cache
  • If the client does not accept the compressed memory
Compression negotiation

How does the server know that the browser can receive compressed content? How does the browser know that the received content is compressed?

When a compressed browser sends a request to the server, it contains an Accept-Encoding request header, telling the server the compression algorithm it supports. For example:

Accept-Encoding: gzip,deflate

If the server response uses compression, the uncompressed file header contains a response header Content-Encoding, indicating how the file is compressed:

Content-Encoding: gzip

However, not only does the browser and server send and receive requests and responses, but also the proxy. In addition, the proxy can cache the response and then respond to subsequent requests from its cache. How can we determine whether the proxy will not send the compressed file to a browser that cannot process the compressed file.

IIS6 and II7 use the same Accept-Encoding header as the original request to notify the proxy to respond to the request from the cache. When compression is enabled, IIS6 and IIS7 include the Vary header in the response:

Vary: Accept-Encoding

IIS6 also allows the File Cache-Control and Expires headers to be reloaded using the attributes in the metadata. This allows you to disable the proxy cache of compressed files. IIS metadata is configured in IIS6 to update the metadata subsection of the compression section. You can find the metadata attributes of the Cache-Control and Expires headers in the following places:

  • HcCacheControlHeader http://msdn.microsoft.com/en-us/library/ms525179 (vs.90). aspx
In IIS7, configure compression and install the dynamic content compression module.

To Compress dynamic files, first install the dynamic content compression module. The installation steps for Vista, Windows 7, or Windows Server 2008 are somewhat different:

  1. Click Start | Administrative Tools | Server Manager.
  2. Expand Roles and click Web Server (IIS ).
  3. Scroll to Role Services and click Add Role Services to open the Add Role Services wizard.
  4. On the Select Role Services page, scroll to Performance, Select Dynamic Content Compression, and Select next.

On Vista or Windows 7:

  1. Click Start | Control Panel | Programs | Turn Windows features on or off. The Windows Features dialog box opens.
  2. Expand Internet Information Service, expand World Wide Web Services, and then expand Performance Features. Select Http Compression Dynamic.
Enable Compression
  1. Open Internet Information Services (IIS) Manager.
  2. Select a machine and double-click the Compression icon on the right.
  3. The compression window opens. The following items are displayed:
    • Enable dynamic content compression: unless the server has used a lot of CPU, it starts dynamic content compression.
    • Enable static content compression: the compressed static content is compressed. Therefore, only the initial compression will occupy the CPU cycle.
    • Only compress files larger than (in bytes): compressing a small file can make it larger.
    • Cache directory: The storage location of static files. You can change the location. Make sure that the drive is a local drive or NTFS partition and is not compressed or shared.
    • Per application pool disk space limit (in MB): if there are many application pools and the disk space is limited, You can adjust them. If there are 100 application pools, the value of this option is 100 MB, and the cached static files will use 100*100 MB = 10 GB.
Set compression for sites, folders, or files
  1. Open the IIS manager and click the site, folder, or file on the left to modify the compression status.
  2. Switch the space in the middle to Features View and double-click the Compression icon.
  3. You can enable or disable dynamic or static File compression in the window that is opened.
Compression grade

The compression level is too high. The larger the compression ratio, the larger the CPU usage.

The compression levels of static and dynamic files can be set separately. For static files, use 9, the highest level. For more information about dynamic File compression level 4, see:

    IIS 7 Compression. Good? Bad? How much? Http://weblogs.asp.net/owscott/archive/2009/02/22/iis-7-compression-good-bad-how-much.aspx.

However, different websites have different optimal compression levels, depending on idle CPU capacity, page file compression rate, and bandwidth costs. Select the optimal compression level for the experiment.

Set the compression level:

  1. Run the following command from the command line:
    C:\Windows\System32\Inetsrv\Appcmd.exe    set config -section:httpCompression    -[name='gzip'].staticCompressionLevel:9   -[name='gzip'].dynamicCompressionLevel:4
  2. Restart the IIS server.
Disable Compression Based on CPU utilization

To ensure that compression does not overload the CPU, IIS7 calculates the average CPU usage every 30 seconds. When the CPU usage exceeds a limit, compression is automatically disabled. When the CPU usage is lower than the limit, compression is enabled again.

The default value is:

  Disable Compression Restart Compression
Dynamic files 90% 50%
Static files 100% 50%

Note that this means that if the CPU on the server is always above 50%, but occasionally higher than 90%, dynamic File compression will be disabled, but it will not be enabled again.

You can modify these restrictions by modifying the applicationHost. config file, which is usually in the C: \ Windows \ System32 \ inetsrv \ config folder.

  1. Find the
  2. Modify the httpdynamicCompressionEnableCpuUsage attribute:
  3. Restart IIS.

To modify other restrictions, modify the attributes:

  Disable Compression Restart Compression
Dynamic files DynamicCompressionDisableCpuUsage DynamicCompressionEnableCpuUsage
Static files StaticCompressionDisableCpuUsage StaticCompressionEnableCpuUsage

If you want IIS to disable Compression Based on CPU utilization, set these attribute values to 100.

Elements and attributes used with httpCompression:

  • HTTP Compression Http://www.iis.net/ConfigReference/system.webServer/httpCompression.
Set the static File Access frequency threshold

IIS7 compresses and caches static files only when they are frequently requested. If the request frequency is not high, IIS7 does not compress static files, which can save CPU and cache space.

By default, a file is considered to be frequently requested only when it is requested twice or more times every 10 seconds. This is determined by the two attributes of the serverRuntime element of web. config:

ServerRuntime attribute Description
FrequentHitThreshold Number of times a URL must be requested within the time span specified in the frequentHitTimePeriod attribute to be considered frequently hit. Must be between 1 and 2147483647. Default is 2.
FrequentHitTimePeriod Time interval in which a URL must be requested the number of times specified in the frequentHitThreshold attribute before it is considered to be frequently hit. Default is 10 seconds.

For example, if a static file is requested 7 times every 15 seconds and needs to be cached, use:

<configuration>...  <system.webServer>    <serverRuntime frequentHitThreshold="7"       frequentHitTimePeriod="00:00:15" />  </system.webServer>...</configuration>
Cache compressed dynamic files

IIS7 only caches compressed versions of static files, while dynamic files are compressed at each request. This means that compressing dynamic files consumes more CPU than static files.

It makes sense that dynamic files are different from each other. However, if dynamic pages are the same for all visitors, it makes sense to cache their compressed versions.

In this case, you can use the ASP. NET OutputCache command. The problem with this method is that IIS stores the uncompressed version of files in the output cache by default. For each request, IIS must compress the cached content before sending it to the browser. This is very inefficient.

Store compressed files in the output Cache

The compressed version of the IIS cache file, rather than the uncompressed version. In this way, you do not need to compress the file during each request to reduce CPU usage.

Because ASP. NET output cache is used, you need to add the OutputCache command to the page:

<%@ OutputCache Duration="300" VaryByParam="none" %>

Then, modify the applicationHost. config file, which is usually in the C: \ Windows \ System32 \ inetsrv \ config folder:

  1. Find the <urlCompression> section.
  2. Add the dynamicCompressionBeforeCache = "true" attribute to the urlCompression element.
  3. Restart IIS.
If the client does not accept the compressed memory

We cache the compressed content. What happens if a visitor accesses the website using a browser that does not support compressed content?

When IIS receives a request that does not compress the content, it discards the Compressed Content in the cache, generates the content again, and caches the uncompress content. It uses uncompressed content until the cache expires, even if the client can accept the compressed content.

This problem can be solved through both cache compression and uncompressed versions. Add VaryByContentEncoding to the OutputCache command:

<%@ OutputCache Duration="300" VaryByParam="none" VaryByContentEncoding="gzip;deflate" %>

One disadvantage of VaryByContentEncoding in the OutputCache command is that this will disable the kernel cache of this file.

Configure compression in IIS6

Omitted

Increase page Compression

If the server uses compression, it makes sense to optimize the compression of text files. The compression algorithm has the best effect on repeated content:

  • Always specify HTML attributes in the same order. One method is to use advanced web controls and custom server controls to generate HTML. Do not use low-level HTML server controls. This will slightly increase the CPU usage, but will ensure consistency, for example, using
    <Asp: Hyperlink runat = "server" ......>
    Replace
    <A runat = "server"...>
  • Similarly, attributes are written alphabetically in the CSS selector.
  • Use the same case. Use lowercase letters for HMTL labels and attributes.
  • Use consistent brackets: Do not mix "...." And '.... '.
More resources
  • 10 Tips for Writing High-Performance Web Applications
    Http://msdn.microsoft.com/en-us/magazine/cc163854.aspx
  • Confguring HTTP Compression in IIS 7
    Http://technet.microsoft.com/en-us/library/cc771003 (WS.10). aspx
  • IIS 6.0 Technical Reference
    Http://technet.microsoft.com/en-us/library/cc775635 (WS.10). aspx
  • IIS Compression in IIS6.0
    Http://weblogs.asp.net/owscott/archive/2004/01/12/57916.aspx
  • Everything you ever wanted to know about compression, but were afraid to ask
    Http://geekswithblogs.net/JamesFleming/archive/2010/02/04/everything-you-ever-wanted-to-know-about-compression-but-were.aspx
  • Measuring the Performance Effects of Dynamic Compression in IIS 7.0
    Http://www.webperformanceinc.com/library/reports/iis7_compression/
  • Let's make the web faster
    Http://code.google.com/speed/articles/use-compression.html
  • Page Speed tool
    Http://code.google.com/speed/page-speed/download.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.