Friday, July 17, 2009

ASP.NET Performance Improvement Techniques(GZIP)

Now a days lot of web applications have many performance issues. We can easily come out these by implementing some techniques. One of them GZip compression is very important.


The beauty of this new GZip support is how easy it is to use in your own ASP.NET code. For example the the cached RSS feed is GZip encoded which uses GZipStream on the Response.OutputStream fed to an XmlWriter():



Response.ContentType = "text/xml";



Stream Output = Response.OutputStream;



if (Westwind.Tools.wwWebUtils.IsGZipSupported())

{

GZipStream gzip = new GZipStream(Response.OutputStream, CompressionMode.Compress);

Response.AppendHeader("Content-Encoding", "gzip");

Output = gzip;

}



Encoding Utf8 = new UTF8Encoding(false); // No BOM!

XmlTextWriter Writer = new XmlTextWriter(Output,Utf8);



Writer.Formatting = Formatting.Indented;



Writer.WriteStartElement("rss");







Writer.WriteEndElement(); // rss



Writer.Close();

Response.End();





GZipStream is a stream acts like a stream filter so you can assign it to an existing stream like ResponseStream can intercept all inbound data then write the updated data into the actual Response stream. It’s super easy to do.



You should always check however if GZip is enabled which is done by this helper method IsGZipSupported() which looks like this:



///

/// Determines if GZip is supported

///


///

public static bool IsGZipSupported()

{

string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

if (!string.IsNullOrEmpty(AcceptEncoding) &&

AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate") )

return true;

return false;

}



This ensures that you don’t encode content when the client doesn’t understand GZip and would be unable to read the encoded GZip content. Note that the code checks for either gzip or deflate so this assumes before encoding you’ll pick the right encoding algorithm.

GZip in Page Content

Speaking of encoding - above was XML and raw ResponseStream encoding, but you can also apply GZip content very, very easily to your page level or any ASP.NET level code (such as in an HTTP handler). In fact, you can use a very generic mechanism to encode any output by using a Response.Filter which the following helper method (also in wwWebUtils) demonstrates:



///
/// Sets up the current page or handler to use GZip through a Response.Filter
/// IMPORTANT:
/// You have to call this method before any output is generated!
///

public static void GZipEncodePage()
{
HttpResponse Response = HttpContext.Current.Response;

if (IsGZipSupported())
{
string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (AcceptEncoding.Contains("deflate"))
{
Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "deflate");
}
else
{
Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
System.IO.Compression.CompressionMode.Compress);
Response.AppendHeader("Content-Encoding", "gzip");
}
}

// Allow proxy servers to cache encoded and unencoded versions separately
Response.AppendHeader("Vary", "Content-Encoding");
}



You can now take this helper function and use this in page level code. For example, the main page in my blog which is HUGE (frequently around 500k) uses it like this:



protected void Page_Load(object sender, EventArgs e)

{

wwWebUtils.GZipEncodePage();



Entry = WebLogFactory.GetEntry();



if (Entry.GetLastEntries(App.Configuration.ShowEntryCount, "pk,Title,Body,Entered,Feedback,Location") < 0)

throw new ApplicationException("Couldn't load WebLog Entries: " + Entry.ErrorMessage);



this.repEntries.DataSource = this.Entry.EntryList;

this.repEntries.DataBind();

}



That’s it. One line and the page will now conditionally GZip encode if the client supports it.



If you really wanted to you can take this even one step further and create a module that automatically sets the Response.Filter early in the pipeline and based on that automatically compresses all content.



However, remember GZip encoding applies compression on the fly so there’s some overhead in the GZip encoding – you are basically adding more CPU processing to your content to reduce the output size. If your content is not that large to start with there’s probably not that much sense in compressing in the first place so I don’t think that whole-sale compression of dynamic content is a good idea.


Caching

But one thing that can mitigate the overhead of GZip compression is caching.



both the RSS feed and the home page are usually heavily cached – the content doesn’t change all that frequently so there’s no reason to keep re-generating it right? If you use GZip on your content you have to be careful to cache both the GZipped content and the non-encoded content or else you’ll feed garbage to clients that don’t understand GZip.



So for example the default page has:



<%@ OutputCache Duration="60" VaryByParam="none" VaryByCustom="GZIP" %>



And then a custom Global.asax handler that looks like this:



public override string GetVaryByCustomString(HttpContext context, string custom)

{

if (custom == "GZIP")

{

if (Westwind.Tools.wwWebUtils.IsGZipSupported())

return "GZip";

return "";

}



return base.GetVaryByCustomString(context, custom);

}



Which results in possibly two different versions of the GZipped page being cached.



And there you have it - GZipped content is easy to create now in ASP.NET 2.0/.NET 2.0 and if judiciously applied it can save some significant bandwidth. On my homepage which is close to 500k of blog text content the GZipped size is 55k or so – nearly a 90% reduction in size. I’d say that’s plenty of worth it, especially when caching is added.

Delicious
Bookmark this on Delicious

Monday, July 6, 2009

Bind data to dropdownlist from client side javascript

var ddlFromWarehouseClientId = document.getElementById("ctl00_ContentPlaceHolder1_grdStockTransfer_ctl01_ddlFromWarehouse");

ddlFromWarehouseClientId.innerHTML = "";
var res1 = (AjaxFunctions.GetStockTranseferData()).value;
ddlFromWarehouseClientId.options.add(new Option("Select", "0", false, false));
var i = 0;
if (res1.Rows.length > 0) {
//CId.options.length = 0;
for (i = 0; i <= res1.Rows.length - 1; i++) {

ddlFromWarehouseClientId.options.add(new Option(res1.Rows[i].Name, res1.Rows[i].Id, false, false));
}
}



Name and Id are database field name

How to call ajax function from client side see my earlier post
http://rajendramalav.blogspot.com/2009/07/call-server-side-method-from-client

Call server side method from client script through Ajax

1.Create a class under App_Code folder

public class AjaxFunctions
{

[Ajax.AjaxMethod]
public bool IsNumber(int i)
{
if(i==1)
return true;
else
return false;
}
}


2.Register this class on page_load event of page

Ajax.Utility.RegisterTypeForAjax(typeof(AjaxFunctions));


3. Java script function
function OnChangeFromOwner() {
var res1 = (AjaxFunctions.IsNumber()).value;
}