asp.net页面加载时间计算方案

本文介绍了一种在ASP.NET中计算页面加载时间的方法。通过在页面预初始化时记录当前时间,并在加载完成后计算时间差,可以得到页面的加载耗时。此方法能够帮助开发者评估页面性能,并监测对数据库和服务调用等操作的改动是否有效提升了页面加载速度。

asp.net页面加载时间计算方案
protected DateTime dt;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        protected override void OnPreInit(EventArgs e)
        {
            dt = DateTime.Now;
            base.OnPreInit(e);
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            base.OnLoadComplete(e);
            TimeSpan ts = DateTime.Now - dt;
            Response.Write("加载页面用时:" + ts.TotalMilliseconds);
        }

 

 

 

引自:

To achieve something "good enough" for most purposes, you can add something like the following to your base page class or master page:

DateTime startTime = DateTime.Now;
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    this.LoadTime.Text = (DateTime.Now - startTime).TotalMilliseconds.ToString();
    this.ServerTime.Text = DateTime.Now.ToString();
}


This example is from a master page that includes the following in its markup:

<div id="footer">Load Time: <asp:Literal runat="server" ID="LoadTime" /> ms<br />
Server Time:  <asp:Literal runat="server" ID="ServerTime" /><br />


This approach doesn't include some of the time spent on rendering logic, but does include everything else the page is doing, such as expensive calls to web services, the file system, or the database, and so it does a good job of showing whether or not changes to such access are improving the overall page load time (or not).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值