using System; using System.Collections; using NUnit.Framework; using NUnit.Framework.Interfaces; using NUnit.Framework.Internal; using Unity.PerformanceTesting.Data; using Unity.PerformanceTesting.Runtime; using UnityEngine.TestTools; namespace Unity.PerformanceTesting { /// /// Test attribute to specify a performance test. It will add category "Performance" to test properties. /// Optionally, a performance regression threshold can be specified, to be used at the test result endpoint. /// [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class PerformanceAttribute : CategoryAttribute, IOuterUnityTestAction, IApplyToTest { /// /// Adds performance attribute to a test method. /// public PerformanceAttribute() : base("Performance") { } /// /// Performance regression threshold. /// public double Threshold { get; } = PerformanceTest.DefaultThresholdValue; /// /// Adds performance attribute to a test method with a performance regression threshold. /// /// A performance regression threshold to associate for the given test, taken into account at the result endpoint. The value represents a multiple of the result. Only positive values are considered valid. If the value is invalid, the property will not be serialized/sent to the result endpoint. public PerformanceAttribute(double threshold) : base("Performance") { Threshold = threshold; } /// /// Executed before a test execution. /// /// Test to execute. /// Enumerable collection of actions to perform before test setup. public IEnumerator BeforeTest(ITest test) { if (RunSettings.Instance == null) { RunSettings.Instance = ResourcesLoader.Load(Utils.RunSettings, Utils.PlayerPrefKeySettingsJSON); } // domain reload will cause this method to be hit multiple times // active performance test is serialized and survives reloads if (PerformanceTest.Active == null) { PerformanceTest.StartTest(test); yield return null; } } /// /// Executed after a test execution. /// /// Executed test. /// Enumerable collection of actions to perform after test teardown. public IEnumerator AfterTest(ITest test) { PerformanceTest.EndTest(test); yield return null; } /// /// Used to apply the Threshold property to the Test's Properties /// /// An NUnit test public new void ApplyToTest(Test test) { if (Threshold > PerformanceTest.ThresholdSerializationCutOff) { test.Properties.Add("Threshold", Threshold); } base.ApplyToTest(test); } } }