Get page load time in asp.net using c#

In this article, i will tell you guys how to get page load time. I have a web form that name is getloadtime.aspx, lets suppose there is lot of code written in it due to that its loading speed is very slow, to calculate the load time following code snippet will be used.

getloadtime.aspx.cs

    DateTime ServerStartTime;
    DateTime ServerEndTime;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnPreInit(EventArgs e)
    {
        ServerStartTime = DateTime.Now;
        base.OnPreInit(e);
    }

    protected override void OnLoadComplete(EventArgs e)
    {

        ServerEndTime = DateTime.Now;
        TrackPageTime();//It will give you page load time
    }


    public void TrackPageTime()
    {
        TimeSpan serverTimeDiff = ServerEndTime.Subtract(ServerStartTime);
    }


I hope this article will be proved very handy for you people. Enjoy your work, enjoy coding.

0 comments: