.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda LINQ Parse Path Process Property Regex Replace Sort Split Static StringBuilder Substring Switch Tuple

C#: .NET: Compression

GZIP compression over HTTP reduces page size in ASP.NET. We want to look at all the factors involved. Factors such as dynamic pages and output caching need to be considered. We test server-side compression using GZIP in ASP.NET.

Website performance chart

Intro. Here I want to use some online tools to first see how long the page takes to transfer over the wire. Often DNS lookups and other overhead can dwarf the time spent downloading the file. Is the file size truly important?

Google search engine

PageSpeed Insights. One newer tool released by Google is called PageSpeed Insights.
This is currently,
as of October 2012,
my preferred page performance test. It does not require a special browser installation.

And:It provides many helpful suggestions—including the "GZIP" one. It is regularly updated.

PageSpeed InsightsProgramming tip

Web Page Test. The site is located at webpagetest.org, and currently it uses the IE7 browser. Next I show the site layout and how you can use it to perform a similar test, as of September 2008.


Tip:This AOL open-source tool lets us try our page on a dialup or DSL modem in Virginia, USA. I chose to simulate the DSL connection.

Framework: NET

Compression. I developed a test console program that measures how long it takes my PC using .NET to compress a 20 KB mostly random string using GZipStream. My results showed that the process took about 2.2 milliseconds per iteration.

GZipStreamPercentage symbol

You could use DEFLATE instead of GZIP for a server-side performance boost. Tests found deflate performs 41% faster on an empty byte array. But my code indicated deflate was less than 5% faster on a randomized 20 KB string.

Next:This code can compare GZipStream and DeflateStream. A better benchmark could be used on the web pages you want to compress.

Class that uses DeflateStream: C#

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

class CompressionTest
    public static void PerformanceTest()
	// Get random bytes
	byte[] buffer = RandomBytes();

	long t1 = Environment.TickCount;
	using (MemoryStream stream = new MemoryStream())
	    for (int i = 0; i < 1000; i++)
		// A
		// GZipStream comp = new GZipStream(stream, CompressionMode.Compress);
		// B
		DeflateStream comp = new DeflateStream(stream, CompressionMode.Compress);
		comp.Write(buffer, 0, buffer.Length);
	long t2 = Environment.TickCount;
	Console.WriteLine(t2 - t1);

    private static byte[] RandomBytes()
	// Simple method to return a somewhat random string
	StringBuilder builder = new StringBuilder();
	while (builder.Length < 20000)
	return new ASCIIEncoding().GetBytes(builder.ToString());

GZIP compression

Enable GZIP. If you are working for a large organization, you will want to enable IIS7 compression on the server. But many of us doing smaller-scale work won't have precise control over the server.

I learned on Rick Strahl's web log this interesting and useful technique for enabling compression directly in ASP.NET on your ASPX pages or code-behind files. You can use Response.Filter and GZipStream for ASP.NET compression.

More on GZip compression with ASP.NET
Page that uses Response.Filter: C#

using System;
using System.Web.UI;
using System.IO.Compression;

public partial class _Default : Page
    protected override void OnLoad(EventArgs e)
	// Apply GZIP compression (or DEFLATE)
	Response.Filter = new GZipStream(Response.Filter,
	Response.AddHeader("Content-Encoding", "gzip");

Squares: abstract

Results. The experiment shows that the time required for downloading a page, not including DNS connections, is larger than the time required for compressing that page. The computer I tested the compression on is likely faster than the server.

When to compress. On many mostly-static sites that have minimal dynamic features, I like to combine output caching and compression. This reduces the CPU load of compressing but also reduces bandwidth.

But:For a dynamic site with large pages, the cost of compression could go up ten times.

Larger pages. A site with 200 KB pages might take ten times more time to compress. However, the server also requires CPU to send files and maintain connections. I expect the benefits of compression would be smaller for these sites.

ASPNET web programming framework

Summary. We looked at some issues relating to HTTP compression in the ASP.NET web framework. Resources from the ASP.NET community helped this analysis. Online tools such as PageSpeed Insights can give a valuable benchmark to your efforts.

Update:This page was last updated in 2014. The external resources change with time.