Alex Lindgren in Technology 4 minute read

Adding a timer to trace Ditto mapping

I've been working on improving server response time for an Umbraco project that has complex strongly-typed view models. We are using Ditto, "the friendly view-model mapper for Umbraco", to map our content to our strongly-typed models. The problem is that some landing pages are quite complex (collections of collections...) and in trying to write reusable code, we were loading more into our view models then needed for certain pages.

I needed a way to figure out what parts of the page were taking so long and visibility into which models were actually getting loaded. So what I did was add the following to my Ditto models:

    private System.Diagnostics.Stopwatch _timer = System.Diagnostics.Stopwatch.StartNew();
    internal void OnConverted(DittoConversionHandlerContext ctx)
        System.Web.HttpContext.Current.Trace.Write("Ditto converted " 
+ ctx.Content.Name + " in " + _timer.Elapsed.TotalMilliseconds + "ms"); _timer.Stop(); } #endif

I then turn tracing on for the site in the web.config:

    <trace enabled="true" requestLimit="20" pageOutput="false" 
traceMode="SortByTime" localOnly="true" /> </system.web>

and go to /trace.axd to view the trace which contains my messages which shows how long it took Ditto to convert each model.

What this does is add a trace to the model because Ditto has an event that fires when it is done and the [DittoOnConverted] attribute added to the class for each model apparently wires this up. I may end up using this and the DittoOnConverting event to optimize the data mapping but at this point it's helping me identify what parts of my overrall model needs optimizing.