forked from Teknikode/Teknik
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.6 KiB
45 lines
1.6 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Diagnostics; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading.Tasks; |
|
using System.Web; |
|
|
|
namespace Teknik.Modules |
|
{ |
|
public class PerformanceMonitorModule : IHttpModule |
|
{ |
|
public void Dispose() { /* Nothing to do */ } |
|
public void Init(HttpApplication context) |
|
{ |
|
context.PreRequestHandlerExecute += delegate (object sender, EventArgs e) |
|
{ |
|
HttpContext requestContext = ((HttpApplication)sender).Context; |
|
Stopwatch timer = new Stopwatch(); |
|
requestContext.Items["Timer"] = timer; |
|
timer.Start(); |
|
}; |
|
context.PostRequestHandlerExecute += delegate (object sender, EventArgs e) |
|
{ |
|
HttpContext requestContext = ((HttpApplication)sender).Context; |
|
|
|
Stopwatch timer = (Stopwatch)requestContext.Items["Timer"]; |
|
timer.Stop(); |
|
// Don't interfere with non-HTML responses |
|
|
|
if (requestContext.Response.ContentType == "text/html" && requestContext.Response.StatusCode == 200) |
|
{ |
|
double ms = (double)timer.ElapsedMilliseconds; |
|
string result = string.Format("{0:F0}", ms); |
|
|
|
requestContext.Response.Write( |
|
"<script type=\"text/javascript\">" + |
|
"var pageGenerationTime = '" + result + "';" + |
|
"pageloadStopTimer();" + |
|
"</script >"); |
|
} |
|
}; |
|
} |
|
} |
|
}
|
|
|