Using QuickBencher in automated tests

From version 0.2 the benchmarking methods started returning an object containing the measurement results. You can then use this to programmatically do stuff like asserts in automated tests - to verify that your code performs within accepted service levels (SLA). A simple performance verifier can look something like this:

    [Test]
    public void Some_test()
    {
        // Given
        var someComponent = new SomeComponent();
        // When
        var measurement = Benchmark.Measure(() => someComponent.DoSomeWork());
        // Then
        Assert.Less(measurement.RealTime, 0.98d, 
            "SLA failed: 'Some Work' should take less than 0.98 seconds.");
    }

Measurements are also returned when running a sequence of benchmarks (with the Bm or Bmbm method):

    [Test]
    public void Some_other_test()
    {
        // Given
        var someComponent = new SomeComponent();
        // When
        var measurements = Benchmark.Bmbm((x) =>
        {
            x.Report("Method 1", () => someComponent.DoSomeWork());
            x.Report("Method 2", () => someComponent.DoSomeWork2());
            x.Report("Method 3", () => someComponent.DoSomeWork3());
        });
        // Then 
        var result1 = measurements.Single((m) => m.Label.Equals("Method 1"));
        var result2 = measurements.Single((m) => m.Label.Equals("Method 2"));
        var result3 = measurements.Single((m) => m.Label.Equals("Method 3"));
        Assert.Less(result1.RealTime, result2.RealTime, "Method 1 should be faster than 2");
        Assert.Less(result2.RealTime, result3.RealTime, "Method 2 should be faster than 3");            
    }

Last edited Jan 27, 2010 at 9:22 AM by tormaroe, version 5

Comments

No comments yet.