Skip to content
Cyrille DUPUYDAUBY edited this page Feb 6, 2018 · 10 revisions

Check.That(Async)Code is the entry points you will use to check bits of code or delegates. Use Check.ThatAsyncCode for asyc methods/delegates, Check.ThatCode otherwise.

Performance

LastsLessThan(duration, unit)

Checks that the code is executed within a given duration, expressed in unit of time; fails otherwise.

// This check succeeds
Check.ThatCode( () =>
{
  for (var i = 0; i < 100; i++) {}
}).LastsLessThan(10, TimeUnit.Millisecons);
// This check fails
Check.ThatCode( () =>
{
  for (var i = 0; i < 5; i++)
  {
    Thread.Sleep(10);
  }
}).LastsLessThan(10, TimeUnit.Millisecons);

ConsumesLessThan(duration, unit)

Checks that the code CPU time consumption is below a given duration, expressed in unit of time; fails otherwise. Note that on Windows system, CPU time measure has a resolution of 10-15 ms, therefore you should use this check on code consumming 50ms of CPU time or more.

// This check succeeds
Check.ThatCode(c() =>
{
  Thread.Sleep(20);
}).ConsumesLessThan(100, TimeUnit.Milliseconds);

// This check fails (code explicitely consumes 40ms of computation time)
Check.ThatCode(
() =>
{
  var timer = new Stopwatch();
  timer.Start();
  while (timer.ElapsedMilliseconds < 40)
  {
    for (var i = 0; i < 1000000; i++)
    { var unused = i * 2;}
  }
}).ConsumesLessThan(10, TimeUnit.Milliseconds);

Clone this wiki locally