Could you create a global action filter? Something like this:
public class PerformanceTestFilter : IActionFilter
{
private Stopwatch stopWatch = new Stopwatch();
public void OnActionExecuting(ActionExecutingContext filterContext)
{
stopWatch.Reset();
stopWatch.Start();
}
public void OnActionExecuted(ActionExecutedContext filterContext)
{
stopWatch.Stop();
var executionTime = stopWatch.ElapsedMilliseconds;
// Do something with the executionTime
}
}
and then add it to your GlobalFilters
collection in `Application_Start()'
GlobalFilters.Filters.Add(new PerformanceTestFilter());
You can get details about the controller being tested from the filterContext
parameter.
UPDATE
Adding the filter to the GlobalFilters
collection directly will create a single instance of the filter for all actions. This, of course, will not work for timing concurrent requests. To create a new instance for each action, create a filter provider:
public class PerformanceTestFilterProvider : IFilterProvider
{
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
return new[] {new Filter(new PerformanceTestFilter(), FilterScope.Global, 0)};
}
}
Then instead of adding the filter directly, rather add the filter provider in Application_Start()
:
FilterProviders.Providers.Add(new PerformanceTestFilterProvider());