Pages

Wednesday, September 12, 2012

Even the small bytes matter


Great post by Cinchcast engineer Enrique Alegretta about how we implemented a framework to minify HTML and inline Javascripts on our Blogtalkradio webpages.
This is cross-referenced from our Tech blog http://tech.cinchcast.com 

Even the small bytes matter
Here at Cinchcast we found ourselves thinking about different ways we could reduce ourbandwidth usage. Since BlogTalkRadio has aconsiderable user base, we use a lotof bandwidth in our efforts to provide an enjoyable user experience.
One of the techniques we use to facilitate faster page loads is enabling gzip, this is a common practice and will save lots of bytes simply by compressing the response output. Since we want to continually evolve and have a great infrastructure we continued to look for more ways to improve response times and page load speed. One particular technique we are using (and the main reason of this blog post) is removing whitespace and minifying inline css and inline javascript (we’re currently minifying external css and js files thanks to the excellent YUI compressor).
Since we’re developers at heart anddon’t like to reinvent the wheel unless we plan on learning more about wheelswe asked ourselves: is there anything out there already that we could use or at least useas a base for our implementation? The answer was yes and was located at this codeplex project.

Enter WebOptimizer

WebOptimizer.NET is a set of http modules you can add to your website for whitespace removal, inline javascript minification and inline css minification among other things. It’s a great project but since its open source what we did instead of using it as is was to take the pieces we wanted and build our own solution (because we like to learn more about wheels XD). This effort lead to the birth of the Cinchcast Framework, a common set of tools and functionality which will be shared among the Cinchcast and Blog Talk Radio applications.

The Cinchcast Web Framework

We ended up digging through WebOptimizer and took the pieces we needed and performed some changes to it. One key class we took and changed was theBaseFilterStream class. This is an abstract class that provides the base functionality to create FilterStreams which will be assigned to the Response.Filter property in order to properly modify the response. One of the changes we did was to modify its constructor in order to have the following:
/// 
/// Initializes a new instance of the  class.
/// 
/// The context.
protected BaseFilterStream(HttpContextBase context)
{
      if (context == null)
            throw new ArgumentNullException("context", "The context cannot be null");

      HttpContext = context;
      Sink = context.Response.Filter;
}

/// 
/// Initializes a new instance of the  class.
/// 
/// The context.
/// The previous stream.
protected BaseFilterStream(HttpContextBase context, Stream previousStream)
{
      HttpContext = context;
      Sink = previousStream;
}

/// 
/// Gets the HTTP context.
/// 
/// 
/// The HTTP context.
/// 
protected HttpContextBase HttpContext
{
      get;  
      private set;
}
The reasons for doing this are two fold:
  1. We wanted to have the HttpContext available to all the classes which inherit from this class, we’re using HttpContextBase abstraction in order to be able to mock it during our unit tests.
  2. We wanted to provide stream chainability to perform several chained actions over the Response output. This is why one constructor has a reference to the previous stream. Every time the Write method is called, all the actions are performed and a call to Sink.Write is made, by using the previousStream what we’re doing is just calling another filter stream Write method which will perform its glorious magic.
We did reuse the RemoveWhiteSpaceFilterStream, MinifyInlineStylesFilterStream and MinifyInlineJavascriptFilterStream, but instead of using the tools for minification the WebOptimizer provided, we used Microsoft’s AjaxMinLib for CSS minification and the JSCompressor class from this codeproject article.

Cleaning up the response

We took our base foundation classes to get a Response.Filter, twisted it to make it show what we want, then wrapped this up into an HttpModulewe named…. yeah, you guess it, CleanHttpResponseModule or CHRM (pronounced charm).
Adding this module to the website automatically performs whitespace removal, inline javascriptminification and inline cssminification, but since we need to debug stuff and if you spent too much time debugging something that is minified you’ll end up as Crazy Dave from PvZ we gave the module the ability to deactivate any of those features. You can change this behavior on a page by page basis (using the queryString) or to the entire application using the appSettings section.

Conclusion and results

Using this module to its full potential (minifies js, css and removing whitespaces) allows us to save something between 2KB to 5KB per request, but since our requests are inthe millions take that value and multiply it and in the end the gains will be significant.
This is why even the small bytes matter.

Reactions:

2 comments:

Your fake breitling uk enjoy features analog along with digital camera occasion screen which has a core a few moments side. Digital camera omega replica sale screen is populate matrix variety supplies your enjoy some other search via other designer watches. It might demonstrate some time throughout 40 urban centers along with 30 distinct occasion areas, so that it is the cheap breitling replica watch out for entire world people. Your lustrous finish for the arms plus the call help it become all to easy to explain to your tag heuer replica uk occasion perhaps after dark using only one small contact with a new source of light. Environmentally friendly travel attribute inside Resident Promaster Aqualand Environmentally friendly Travel Titanium Enjoy can make it virtually preservation no cost for the life span. This specific attribute implies your cartier replica enjoy doesn't need a new battery pack – your enjoy takes in the via just about any offered source of light that could very last for a long time.

The use of the water in the water OMEGA has extraordinary charm. Launched in 1932, Omega Ocean series, called the first piece of replica watches diving in the world; dive to 300 meters water depth is still in working order mechanical diving table; table on a cosmic ocean, diving performance is more perfect, styles but also the significant marine, sport, fashion temperament. On the land of OMEGA, it is a time accurate and design excellence and known. Launched in 1994, the world the first piece of fake hublot watches Omega mid Tourbillon watches marked the birth of the first true mid Tourbillon. In order to complete in 16.8 mm narrow to display their skills, Omega mid Tourbillon bidirectional driving Tourbillon system design and production are by hand made. The world's track and field events, the Olympic Games and other sports events are quite popular with OMEGA's top of rolex replica the table. Aerospace OMEGA also dominate in the watch industry in. The "Super Series" by the US and UK in two for the air force designated watch requisition in dior replica the two world war. Omega Speedmaster professional chronograph. In 1965 become American astronaut specified timepiece, became a Soviet cosmonaut specified timepiece in 1975. It was the first one, and the only one that was on the moon.

Post a Comment