Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 10th, 2007, 07:03 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default Optimisation TIP: Viewstate

hi all,

found a nice little article explaining the benefits of overrriding the LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium methods to allow for compression of the viewstate. i've jigged it around and managed to get it to work globally across my beerhouse test site. basically, i only had to amend two classes in the App_Code folder (all additions in red if you prefer to just paste those sections in):

Globals.cs
BasePage.cs

here is the Globals.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;

namespace MB.TheBeerHouse
{
    public static class Globals
    {
        // compression for viewstate (or any html perhaps)
        public static byte[] Compress(byte[] data)
        {
            MemoryStream output = new MemoryStream();
            GZipStream gzip = new GZipStream(output,
                             CompressionMode.Compress, true);
            gzip.Write(data, 0, data.Length);
            gzip.Close();
            return output.ToArray();
        }

        public static byte[] Decompress(byte[] data)
        {
            MemoryStream input = new MemoryStream();
            input.Write(data, 0, data.Length);
            input.Position = 0;
            GZipStream gzip = new GZipStream(input,
                             CompressionMode.Decompress, true);
            MemoryStream output = new MemoryStream();
            byte[] buff = new byte[64];
            int read = -1;
            read = gzip.Read(buff, 0, buff.Length);
            while (read > 0)
            {
                output.Write(buff, 0, read);
                read = gzip.Read(buff, 0, buff.Length);
            }
            gzip.Close();
            return output.ToArray();
        } // end viewstate compression

        public readonly static TheBeerHouseSection Settings = (TheBeerHouseSection)WebConfigurationManager.GetSe ction("TheBeerHouse");
        public static string ThemesSelectorID = "";

        static Globals()
        {

        }
    }
}


and here is the BasePage.cs (i've only included the new entries here as it was rather a long class in my app - again actual entries in red)

using System.IO;

namespace MB.TheBeerHouse.UI
{
    public class BasePage : System.Web.UI.Page
    {
        // added to compress viewstate also in globals
        protected override object LoadPageStateFromPersistenceMedium()
        {
            string viewState = Request.Form["__VSTATE"];
            byte[] bytes = Convert.FromBase64String(viewState);
            bytes = Globals.Decompress(bytes);
            LosFormatter formatter = new LosFormatter();
            return formatter.Deserialize(Convert.ToBase64String(bytes ));
        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            LosFormatter formatter = new LosFormatter();
            StringWriter writer = new StringWriter();
            formatter.Serialize(writer, viewState);
            string viewStateString = writer.ToString();
            byte[] bytes = Convert.FromBase64String(viewStateString);
            bytes = Globals.Compress(bytes);
            ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
        } // end viewstate additions




Hope it works as great for you as it is for me, average savings of 30-60%, which means a good speedy page load and less bandwidth useage!!

enjoy

[edit] as an aside, the bigger the viewstate, the bigger the savings. i just ran a few pages that had a viewstate size of 25k. this reduced to 9k with compression - so far so good!!



jimi

http://www.jamestollan.com
__________________
jimi

http://www.originaltalent.com
 
Old May 11th, 2007, 04:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I like this idea a lot! By leveraging the base page you've made it possible to affect many pages with a small amount of code.

I suggest using a configuration switch to let you easily enable/disable compression. That lets you compare your results to see which setting gives the fastest page loads.

With shared hosting, I'm not sure if your portion of the web server CPU is high enough to show an overall benefit in load times. But it may very well be useful even in this case.

Of course, if you think most of your users have medium or slow speed connections, then compression is almost always a good idea. With high speed connections and/or shared web servers, the benefit is less predictable.

But this is a first class elegant idea in any event!

Eric

 
Old May 12th, 2007, 07:31 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

yes, the BasePage is your friend :)

i like the idea too and will look at the config switch idea as a generality anyway as it may be beneficial to be able switch on/off at will.

i'll keep you updated re any other thoughts in this area.

jimi

http://www.jamestollan.com
 
Old May 13th, 2007, 09:18 PM
Authorized User
 
Join Date: Mar 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good work :)


Sorry
 
Old May 14th, 2007, 08:20 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

mary,

i can only take credit for the implementation of the 'work'. the actual code was derived from two seperate sources and kinda worked around by me into a single unified 'idea', the remit being to fit the beerhouse model and to be as trim and elegant as i could muster.

i'm quite pleased that it met the criteria without being in any way 'bloated' or esoteric.

next thing on my list - async threads!! [D]

jimi

http://www.jamestollan.com
 
Old May 14th, 2007, 08:45 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

as an aside. the other thought i had with the viewstate was a combination of what i've done along with a server side storage of the viewstate. so basically, the client page would be issed with a GUID for the __vstate attibute. the contents of compressed viewstate would be stored in a table in the database and accessed via the GUID key. of course, this would have then required housekeeping routines/expiry etc and i wasn't sure if the viewstate transmission was actually such a huge deal (certainly not when compressed) versus the additional server side processing.

but certainly worth thinking about. i'll leave the 'reader' to implement the server side functionality based on the LoadPageStateFromPersistenceMedium and SavePageStateToPersistenceMedium overrides to achieve the above.

jimi

http://www.jamestollan.com
 
Old May 15th, 2007, 09:57 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

caveat:

if you set viewstate properties in the client app, then this won't work unless you compress the saved viewstate variable in the same way as the basepage.cs class. this is due to the fact that the the viewstate on the page is always a compressed version, if you add cleint side viewstate vars, then those will be added to the compressed page _vstate as uncompressed data, thereby creating a bit of a shambles.

just something to be aware of - only discovered this as i was working on a project that uses ajax and persists the 'page #' between calls using the viewstate.

jimi

http://www.jamestollan.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Engine Optimisation mat41 Classic ASP Professional 2 November 27th, 2007 06:08 PM
Tool Tip MunishBhatia ASP.NET 2.0 Professional 2 May 11th, 2007 05:39 PM
Query Optimisation artidatanova SQL Server 2000 1 April 4th, 2005 08:42 PM
Search Engine Optimisation mekh HTML Code Clinic 1 February 25th, 2005 04:54 AM
CPU Usage Optimisation avbabu VS.NET 2002/2003 1 January 21st, 2005 07:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.