How can I record page byte size and view state
-
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
-
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
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 theViewState
. 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 theResponse
object in theGlobal.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
-
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 theViewState
. 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 theResponse
object in theGlobal.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
Thanks, I shall give it a go Jim
-
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 theViewState
. 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 theResponse
object in theGlobal.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
Worked like a dream, thanks. Jim