public void CheckOnLimitValue() { // simulate arbitrary execution signature var trace = new RunTrace { ExecutionTime = TimeSpan.FromMilliseconds(30), TotalProcessorTime = TimeSpan.FromMilliseconds(20) }; Check.That(trace).LastsLessThan(30, TimeUnit.Milliseconds); Check.ThatCode(() => Check.That(trace).LastsLessThan(29, TimeUnit.Milliseconds)). IsAFailingCheckWithMessage("", "The checked code's execution time was too high.", "The checked code's execution time:", "\t[30 Milliseconds]", "The expected code's execution time: less than", "\t[29 Milliseconds]"); Check.That(trace).ConsumesLessThan(20, TimeUnit.Milliseconds); Check.ThatCode(() => Check.That(trace).ConsumesLessThan(19, TimeUnit.Milliseconds)). IsAFailingCheckWithMessage("", "The checked code's cpu consumption was too high.", "The checked code's cpu consumption:", "\t[20 Milliseconds]", "The expected code's cpu consumption: less than", "\t[19 Milliseconds]"); }
private static void CaptureTrace(Action action, RunTrace result) { #if !PORTABLE var watch = new Stopwatch(); var cpu = Process.GetCurrentProcess().TotalProcessorTime; watch.Start(); #else var watchPortable = DateTime.UtcNow; #endif try { action(); } catch (Exception e) { result.RaisedException = e; } finally { #if !PORTABLE watch.Stop(); result.TotalProcessorTime = Process.GetCurrentProcess().TotalProcessorTime - cpu; // ReSharper disable PossibleLossOfFraction result.ExecutionTime = TimeSpan.FromTicks(watch.ElapsedTicks); #else result.ExecutionTime = DateTime.Now - watchPortable; result.TotalProcessorTime = result.ExecutionTime; #endif } }
/// <summary> /// Execute the action to capture the run. /// </summary> /// <param name="action"> /// <see cref="Action"/> to be analyzed. /// </param> /// <returns> /// Return <see cref="RunTrace"/> describing the execution. /// </returns> internal static RunTrace GetTrace(Action action) { var result = new RunTrace(); CaptureTrace(action, result); return(result); }
internal static RunTrace GetAsyncTrace(Func <Task> awaitableMethod) { var result = new RunTrace(); CaptureTrace( () => { try { // starts and waits the completion of the awaitable method awaitableMethod().Wait(); } catch (AggregateException exception) { result.RaisedException = exception.InnerException; } }, result); return(result); }