Every now and then you have to time measure different parts of your system. One approach could be a simple StopWatch or even different DateTime objects subtracted one another. My approach, as with TransientOperation, is to provide a lamda friendly way of performing this task.

Instrumentation

Any measurement can be performed simply by calling one of the overloaded methods on the static TimeMeasure class; WithAction or WithFunc. The latter will contain a property holding the Result of the operation.

As with most of the "instrumentation" in this framework, you can easily attach a callback method to TimeMeasure.CompletedCallback and hereby get valuable information/statistic for your liking.

I hope this little introduction will help simplify your time measurement related operations. Happy coding!

using System;  
using System.Diagnostics;  
using System.Net;  
using System.Threading;  
using Cuemon.Diagnostics;  
using Cuemon.Net.Http;  
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cuemon.Examples  
{
    [TestClass]
    public class TimeMeasureTest
    {
        [TestMethod]
        public void AggregateTimeMeasurementTest()
        {
            var websiteProfiler = TimeMeasure.WithFunc(GetWebpageBodySize, new Uri("https://www.cuemon.net/";));
            Trace.WriteLine("Body size: {0} KB.".FormatWith(websiteProfiler.Result.BytesToKilobytes()));
            Trace.WriteLine("");
            Trace.WriteLine(websiteProfiler);
            var profiler = TimeMeasure.WithAction(MethodThatTakesBetweenOneToFiveSeconds);
            Trace.WriteLine(profiler);
            profiler = TimeMeasure.WithAction(MethodThatTakesBetweenFiveToTwentySeconds);
            Trace.WriteLine(profiler);
            profiler = TimeMeasure.WithAction(MethodWithRandomParameters, false42"flexible"Guid.Empty, 666.666m);
            Trace.WriteLine(profiler);
        }

        public long GetWebpageBodySize(Uri location)
        {
            return location.HttpGet(o => o.AutomaticDecompression = DecompressionMethods.None).ContentLength;
        }

        public void MethodThatTakesBetweenOneToFiveSeconds()
        {
            Thread.Sleep(TimeSpan.FromSeconds(NumberUtility.GetRandomNumber(16)));
        }

        public void MethodThatTakesBetweenFiveToTwentySeconds()
        {
            Thread.Sleep(TimeSpan.FromSeconds(NumberUtility.GetRandomNumber(521)));
        }

        public void MethodWithRandomParameters(bool a, int b, string c, Guid d, decimal e)
        {
            // do nothing
        }
    }
}
Notice the ClassName.MemberName(Parameters) followed by the actual elapsed time and the values of the Paramters (if any).
Body size: 31.3037109375 KB.

TimeMeasureTest.GetWebpageBodySize(Uri location) took 00:00:00.482019300 to execute. Parameters: { location=https://www.cuemon.net/ }  
TimeMeasureTest.MethodThatTakesBetweenOneToFiveSeconds() took 00:00:04.019125500 to execute.  
TimeMeasureTest.MethodThatTakesBetweenFiveToTwentySeconds() took 00:00:08.001355400 to execute.  
TimeMeasureTest.MethodWithRandomParameters(Boolean a, Int32 b, String c, Guid d, Decimal e) took 00:00:00.001324900 to execute. Parameters: { a=False, b=42, c=flexible, d=00000000-0000-0000-0000-000000000000, e=666,666 }