Sql Server performance stopwatch
C#
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Properties
.gitattributes
.gitignore
ElapsedSqlStats.cs
LICENSE.txt
README.md
SqlSessionPerfStats.cs
SqlStopwatch.cs
SqlStopwatch.csproj
SqlStopwatch.sln
packages.config

README.md

SqlStopwatch Perf Utility Class

This utility is modeled after the .NET Stopwach class and is used to reveal how hard the SQL Server engine is working to service individual requests.

The constructor for SqlStopwatch expects an open SqlConnection instance. In between the subsequent calls to Start and Stop/Elapsed, the stopwatch will calculate elapsed time and total logial reads for all work performed on that SQL session.

If you're wanting to measure the performance of individual SQL calls then you should call Elapsed after each call to gather up the impact of each step. Alternately, you can Clear or call Start again on the SqlStopwatch to refresh the 'before' snapshot.

Here's a simple example using the Adventureworks database:

var sw = new SqlStopwatch(con);

// Start will capture a 'before' snapshot of the connection's
// session information. This is needed since some of the 
// counters in the DMV tables accumulate over the life
// of the session
sw.Start(); 

var emps = con.Query("SELECT * FROM HumanResources.Employee")
			  .ToList();

// Calling Elapsed will calculate teh elapsed time and 
// number of logical reads since the last time that Start 
// was called. To reset the timer, call Start again or Clear
var elapsed = sw.Elapsed;

// Output example: 
// [SPID 53] Elapsed Time: 28 ms, Logical Reads: 9
Console.WriteLine(elapsed);