The Unity Performance Testing Package extends Unity Test Framework with performance testing capabilities. It provides an API and test case decorators for taking measurements/samples of Unity profiler markers, and other custom metrics, in the Unity Editor and built players. It also collects configuration metadata that is useful for comparing data across different hardware and configurations, such as build and player settings.
The Performance Testing Package is intended for use with the Unity Test Framework. You should be familiar with how to create and run tests as described in the Unity Test Framework documentation.
Note: When tests are run with Unity Test Framework, a development player is always built to support communication between the editor and player, effectively overriding the development build setting from the build settings UI or scripting API.
Install the Performance Testing Package package using one of the following methods:
Example:
manifest.json
file for your Unity project (located in the YourProject/Packages directory) in a text editor."com.unity.test-framework.performance": "3.0.3",
to the dependencies.When the package is installed, add a reference to Unity.PerformanceTesting
in your assembly definition to access the performance testing APIs.
Unity releases can often include changes that break compatibility with the Performance Testing Package, so we cannot currently guarantee latest package version compatability with every Unity version. The table below shows which version of the package is compatible with which Unity release streams.
Unity stream | Package version |
---|---|
2023.2 | 3.0.3 |
2023.1 | 3.0.3 |
2022.2 | 3.0.3 |
2022.1 | 3.0.3 |
2021.3 | 3.0.3 |
2020.3 | 3.0.3 |
2019.4 | 2.8.1-preview |
Use IPrebuildSetup attribute when you need to generate assets. Place assets in Resources or StreamingAssets folders, scenes can be placed anywhere in the project, but should be added to build settings.
public class TestsWithPrebuildStep : IPrebuildSetup
{
public void Setup()
{
// this code is executed before entering playmode or the player is executed
}
}
public class MyAmazingPerformanceTest
{
[Test, Performance]
[PrebuildSetup(typeof(TestsWithPrebuildStep))]
public void Test()
{
...
}
}
When loading scenes in IPrebuildSetup you have to use LoadSceneMode.Additive
.
private static string m_ArtifactsPath = "Assets/Artifacts/";
public static Scene NewScene(NewSceneSetup setup)
{
Scene scene = EditorSceneManager.NewScene(setup, NewSceneMode.Additive);
EditorSceneManager.SetActiveScene(scene);
return scene;
}
public static void SaveScene(Scene scene, string name, bool closeScene = true)
{
EditorSceneManager.SaveScene(scene, GetScenePath(name));
if (closeScene)
{
foreach (var sceneSetting in EditorBuildSettings.scenes)
if (sceneSetting.path == GetScenePath((name)))
return;
EditorSceneManager.CloseScene(scene, true);
EditorSceneManager.SetActiveScene(EditorSceneManager.GetSceneAt(0));
var newListOfScenes = new List<EditorBuildSettingsScene>();
newListOfScenes.Add(new EditorBuildSettingsScene(GetScenePath(name), true));
newListOfScenes.AddRange(EditorBuildSettings.scenes);
EditorBuildSettings.scenes = newListOfScenes.ToArray();
}
}
public static string GetScenePath(string name)
{
return m_ArtifactsPath + name + ".unity";
}