Skip to content

Simple performance stopwatch

Jon P Smith edited this page Dec 16, 2024 · 16 revisions

I often needed to know how long a process took. I do use BenchmarkDotNet, but often I want a simpler method, and My solution is TimeThings. TimeThings is very easy to use and very flexible (see next section), but its nowhere as good at BenchmarkDotNet, but it only adds ~0.3 ms to the performance test. I use TimeThings to get a "quick and dirty" performance while I'm building a new application, but if performance is important I use BenchmarkDotNet to get accurate timing, for instance I used BenchmarkDotNet in my Net.DistributedFileStoreCache.

TimeThings used in Xunit unit tests

I usually TimeThings in my Xunit unit tests, and the code below shows a basic use, but you be aware most unit tests run in parallel, which affect the time a process takes. Therefore you need to run the Xunit's test that contains a TimeThings on its own to get the correct performance timing (or if you have one unit test class that contains multiple TimeThingss, then you can run all of the unit tests in the class because each unit test is run sequentially in a test class). The code below shows a Xunit with the a TimeThings - the result performance time is sent to your Test Explorer's output panel.

public class PerfBookJsonContext
{
    private readonly ITestOutputHelper _output;
    public PerfBookJsonContext(ITestOutputHelper output) => _output = output;

    [Fact]
    public void TestWithTimeThings()
    {
        //SETUP                  
        //your test setup

        //ATTEMPT
        using (new TimeThings(output)) //The performance time it sent to your 
        {
            //your process you need to test and 
        }

        //VERIFY
        //your test checks
    }
}

TimeThings used in outside of Xunit

The TimeThings has a version which takes an Action<TimeThingResult> which is fills in the performance data when the TimeThings is disposed.

[Fact]
public void TestTimeThingResultReturn()
{
    //SETUP                  
    TimeThingResult result = null;

    //ATTEMPT
    using (new TimeThings(x => result = x))
    {
        Thread.Sleep(10);
    }

    //VERIFY
    //your test checks
}

The TimeThings's result data

The TimeThings examples shown so far only uses the first parameter, but there are two optional parameters. Here is the Xunit's TimeThings class:

public TimeThings(ITestOutputHelper output, string message = "", int numRuns = 0)

And here are the types of output string you would get for the three possibilities.

new TimeThings output string
new TimeThings(_output) "took 1.64 ms."
new TimeThings(_output, "Sample 1") "Sample 1 took 1.64 ms."
new TimeThings(_output, "read book", 500) "500 x read book took 23.45 ms., ave. per run = 46.90 us."

Note that the third version will return an average time by dividing the overall time but the number of, which is scaled to make the timing more useful. The time types are

short name full name
sec. Seconds
ms. Milliseconds
us. Microseconds
ns. Nanoseconds

END

Clone this wiki locally