Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Diagnostics;
using System.Web;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
namespace Website.Pipelines.RenderRendering
{
public class PerformanceMeasurementProcessorBegin : RenderRenderingProcessor
{
public override void Process(RenderRenderingArgs args)
{
#if DEBUG
args.Writer.WriteLine();
args.Writer.WriteLine("<!-- START: {0} -->", args.Rendering.Renderer);
var sw = new Stopwatch();
HttpContext.Current.Items["PerformanceMeasurementProcessor:sw"] = sw;
sw.Start();
#endif
}
}
}
using System.Diagnostics;
using System.Web;
using Sitecore.Diagnostics;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
namespace Website.Pipelines.RenderRendering
{
public class PerformanceMeasurementProcessorEnd : RenderRenderingProcessor
{
public override void Process(RenderRenderingArgs args)
{
#if DEBUG
var sw = HttpContext.Current.Items["PerformanceMeasurementProcessor:sw"] as Stopwatch;
if (sw == null)
{
Log.Warn("Stopwatch not found!", this);
return;
}
sw.Stop();
if (sw.ElapsedMilliseconds > 100)
Log.Warn($"PERFORMANCE ALERT: {args.Rendering.Renderer} Time taken: {sw.ElapsedMilliseconds}ms", this);
args.Writer.WriteLine();
args.Writer.WriteLine($"<!-- END: {args.Rendering.Renderer} Time taken: {sw.ElapsedMilliseconds}ms -->");
#endif
}
}
}
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<mvc.renderRendering>
<processor type="Website.Pipelines.RenderRendering.PerformanceMeasurementProcessorBegin, Website" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']"/>
<processor type="Website.Pipelines.RenderRendering.PerformanceMeasurementProcessorEnd, Website" patch:after="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']"/>
</mvc.renderRendering>
</pipelines>
</sitecore>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment