/// <summary> /// Executes the action and records the time to complete the action. /// The time is recorded in system clock ticks. /// This time may vary between frameworks and platforms, but is equivalent to <c>(1/Stopwatch.Frequency)</c> seconds. /// Note this is a convenience method and can carry a cost. /// If the <paramref name="action"/> delegate is not cached, then it may incur an allocation cost for each invocation of <see cref="Record"/> /// </summary> /// <param name="recorder">The <see cref="IRecorder"/> instance to record the latency in.</param> /// <param name="action">The functionality to execute and measure</param> /// <remarks> /// <para> /// The units returned from <code>Stopwatch.GetTimestamp()</code> are used as the unit of /// recording here as they are the smallest unit that .NET can measure and require no /// conversion at time of recording. /// Instead conversion (scaling) can be done at time of output to microseconds, milliseconds, /// seconds or other appropriate unit. /// These units are sometimes referred to as ticks, but should not not to be confused with /// ticks used in <seealso cref="DateTime"/> or <seealso cref="TimeSpan"/>. /// </para> /// <para> /// If you are able to cache the <paramref name="action"/> delegate, then doing so is encouraged. /// <example> /// Here are two examples. /// The first does not cache the delegate /// /// <code> /// for (long i = 0; i < loopCount; i++) /// { /// histogram.Record(IncrementNumber); /// } /// </code> /// This second example does cache the delegate /// <code> /// Action incrementNumber = IncrementNumber; /// for (long i = 0; i < loopCount; i++) /// { /// histogram.Record(incrementNumber); /// } /// </code> /// In the second example, we will not be making allocations each time i.e. an allocation of an <seealso cref="Action"/> from <code>IncrementNumber</code>. /// This will reduce memory pressure and therefore garbage collection penalties. /// For performance sensitive applications, this method may not be suitable. /// As always, you are encouraged to test and measure the impact for your scenario. /// </example> /// </para> /// </remarks> public static void Record(this IRecorder recorder, Action action) { var start = ValueStopwatch.GetTimestamp(); action(); var elapsed = ValueStopwatch.GetTimestamp() - start; recorder.RecordValue(elapsed); }
public void Dispose() { var elapsed = ValueStopwatch.GetTimestamp() - _start; _recorder.RecordValue(elapsed); }
public Timer(IRecorder recorder) { _recorder = recorder; _start = ValueStopwatch.GetTimestamp(); }