More On Compressing Asp Page and performances
3 days ago i've got a phone call from some project support. And i asked him how about the Bandwith test? did it passed?. Well he said that Compare to the last one, the size is Smaller 40%.
How is it possible?
1.Enable the Gzip content, which is already support by most browser and the feature is already exists since web 1.1
How to do this? there are many ways,westwind way, And i followed the omar way again, Enabling the IIS6 GZip compression by changing the metabase , though i already implemented it, but i haven't take a look the response content using tool. but it supposed to run , i already take a look at the Windows\temporaryiisfiles everything has a GZIP_what_.css or js or html,but to see the asp page is on the response content equals gzip .
2.I'm using the ViewState Compressor
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5:
6: /// <summary>
7: /// Summary description for Compressor
8: /// </summary>
9: using System.IO;
10: using System.IO.Compression;
11:
12: public static class Compressor
13: {
14:
15: public static byte[] Compress(byte[] data)
16: {
17: MemoryStream output = new MemoryStream();
18: GZipStream gzip = new GZipStream(output,
19: CompressionMode.Compress, true);
20: gzip.Write(data, 0, data.Length);
21: gzip.Close();
22: return output.ToArray();
23: }
24:
25: public static byte[] Decompress(byte[] data)
26: {
27: MemoryStream input = new MemoryStream();
28: input.Write(data, 0, data.Length);
29: input.Position = 0;
30: GZipStream gzip = new GZipStream(input,
31: CompressionMode.Decompress, true);
32: MemoryStream output = new MemoryStream();
33: byte[] buff = new byte[64];
34: int read = -1;
35: read = gzip.Read(buff, 0, buff.Length);
36: while (read > 0)
37: {
38: output.Write(buff, 0, read);
39: read = gzip.Read(buff, 0, buff.Length);
40: }
41: gzip.Close();
42: return output.ToArray();
43: }
44: }
45:
And i moved it to the BasePage
1: using System;
2: using System.IO;
3: using System.Web;
4: using System.Web.UI;
5:
6: /// <summary>
7: /// Summary description for BasePage
8: /// </summary>
9: public class BasePage : Page
10: {
11: /// <summary>
12: /// Give the Page Title
13: /// </summary>
14: /// <param name="e"></param>
15: protected override void OnLoadComplete(EventArgs e)
16: {
17: if (string.IsNullOrEmpty(Page.Title) || Page.Title == "Untitled Page")
18: {
19: //ambil nama dari halaman
20: string fileName = Path.GetFileNameWithoutExtension(Request.PhysicalPath);
21: Page.Title = fileName;
22: }
23: //var anevent = new PipeLineEvent(HttpContext.Current.User.Identity.Name + " Visit This Page", this);
24: //anevent.Raise();
25: base.OnLoadComplete(e);
26: }
27: /// <summary>
28: /// Handles on Load ViewState Zip from Page
29: /// </summary>
30: /// <param name="viewState"></param>
31: protected override object LoadPageStateFromPersistenceMedium()
32: {
33: string viewState = Request.Form["__VSTATE"];
34: byte[] bytes = Convert.FromBase64String(viewState);
35: bytes = Compressor.Decompress(bytes);
36: LosFormatter formatter = new LosFormatter();
37: return formatter.Deserialize(Convert.ToBase64String(bytes));
38:
39: }
40: /// <summary>
41: /// Handles on Save ViewState Zip to Page
42: /// </summary>
43: /// <param name="viewState"></param>
44: protected override void SavePageStateToPersistenceMedium(object viewState)
45: {
46: LosFormatter formatter = new LosFormatter();
47: StringWriter writer = new StringWriter();
48: formatter.Serialize(writer, viewState);
49: string viewStateString = writer.ToString();
50: byte[] bytes = Convert.FromBase64String(viewStateString);
51: bytes = Compressor.Compress(bytes);
52: ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
53: }
54: /// <summary>
55: /// Move View State to Below Page
56: /// </summary>
57: /// <param name="writer"></param>
58: protected override void Render(System.Web.UI.HtmlTextWriter writer)
59: {
60: System.IO.StringWriter stringWriter = new System.IO.StringWriter();
61: HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
62: base.Render(htmlWriter);
63: string html = stringWriter.ToString();
64: // int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
65: int StartPoint = html.IndexOf("<input type=\"hidden\" name=\"__VSTATE\"");
66:
67: if (StartPoint >= 0)
68: {
69: int EndPoint = html.IndexOf("/>", StartPoint) + 2;
70: string viewstateInput = html.Substring(StartPoint, EndPoint - StartPoint);
71: html = html.Remove(StartPoint, EndPoint - StartPoint);
72: int FormEndStart = html.IndexOf("</form>");
73: if (FormEndStart >= 0)
74: {
75: html = html.Insert(FormEndStart, viewstateInput);
76: }
77: }
78: writer.Write(html);
79:
80: }
81: }
Now all you've gotta do is inherit from this base page. I'll make another post on creating simple template
Losformater is the forgotten formater.
"The limited object serialization (LOS) formatter is designed for highly compact ASCII format serialization. This class supports serializing any object graph, but is optimized for those containing strings, arrays, and hashtables. It offers second order optimization for many of the .NET primitive types"
For addition in performance
Well, Do As Yslow suggest us.
1.Move every javascript at the end of the body
2.CSS always on top
3.CN
4.and so on and so on