Course contentsShow
← C#
Lesson 1697 of 1,90815. Software Quality and TestingFree lesson

Performance Regression Testing

Set up automated thresholds in CI to catch performance regressions.

In this lesson, you will learn how to shift from reactive performance monitoring to proactive quality assurance by setting automated thresholds in your CI pipeline.

The Problem with Manual Performance Baselines

In a mature codebase, performance regressions are often silent killers. A change that adds 50ms to a request might seem trivial, but as your architecture scales, those milliseconds accumulate into catastrophic system latency. Relying on "feeling" the performance or manual profiling is insufficient for high-velocity CI/CD.

Establishing Thresholds

To catch these regressions, you must treat performance metrics as first-class test results. We define a "Performance Budget"—an upper bound on execution time or memory allocation—that, if exceeded, causes the build to fail.

Using BenchmarkDotNet is the standard for C# performance measurement, but to automate this in CI, you must treat its output as a gate. You can integrate this by parsing the JSON output from BenchmarkDotNet and comparing it against a known "Gold Standard" baseline.

Consider this logic in your CI build script:

public void AssertPerformanceThreshold(BenchmarkResult current, BenchmarkResult baseline)
{
    // Set a 10% tolerance threshold
    double threshold = 1.10;
    
    if (current.MeanExecutionTime > (baseline.MeanExecutionTime * threshold))
    {
        throw new Exception($"Performance regression detected! " +
            $"Current: {current.MeanExecutionTime}ms, " +
            $"Baseline: {baseline.MeanExecutionTime}ms");
    }
}

CI/CD Integration Strategy

  1. Baseline Storage: Store your "baseline" results (e.g., in a JSON file or a dedicated database) that represent the performance of your last stable release.
  2. Automated Comparison: During your CI pipeline, run the performance suite and compare the results against the stored baseline.
  3. Hard Failure: If the current runtime exceeds the baseline by your defined percentage, configure the pipeline to exit 1. This stops the deployment immediately, forcing the developer to investigate the performance penalty before the code merges.

By enforcing these thresholds, you turn performance from an abstract goal into a hard constraint that the CI server defends on your behalf.

Key Takeaway: Automated performance regression testing requires comparing current execution metrics against a version-controlled baseline and failing the build if thresholds are exceeded.