Thom Lawrence

Perfidy: Performance Testing In NUnit

September 17, 2006 · No Comments

Unsatisfied with calling QueryPerformanceCounter and being done with it, I spent today coming up with a fluent interface for performance testing in NUnit:

using (At.Most(5).Seconds)
{
    // Stuff that should take under 5 seconds
}

using (At.Most(1).Minute.GiveOrTake(10).Seconds)
{
    // Stuff that should take under 70 seconds
}

Using.At.Most(10).Seconds.For(1000).Iterations.Do(delegate()
{
    // Stuff that should take under 10 seconds for 1000 iterations
});

Using.At.Most(5).Seconds.For(20).Threads.Do(delegate()
{
    // Stuff that should take under 5 seconds when run inside
    // 20 parallel threads
});

It’s basically a wrapper around NUnit’s Assert.Less() and I call it Perfidy (which I thought was a good word for code that treacherously refuses to pass its tests). You can grab a very rough version here: Perfidy.zip

I suppose this kind of thing doesn’t really qualify as unit testing, and it’s not deterministic. But if you’re fairly generous with the limits and get your tests up and running early in a project, you at least know vaguely where you’re going to suffer.

I’ve not included any tests for the tests. I wasn’t sure if NUnit gives you a way to say ‘give me a green bar if this test fails’, and to test everything takes a fair coffee break, as there are lots of Thread.Sleep()s around. If anyone spots anything obviously wrong, or knows of another library that does the same sort of thing, I’d love to hear about it. Especially if it has pretty syntax. :)

Categories: C# · Code · Programming
Tagged: , , , ,

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment