Wrap all ViewEngines in a profiler

Wrap all ViewEngines in a custom profiler view engine, using the
ProfilerResolver to track performance
This commit is contained in:
mortenbock
2013-10-02 17:48:44 +02:00
parent e6bb999d83
commit 9ec30860b0
4 changed files with 90 additions and 6 deletions

View File

@@ -0,0 +1,28 @@
using System.IO;
using System.Web.Mvc;
namespace Umbraco.Core.Profiling
{
public class ProfilingView : IView
{
private readonly IView _inner;
private readonly string _name;
private readonly string _viewPath;
public ProfilingView(IView inner)
{
_inner = inner;
_name = inner.GetType().Name;
var razorView = inner as RazorView;
_viewPath = razorView != null ? razorView.ViewPath : "Unknown";
}
public void Render(ViewContext viewContext, TextWriter writer)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("{0}.Render: {1}", _name, _viewPath)))
{
_inner.Render(viewContext, writer);
}
}
}
}

View File

@@ -0,0 +1,48 @@
using System.Web.Mvc;
namespace Umbraco.Core.Profiling
{
public class ProfilingViewEngine: IViewEngine
{
private readonly IViewEngine _inner;
private readonly string _name;
public ProfilingViewEngine(IViewEngine inner)
{
_inner = inner;
_name = inner.GetType().Name;
}
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("{0}.FindPartialView, {1}, {2}", _name, partialViewName, useCache)))
{
return WrapResult(_inner.FindPartialView(controllerContext, partialViewName, useCache));
}
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("{0}.FindView, {1}, {2}, {3}", _name, viewName, masterName, useCache)))
{
return WrapResult(_inner.FindView(controllerContext, viewName, masterName, useCache));
}
}
private static ViewEngineResult WrapResult(ViewEngineResult innerResult)
{
var profiledResult = innerResult.View != null ?
new ViewEngineResult(new ProfilingView(innerResult.View), innerResult.ViewEngine) :
new ViewEngineResult(innerResult.SearchedLocations);
return profiledResult;
}
public void ReleaseView(ControllerContext controllerContext, IView view)
{
using (ProfilerResolver.Current.Profiler.Step(string.Format("{0}.ReleaseView, {1}", _name, view.GetType().Name)))
{
_inner.ReleaseView(controllerContext, view);
}
}
}
}

View File

@@ -562,6 +562,8 @@
<Compile Include="Profiling\LogProfiler.cs" />
<Compile Include="Profiling\ProfilerExtensions.cs" />
<Compile Include="Profiling\ProfilerResolver.cs" />
<Compile Include="Profiling\ProfilingView.cs" />
<Compile Include="Profiling\ProfilingViewEngine.cs" />
<Compile Include="Profiling\WebProfiler.cs" />
<Compile Include="PublishedContentExtensions.cs" />
<Compile Include="Dictionary\ICultureDictionary.cs" />

View File

@@ -9,7 +9,6 @@ using StackExchange.Profiling.MVCHelpers;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Dictionary;
using Umbraco.Core.Dynamics;
using Umbraco.Core.Logging;
using Umbraco.Core.ObjectResolution;
using Umbraco.Core.Profiling;
@@ -25,9 +24,8 @@ using Umbraco.Web.PublishedCache;
using Umbraco.Web.Routing;
using Umbraco.Web.WebApi;
using umbraco.BusinessLogic;
using umbraco.businesslogic;
using umbraco.cms.businesslogic;
using umbraco.presentation.cache;
using ProfilingViewEngine = Umbraco.Core.Profiling.ProfilingViewEngine;
namespace Umbraco.Web
@@ -73,9 +71,9 @@ namespace Umbraco.Web
new MasterControllerFactory(FilteredControllerFactoriesResolver.Current));
//set the render view engine
ViewEngines.Engines.Add(new ProfilingViewEngine(new RenderViewEngine()));
ViewEngines.Engines.Add(new RenderViewEngine());
//set the plugin view engine
ViewEngines.Engines.Add(new ProfilingViewEngine(new PluginViewEngine()));
ViewEngines.Engines.Add(new PluginViewEngine());
//set model binder
ModelBinders.Binders.Add(new KeyValuePair<Type, IModelBinder>(typeof(RenderModel), new RenderModelBinder()));
@@ -129,7 +127,15 @@ namespace Umbraco.Web
/// <returns></returns>
public override IBootManager Complete(Action<ApplicationContext> afterComplete)
{
//set routes
//Wrap viewengines in the profiling engine
IViewEngine[] engines = ViewEngines.Engines.Select(e => e).ToArray();
ViewEngines.Engines.Clear();
foreach (var engine in engines)
{
ViewEngines.Engines.Add(new ProfilingViewEngine(engine));
}
//set routes
CreateRoutes();
base.Complete(afterComplete);