Notes on Automated Performance Testing in Unity

In my current project, Fluff, I want to automate my performance testing so that any PR that does not meet my minimal performance requirements will be rejected. The documentation for The Unity Performance Tesing Extension gets me 90% of the way there, but I have to do 2 things to get it working. I’m documenting it here because odds are good that I’ll need this again someday.

Newtonsoft Conflict

I was using this wonderful AOT compatible Newtonsoft wrapper for Unity from JilleJr , but com.unity.test-framework.performance": "2.3.1-preview"references com.unity.nuget.newtonsoft-json. So, I have to remove the JilleJr Package in favor of this semi-supported one.

Reading Performance Results at Test Time

I want to read the performance results at runtime so that I can fail the test pipeline if a PR causes a sudden performance drop. This isn’t particularly tricky, but it’s not clear from the Unity Documentation.

Here’s what works for me:

[UnityTest, Performance]
public IEnumerator ExamplePerformanceTest()
{
    Action methodUnderTest = () => Vector2.MoveTowards(Vector2.one, Vector2.zero, 0.5f);
    Measure.Method(methodUnderTest)
    .WarmupCount(5)
    .IterationsPerMeasurement(10000)
    .MeasurementCount(20)
    .Run();

    // get the results of our performance test
    var performance = PerformanceTest.Active;

    // we want statistics for testing, not the raw values.
    performance.CalculateStatisticalValues();

    // Time is sampled by default. 
    // See documentation for more samples
    var elapsedSeconds = performance.SampleGroups.First(s => s.Name == "Time");

    Assert.Less(elapsedSeconds.Average, 1f); 
    // In practice, I use FluentAssertions like this: 
    // elapsedSeconds.Average.Should().BeLessThan(1);
    
    yield return null;
}