UnityGame/Library/PackageCache/com.unity.test-framework.performance/Editor/PerformanceTestRunSaver.cs

56 lines
1.8 KiB
C#
Raw Permalink Normal View History

2024-10-27 10:53:47 +03:00
using System;
using System.IO;
using Unity.PerformanceTesting.Runtime;
using UnityEditor;
using UnityEngine;
using UnityEditor.TestTools.TestRunner.Api;
using UnityEditor.TestTools.TestRunner.CommandLineTest;
namespace Unity.PerformanceTesting.Editor
{
[InitializeOnLoad]
internal class TestRunnerInitializer
{
static TestRunnerInitializer()
{
var api = ScriptableObject.CreateInstance<TestRunnerApi>();
var obj = ScriptableObject.CreateInstance<PerformanceTestRunSaver>();
api.RegisterCallbacks(obj);
}
}
[Serializable]
internal class PerformanceTestRunSaver : ScriptableObject, ICallbacks
{
void ICallbacks.RunStarted(ITestAdaptor testsToRun)
{
PerformanceTest.Active = null;
}
void ICallbacks.RunFinished(ITestResultAdaptor result)
{
PlayerCallbacks.Saved = false;
try
{
var resultWriter = new ResultsWriter();
var xmlPath = Path.Combine(Application.persistentDataPath, "TestResults.xml");
var jsonPath = Path.Combine(Application.persistentDataPath, "PerformanceTestResults.json");
resultWriter.WriteResultToFile(result, xmlPath);
var xmlParser = new TestResultXmlParser();
var run = xmlParser.GetPerformanceTestRunFromXml(xmlPath);
if (run == null) return;
File.WriteAllText(jsonPath, JsonUtility.ToJson(run, true));
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\n" + e.InnerException);
}
}
void ICallbacks.TestStarted(ITestAdaptor test) { }
void ICallbacks.TestFinished(ITestResultAdaptor result) { }
}
}