nQuant Reduces The Visual Studio Gallery and MSDN Code Samples page size down by 10%

http://www.mattwrock.com/post/2011/09/30/nQuant-Reduces-The-Visual-Studio-Gallery-and-MSDN-Code-Samples-page-size-down-by-10.aspx (30. September 2011 09:21 by Matt Wrock)

Today the Microsoft Galleries team where I work and which supports the Visual Studio extensions gallery and the MSDN Code Samples gallery, among many others, began quantizing its sprited images with nQuant and has realized a 10% reduction in page size.


A few months ago, the visual studio gallery and the MSDN code samples gallery adopted my OSS project RequestReduce, which merges and minifies CSS as well as automatically sprites and optimizes background images. As I reported then, we experienced a 20% improvement in global page load times. At that time RequestReduce performed lossless PNG compression which can dramatically reduce the size of the sprited images which RequestReduce generates. I had also played with some “lossy” open source command line utilities that further reduced the size of PNG images – sometimes dramatically and often without perceptible quality loss. However, when I integrated these utilities intoRequestReduce and applied it to some of the galleries that the Microsoft Galeries team develops (most notably the Silverlight and Azure galleries), the lossy optimization quality was simply unacceptable.

I did quite a bit of research on the topic of image quantization which is the process of removing colors from an image to produce a much smaller image while utilizing sophisticated algorithms to make this color loss imperceptible (or nearly imperceptible) to the human eye. It quite possibly may even be just as effective on alien eyes but to date, we lack the empirical evidence. You can count on me to update this post as more data accumulates in that exciting area of study.

While investigating this, I came across an algorithm developed by Xiaolin Wu that appeared to optimize RGB images (without transparency) with a quality unmatched by any other algorithm I had experimented with. Unfortunately, the algorithm was not immediately compatible with the transparent PNGs generated by RequestReduce. After several weeks of tinkering during very odd hours, I managed to adapt the algorithm to convert 32 bit transparent PNGs to 8 bit 256 color PNGs with far superior quality than those produced by many popular C command line tools. Furthermore, this is a C# library that can be easily integrated into any other .net assembly, nQuant also provides a command line wrapper which can be used for build tasks or ad hoc optimizations.

If you would like to see how nQuant can optimize images, head on over to nquant.codeplex.com where you can download either the compiled assembly and command line utility or the full source code. The site also provides complete instructions on the propper use of the nQuant API. It is dead simple. Here is an example of how to quantize a single image from within C#:

var quantizer = new WuQuantizer();
using(var bitmap = new Bitmap(sourcePath)){
	using(var quantized = quantizer.QuantizeImage(bitmap, alphaTransparency, alphaFader)){
		quantized.Save(targetPath, ImageFormat.Png);

Using the command line, you would issue a command like this:

nQuant myimage.png /o mynewimage.png

If you would like to not only optimize your images but also minify and merge your CSS as well as sprite your CSS background images into a single sprite, then check out RequestReduce. Unlike other similar optimization tools, you do not need to change your code or rearrange your folder structure and you do not need to supply a wealth of config options to get started. It works with multiple server environments and supports content on a CDN. RequestReduce also includes nQuant which it uses to reduce the size of the images it produces.

For more details on the algorithm included in the nQuant library and my efforts to adapt it to ARGB transparent images, read my post on this subject.


Making ASP.NET Output Cache Work with Post-Back

Making ASP.NET Output Cache Work with Post-Back.

ASP.NET Output Cache & Post-Back

ASP.NET’s page caching and user control fragment caching prevents post-back from working because, during post-back, a cached version of the page may be displayed instead of the form submission being processed. However, all is not lost! I’ve found a simple solution to this using a HTTP module to bypass the output cache during post-back. This allows the initial page view to be cached to disk, but – and here’s the good bit – user interaction via post-back remains un-cached, so you get the benefits of post-back while losing none of the goodness of output caching!

Continue reading

Event Broker for ASP.NET

Event Broker for ASP.NET.

Event Broker for ASP.NET

The Event Broker is an implementation of the Observer pattern. The key benefit of an Event Broker is that it offers a loosely coupled mechanism to broadcast and receive events. This is especially useful in ASP.NET where you want to raise events in one user control, say, and handle them in another without needing to wire up each user control to each other explicitly.

I first came across the Event Broker when developing a Windows Forms application using the Smart Client Application Block and thought it was a really cool idea. I have been using the Event Broker concept ever since in various guises, and it’s especially useful in ASP.NET when you want communication between controls to be loosely coupled.

I thought I’d build a really simple implementation that can be used with Web Forms or MVC and allows you to broadcast and receive named events.

For instance let’s say you’re using Web Forms and you have a user control that has a button and you’d like to notify other user controls on a page that the button was clicked without introducing any coupling between the controls or the page they are sitting on. Using the Event Broker this is really easy to do.

My Event Broker implementation provides a way to broadcast and receive loosely coupled events. Behind the scenes the Event Broker uses the HttpContext.Items to keep track of its information. This means that the loosely coupled events are scoped to each request so events and their handlers inherently know which requests they’re for.

Continue reading

tSQLt – Database Unit Testing for SQL Server

What is tSQLt?

tSQLt is a database unit testing framework for Microsoft SQL Server. tSQLt is compatible with SQL Server 2005 (service pack 2 required) and above on all editions.

Main Features

tSQLt allows you to implement unit tests in T-SQL. This is important as you do not have to switch between various tools to create your code and your unit tests. tSQLt also provides the following features to make it easier to create and manage unit tests:

  • Tests are automatically run within transactions – this keeps tests independent and reduces any cleanup work you need
  • Tests can be grouped together within a schema – allowing you to organize your tests and use common setup methods
  • Output can be generated in plain text or XML – making it easier to integrate with a continuous integration tool
  • Provides the ability to fake tables and views, and to create stored procedure spies – allowing you to isolate the code which you are testing

Practical Performance Profiling: Improving the Efficiency of .NET Code

In Practical Performance Profiling, Jean-Philippe Gouigoux provides both the theory and practical skills necessary to analyze and improve the performance of .NET code. By focusing on the 20% of code defects that account for 80% of performance loss, Jean-Philippe Gouigoux guides the reader through using a profiler and explains how to identify and correct performance bottlenecks. This book is for all developers who wish to boost the performance of their .NET code without having to embark on the steep slopes of tuning.

Continue reading

InfoQ: T-SQL Improvements in SQL Server 2012

InfoQ: T-SQL Improvements in SQL Server 2012.

T-SQL received a lot of improvements in SQL Server 2012 including support for the ANSI FIRST_VALUE and LAST_VALUE functions, declarative data paging using FETCH and OFFSET, and .NET’s parsing and formatting functions.

Fetch and Offset

Currently SQL Server developers wishing to implement server-side paging tend to use imperative techniques such as loading a result set into a temp table, numbering the rows, and then selecting out the range they were actually interested in. Others use the more modern ROW_NUMBER and OVER pattern, while some stick to cursors. None of these techniques are not hard, but they can be time consuming and error prone. Moreover they are inconsistent, as each developer has their own favorite implementation.

SQL Server 2012 addresses this by adding true declarative support for data paging. T do this, developers have been given the ability to add an OFFSET and FETCH NEXT option to T-SQL’s ORDER BY clause. Currently these are not performance optimizations, SQL Server is still doing what you would have done manually. But as Dr. Greg Low says in his presentation, the people writing the query optimizer are in a much better position to improve things when they know what you are trying to achieve rather than how you are trying to achieve it.

Continue reading