Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How can I record page byte size and view state

How can I record page byte size and view state

Scheduled Pinned Locked Moved ASP.NET
questiondatabaseperformance
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jim Taylor
    wrote on last edited by
    #1

    I would like to record the size of the response output of a page so that I can record it in a database for performance monitoring and also record the size of viewstate. How can I acheive this? Jim

    M 1 Reply Last reply
    0
    • J Jim Taylor

      I would like to record the size of the response output of a page so that I can record it in a database for performance monitoring and also record the size of viewstate. How can I acheive this? Jim

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi Jim, Because the output stream of the Response object is write-able only, so you'd better implement a custom filter which is able to access the output contents after the request is processed to record the size of the page and the ViewState. The sample code goes like this:

      public class CustomFilter : Stream
      {
      private Stream m_sink;
      private long m_pageSize;
      private long m_viewStateSize;

      public CustomFilter(Stream sink)
      {
      	m\_sink = sink;
      }
                              
      //You have to override some properties and methods
      //...	
                                
      public long PageSize
      {
      	get { return m\_pageSize;}
      }
                     
      public long ViewStateSize
      {
      	get { return m\_viewStateSize;}
      }
                          
      public override void Write(byte\[\] buffer, int offset, int count)
      {
      	m\_pageSize += count;
                       
      	byte\[\] data = new byte\[count\];
      	Buffer.BlockCopy(buffer, offset, data, 0, count);
      	m\_viewStateSize = GetViewStateSize(data);
                      
      	m\_sink.Write(buffer, offset, count);
      } 
                                   
      private long GetViewStateSize(byte\[\] data)
      {
      	//You cannot use the StreamReader with the output stream as it's write-able only.
      	MemoryStream stream = new MemoryStream(data);
      	StreamReader reader = new StreamReader(stream, Encoding.ASCII);
      	string text = reader.ReadToEnd();
      	int num1 = text.IndexOf("\_\_VIEWSTATE");
      	if (num1 > 0)
      	{
      		num1 += 20;
      		int num2 = text.IndexOf("\\"", num1);
      		text = text.Substring(num1, num2 - num1);
      		        
      		//The length of the text is the number of bytes as the Encoding.ASCII 
      		//is used to read data.
      		return text.Length;
      	}
      	            
      	return 0;
      }
      

      }

      + Now, you simply set the custom filter to the Filter property of the Response object in the Global.asax.cs file:

      public class Global : System.Web.HttpApplication
      {
      protected void Application_BeginRequest(Object sender, EventArgs e)
      {
      Response.Filter = new CustomFilter(Response.Filter);
      }

      protected void Application\_EndRequest(Object sender, EventArgs e)
      {
      	CustomFilter filter = Response.Filter as CustomFilter;
      	long pageSize = filter.PageSize;
      	long viewStateSize = filter.ViewStateSize;
                           
      	//Your code here to persist the values in DB.
      }
                  	           
      ...
      

      }

      + In addition, you can also create an http module to re

      J 2 Replies Last reply
      0
      • M minhpc_bk

        Hi Jim, Because the output stream of the Response object is write-able only, so you'd better implement a custom filter which is able to access the output contents after the request is processed to record the size of the page and the ViewState. The sample code goes like this:

        public class CustomFilter : Stream
        {
        private Stream m_sink;
        private long m_pageSize;
        private long m_viewStateSize;

        public CustomFilter(Stream sink)
        {
        	m\_sink = sink;
        }
                                
        //You have to override some properties and methods
        //...	
                                  
        public long PageSize
        {
        	get { return m\_pageSize;}
        }
                       
        public long ViewStateSize
        {
        	get { return m\_viewStateSize;}
        }
                            
        public override void Write(byte\[\] buffer, int offset, int count)
        {
        	m\_pageSize += count;
                         
        	byte\[\] data = new byte\[count\];
        	Buffer.BlockCopy(buffer, offset, data, 0, count);
        	m\_viewStateSize = GetViewStateSize(data);
                        
        	m\_sink.Write(buffer, offset, count);
        } 
                                     
        private long GetViewStateSize(byte\[\] data)
        {
        	//You cannot use the StreamReader with the output stream as it's write-able only.
        	MemoryStream stream = new MemoryStream(data);
        	StreamReader reader = new StreamReader(stream, Encoding.ASCII);
        	string text = reader.ReadToEnd();
        	int num1 = text.IndexOf("\_\_VIEWSTATE");
        	if (num1 > 0)
        	{
        		num1 += 20;
        		int num2 = text.IndexOf("\\"", num1);
        		text = text.Substring(num1, num2 - num1);
        		        
        		//The length of the text is the number of bytes as the Encoding.ASCII 
        		//is used to read data.
        		return text.Length;
        	}
        	            
        	return 0;
        }
        

        }

        + Now, you simply set the custom filter to the Filter property of the Response object in the Global.asax.cs file:

        public class Global : System.Web.HttpApplication
        {
        protected void Application_BeginRequest(Object sender, EventArgs e)
        {
        Response.Filter = new CustomFilter(Response.Filter);
        }

        protected void Application\_EndRequest(Object sender, EventArgs e)
        {
        	CustomFilter filter = Response.Filter as CustomFilter;
        	long pageSize = filter.PageSize;
        	long viewStateSize = filter.ViewStateSize;
                             
        	//Your code here to persist the values in DB.
        }
                    	           
        ...
        

        }

        + In addition, you can also create an http module to re

        J Offline
        J Offline
        Jim Taylor
        wrote on last edited by
        #3

        Thanks, I shall give it a go Jim

        1 Reply Last reply
        0
        • M minhpc_bk

          Hi Jim, Because the output stream of the Response object is write-able only, so you'd better implement a custom filter which is able to access the output contents after the request is processed to record the size of the page and the ViewState. The sample code goes like this:

          public class CustomFilter : Stream
          {
          private Stream m_sink;
          private long m_pageSize;
          private long m_viewStateSize;

          public CustomFilter(Stream sink)
          {
          	m\_sink = sink;
          }
                                  
          //You have to override some properties and methods
          //...	
                                    
          public long PageSize
          {
          	get { return m\_pageSize;}
          }
                         
          public long ViewStateSize
          {
          	get { return m\_viewStateSize;}
          }
                              
          public override void Write(byte\[\] buffer, int offset, int count)
          {
          	m\_pageSize += count;
                           
          	byte\[\] data = new byte\[count\];
          	Buffer.BlockCopy(buffer, offset, data, 0, count);
          	m\_viewStateSize = GetViewStateSize(data);
                          
          	m\_sink.Write(buffer, offset, count);
          } 
                                       
          private long GetViewStateSize(byte\[\] data)
          {
          	//You cannot use the StreamReader with the output stream as it's write-able only.
          	MemoryStream stream = new MemoryStream(data);
          	StreamReader reader = new StreamReader(stream, Encoding.ASCII);
          	string text = reader.ReadToEnd();
          	int num1 = text.IndexOf("\_\_VIEWSTATE");
          	if (num1 > 0)
          	{
          		num1 += 20;
          		int num2 = text.IndexOf("\\"", num1);
          		text = text.Substring(num1, num2 - num1);
          		        
          		//The length of the text is the number of bytes as the Encoding.ASCII 
          		//is used to read data.
          		return text.Length;
          	}
          	            
          	return 0;
          }
          

          }

          + Now, you simply set the custom filter to the Filter property of the Response object in the Global.asax.cs file:

          public class Global : System.Web.HttpApplication
          {
          protected void Application_BeginRequest(Object sender, EventArgs e)
          {
          Response.Filter = new CustomFilter(Response.Filter);
          }

          protected void Application\_EndRequest(Object sender, EventArgs e)
          {
          	CustomFilter filter = Response.Filter as CustomFilter;
          	long pageSize = filter.PageSize;
          	long viewStateSize = filter.ViewStateSize;
                               
          	//Your code here to persist the values in DB.
          }
                      	           
          ...
          

          }

          + In addition, you can also create an http module to re

          J Offline
          J Offline
          Jim Taylor
          wrote on last edited by
          #4

          Worked like a dream, thanks. Jim

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups