public static double OperationTimer(Action action, bool gabageCollect = false) { action.ShouldNotBeNull("action"); if(gabageCollect) GabageCollect(); var stopwatch = new Stopwatch(); try { stopwatch.Start(); action(); } finally { stopwatch.Stop(); } return stopwatch.ElapsedMilliseconds; }
public void CallsDelegateAndReturnNonZeroTiming() { bool called = false; double msecs = With.OperationTimer(delegate { called = true; ThreadTool.Sleep(40); }); // The only reliable way of testing this that I can think of. Assert.IsTrue(msecs > 0); Assert.IsTrue(called); Console.WriteLine("Operation elapsed time (msec): " + msecs); }