public class HttpModule : IHttpModule
{
    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        //统计页面加载速度和次数
        context.PreRequestHandlerExecute += delegate (object sender, EventArgs e)
        {
            //Set Page Timer Star
            HttpContext requestContext = ((HttpApplication)sender).Context;
            Stopwatch timer = new Stopwatch();
            requestContext.Items["Timer"] = timer;
            timer.Start();
        };
        context.PostRequestHandlerExecute += delegate (object sender, EventArgs e)
        {
            HttpContext httpContext = ((HttpApplication)sender).Context;
            HttpResponse response = httpContext.Response;
            Stopwatch timer = (Stopwatch)httpContext.Items["Timer"];
            timer.Stop();
            // Don't interfere with non-HTML responses
            if (response.ContentType == "text/html")
            {
                double seconds = (double)timer.ElapsedTicks / Stopwatch.Frequency;
                //string result_time = string.Format("{0:F4} sec ", seconds);
                RenderQueriesToResponse(response, seconds, context.Request.RawUrl);
            }
        };
    }

    void RenderQueriesToResponse(HttpResponse response, double result_time,string page)
    {
        //response.Write("<div style=\"margin: 5px; background-color: #FFFF00\"");
        //response.Write(string.Format("<b>Page Generated in " + result_time));
        //response.Write("</div>");
        SqlDB db = new SqlDB();
        string[] paraName = new string[2] { "@page", "@result_time" };
        string[] paraValue = new string[2] { page, result_time.ToString() };
        db.ExecuteNonQueryByProc("AddPageLoadSpeedData", paraName, paraValue);
    }
}

 

转载请注明出处: http://bluesd7.com/蓝影闪电的随笔/ContentId/136/ASP-NET-计算每个页面的加载速度

 

评论数量 (0)

    留下一个评论

    剩余2000个字符。 一共限制在2000个字符内.
    发送评论

    搜索

    关于我

    姓名:余钊

    英文名:Joshua

    现居:武汉

    Email: yuzhao_blue@163.com

    关注:架构, 全栈, SQL, 高性能, 高并发

    日历