Simon Harriyott

Unit testing for speed?

I was just thinking about performance testing, and wondered if writing a unit test would be sensible. If a functional requirement is, for example, "Import all of the data in the crime scene XML file into the database within 0.5 seconds", a test could be written to:
  • Get the time into startTime
  • Call the ImportCrimeSceneData method
  • Get the time into endTime
  • Assert endTime < startTime + 0.5 seconds
If this fails, then the ImportCrimeSceneData method can be tuned until it passes. Once it has passed, then any future changes to the method are checked automatically to ensure that the limit isn't exceeded.

Although the idea seems like it would work, it feels like a wrong thing to do. Has anyone else done this successfully, or can anyone give me an actual reason why this is wrong (other than "it just feels like it")?
20 July 2005