Created
May 11, 2015 13:22
-
-
Save michaeljbailey/460ab0ec1f60376279df to your computer and use it in GitHub Desktop.
Timing for Razor rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Timing | |
{ | |
using System; | |
using System.Diagnostics; | |
using System.Web.Mvc; | |
public class TraceScope : IDisposable | |
{ | |
private readonly string _name; | |
private readonly ViewContext _viewContext; | |
private readonly Stopwatch _stopwatch; | |
public TraceScope(string name, ViewContext viewContext) | |
{ | |
_name = name; | |
_viewContext = viewContext; | |
_stopwatch = Stopwatch.StartNew(); | |
} | |
public void Dispose() | |
{ | |
_stopwatch.Stop(); | |
_viewContext.Writer.Write("<!-- {0} rendered in {1} milliseconds -->", _name, _stopwatch.ElapsedMilliseconds); | |
} | |
} | |
public static class HtmlHelperExtensions | |
{ | |
public static IDisposable BeginTrace(this HtmlHelper helper, string name) | |
{ | |
return new TraceScope(name, helper.ViewContext); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment