One final thing we’ll explore is a package that extends Unity Test Framework with Performance Tests.
The Performance Testing package can be used to measure performance in our game. This is a great tool if we want to track various regressions/progressions that happen over time in our project. In this example, you’ll learn how to create a test that measures game average frames.
Unity.PerformanceTesting
in your PlayModeTests assembly definition to access the performance testing APIs.You’re now ready to complete your objective. In PerformanceTests.cs
create a new function called MainScene_MeasureAverageFrames()
. In this function move your character to the wand position and wait until the wand pickup effect is over. During all that time, measure the frames.
Time.deltaTime
from UnityEngine API and Measure.Custom
from the Performance Testing package API.Measure.Frames().Scope()
API to measure them into a separate scope.PerformanceTests.cs
using System.Collections;
using Unity.PerformanceTesting;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.SceneManagement;
public class PerformanceTests
{
private Transform _characterTransform;
private float _wandLocation = 21.080f;
[UnityTest, Performance]
public IEnumerator MainScene_MeasureAverageFrames()
{
SceneManager.LoadScene("Assets/Scenes/Main.unity", LoadSceneMode.Single);
using (Measure.Frames().Scope("Frames.MainSceneOnLoad.Unstable"))
{
for (var i = 0; i < 25; i++)
{
yield return null;
}
}
using (Measure.Frames().Scope("Frames.MainSceneGameplay"))
{
yield return GoRight();
while (GetCurrentCharacterPosition() <= _wandLocation)
{
yield return null;
}
StopMoving();
yield return new WaitForSeconds(15);
}
}
private float GetCurrentCharacterPosition()
{
// Get Main character's Transform which is used to manipulate position.
if (_characterTransform == null)
{
_characterTransform = GameObject.Find("Sara Variant").transform;
}
return _characterTransform.position.x;
}
private IEnumerator GoRight()
{
TestInputControl.MoveLeft = false;
yield return null;
TestInputControl.MoveRight = true;
}
private void StopMoving()
{
TestInputControl.MoveRight = false;
TestInputControl.MoveLeft = false;
}
}
Bonus Solution
Measure.Custom("FPS", (int)(1f / Time.deltaTime));