I’m working on a project to migrate several databases from SQL Server 2000 to SQL Server 2008 and I wanted a way to validate performance through unit testing.
TSQL unit testing is extremely easy with TSQLUnit. I was able to get up and running within minutes and it doesn’t require a ton of bloated software to use. All you need to do is run a single SQL script and voila you’re done.
You can read more about TSQLUnit at: http://sourceforge.net/projects/tsqlunit/
I setup a simple test using TSQLUnit to test the execution time of a very simple stored procedure which returns the average reads from a performance counter table.
The stored procedure I was testing was:
CREATE PROCEDURE [dbo].[GetAverageReads]
@AVGReads INT OUT
AS
SET NOCOUNT ON
SELECT @AVGReads = AVG(Reads)
FROM dbo.TRACE_20090325
The TSQLUnit test I used was:
CREATE PROCEDURE [dbo].[ut_TestGetAverageReads]
AS
SET NOCOUNT ON
DECLARE @startTime DATETIME
DECLARE @endTime DATETIME
SET @startTime = GETDATE()
DECLARE @AVGReads INT
EXEC dbo.GetAverageReads @AVGReads OUT
SET @endTime = GETDATE()
DECLARE @elapsedTime INT
SET @elapsedTime = DATEDIFF(ms, @startTime, @endTime)
IF (@elapsedTime > 1000)
EXEC dbo.tsu_Failure 'GetAverageReads exceeded time.'
The test checks to see if the stored procedure exceeded 1 second (1000 ms) and if so, the test will fail. Because the row count is low and the stored procedure is simple, the test passes. To see what the test results would look like if the test failed I changed the comparison to use 1 ms instead of 1000 ms.
Here is the test failure result when running the tsu_runTests stored procedure:
=======================================================
Run tests starts:Jul 9 2009 10:26AM
Warning: Null value is eliminated by an aggregate or other SET operation.
=======================================================
Testsuite: (1 tests ) execution time: 143 ms.
>>> Test: ut_TestGetAverageReads GetAverageReads exceeded time.
-----------------------------------------------------------------------
Run tests ends:Jul 9 2009 10:26AM
Summary:
1 tests, of which 1 failed and 0 had an error.
FAILURE!
-----------------------------------------------------------------------
=======================================================
This provides a base to start from as I work on the performance unit tests. You will want to do profiling and other performance tests. The performance unit tests provide a quick way to identify areas that exceed your performance requirements.
The tests can then be used within the build/CI environment, as a way to quickly identify any performance issues with a new build.
If you have any questions or feedback on this post, I would greatly appreciate it.
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.