From fbc2291f29efdd25b9ac29fee86a17eb5841574a Mon Sep 17 00:00:00 2001 From: Sean Stolberg Date: Thu, 6 Sep 2018 15:36:56 +0300 Subject: [PATCH] Cherry pick from 2018.2 --- .../Prebuild/RenderPerformancePrebuildStep.cs | 26 +- .../RenderPerformanceMonoBehaviorTest.cs | 6 +- ...enderPerformanceWithObjMonoBehaviorTest.cs | 4 +- .../Assets/Tests/PlaymodeMetadataCollector.cs | 232 ++++++++++++++++++ .../Tests/PlaymodeMetadataCollector.cs.meta | 11 + 5 files changed, 275 insertions(+), 4 deletions(-) create mode 100644 PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs create mode 100644 PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs.meta diff --git a/PerformanceTests/UnityPerformanceBenchmark/Assets/Prebuild/RenderPerformancePrebuildStep.cs b/PerformanceTests/UnityPerformanceBenchmark/Assets/Prebuild/RenderPerformancePrebuildStep.cs index c3e68e5c..6093fc90 100644 --- a/PerformanceTests/UnityPerformanceBenchmark/Assets/Prebuild/RenderPerformancePrebuildStep.cs +++ b/PerformanceTests/UnityPerformanceBenchmark/Assets/Prebuild/RenderPerformancePrebuildStep.cs @@ -18,6 +18,7 @@ public class RenderPerformancePrebuildStep : IPrebuildSetup private List enabledXrTargets = new List(); private GraphicsDeviceType playerGraphicsApi; private StereoRenderingPath stereoRenderingPath = StereoRenderingPath.SinglePass; + private ScriptingImplementation scriptingImplementation = ScriptingImplementation.IL2CPP; private bool mtRendering = true; private bool graphicsJobs = false; private AndroidSdkVersions minimumAndroidSdkVersion = AndroidSdkVersions.AndroidApiLevel24; @@ -47,7 +48,8 @@ public void Setup() PlayerSettings.SetGraphicsAPIs(EditorUserBuildSettings.activeBuildTarget, new[] {playerGraphicsApi}); PlayerSettings.MTRendering = mtRendering; PlayerSettings.graphicsJobs = graphicsJobs; - PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, "com.unity3d.performance.benchmark"); + PlayerSettings.SetScriptingBackend(EditorUserBuildSettings.selectedBuildTargetGroup, scriptingImplementation); + // If Android, setup Android player settings if (EditorUserBuildSettings.selectedBuildTargetGroup == BuildTargetGroup.Android) @@ -61,6 +63,7 @@ public void Setup() // If iOS, setup iOS player settings if (EditorUserBuildSettings.selectedBuildTargetGroup == BuildTargetGroup.iOS) { + PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, "com.unity3d.performance.benchmark"); PlayerSettings.iOS.appleDeveloperTeamID = appleDeveloperTeamId; PlayerSettings.iOS.appleEnableAutomaticSigning = false; PlayerSettings.iOS.iOSManualProvisioningProfileID = iOsProvisioningProfileId; @@ -128,6 +131,9 @@ private void CreatePerformanceTestRunJson(PerformanceTestRun perfTestRun) private OptionSet DefineOptionSet() { return new OptionSet() + .Add("scriptingbackend=", + "Scripting backend to use. IL2CPP is default. Values: IL2CPP, Mono", + ParseScriptingBackend) .Add("enabledxrtargets=", "XR targets to enable in XR enabled players, separated by ';'. Values: \r\n\"Oculus\"\r\n\"OpenVR\"\r\n\"cardboard\"\r\n\"daydream\"", xrTargets => enabledXrTargets = ParseEnabledXrTargets(xrTargets)) @@ -176,5 +182,23 @@ private List ParseEnabledXrTargets(string paths) return vrTargets; } + + private void ParseScriptingBackend(string scriptingBackend) + { + var sb = scriptingBackend.ToLower(); + if (sb.Equals("mono")) + { + scriptingImplementation = ScriptingImplementation.Mono2x; + } else if (sb.Equals("il2cpp")) + { + scriptingImplementation = ScriptingImplementation.IL2CPP; + } + else + { + throw new ArgumentException(string.Format("Unrecognized scripting backend {0}. Valid options are Mono or IL2CPP", scriptingBackend)); + } + + + } #endif } diff --git a/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceMonoBehaviorTest.cs b/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceMonoBehaviorTest.cs index 3bc6bb13..1ca3935d 100644 --- a/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceMonoBehaviorTest.cs +++ b/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceMonoBehaviorTest.cs @@ -1,4 +1,5 @@ -using System; +#if UNITY_2018_1_OR_NEWER +using System; using System.Linq; using UnityEngine; using Unity.PerformanceTesting; @@ -223,4 +224,5 @@ public void FindRenderedObjectMetrics() verts = meshVertexCount; tris = trianglesLength; } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceWithObjMonoBehaviorTest.cs b/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceWithObjMonoBehaviorTest.cs index c739df44..0d9a172c 100644 --- a/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceWithObjMonoBehaviorTest.cs +++ b/PerformanceTests/UnityPerformanceBenchmark/Assets/Scripts/RenderPerformanceWithObjMonoBehaviorTest.cs @@ -1,4 +1,5 @@ -using UnityEngine; +#if UNITY_2018_1_OR_NEWER +using UnityEngine; public class RenderPerformanceWithObjMonoBehaviorTest : RenderPerformanceMonoBehaviorTest @@ -15,4 +16,5 @@ public override void RunStartCode() Camera.main.transform.LookAt(cube.transform); } } +#endif diff --git a/PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs b/PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs new file mode 100644 index 00000000..a058af78 --- /dev/null +++ b/PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs @@ -0,0 +1,232 @@ +#if UNITY_2018_1_OR_NEWER +using System; +using System.Collections; +using System.IO; +using System.Threading; +using NUnit.Framework; +using Unity.PerformanceTesting; +using Unity.PerformanceTesting.Runtime; +using UnityEngine; +using UnityEngine.Networking; +using UnityEngine.TestTools; +#if ENABLE_VR +using UnityEngine.XR; +#endif +#if UNITY_EDITOR +using UnityEditor; +#endif + +[Category("Performance")] +public class PlaymodeMetadataCollector : IPrebuildSetup +{ + private PerformanceTestRun m_TestRun; + + private string m_TestRunPath + { + get { return Path.Combine(Application.streamingAssetsPath, "PerformanceTestRunInfo.json"); } + } + + [UnityTest, Order(0), PrebuildSetup(typeof(RenderPerformancePrebuildStep))] + public IEnumerator GetPlayerSettingsTest() + { + yield return ReadPerformanceTestRunJsonAsync(); + m_TestRun.PlayerSystemInfo = GetSystemInfo(); + m_TestRun.QualitySettings = GetQualitySettings(); + m_TestRun.ScreenSettings = GetScreenSettings(); + m_TestRun.TestSuite = "Playmode"; + m_TestRun.BuildSettings.Platform = Application.platform.ToString(); + + TestContext.Out.Write("##performancetestruninfo:" + JsonUtility.ToJson(m_TestRun)); + } + + private PerformanceTestRun ReadPerformanceTestRunJson() + { + try + { + string json; + if (Application.platform == RuntimePlatform.Android) + { + UnityWebRequest reader = new UnityWebRequest("jar:file://" + m_TestRunPath); + while (!reader.isDone) + { + Thread.Sleep(1); + } + + json = reader.downloadHandler.text; + } + else + { + json = File.ReadAllText(m_TestRunPath); + } + + return JsonUtility.FromJson(json); + } + catch + { + return new PerformanceTestRun { PlayerSettings = new Unity.PerformanceTesting.PlayerSettings() }; + } + } + + + private IEnumerator ReadPerformanceTestRunJsonAsync() + { + string json; + if (Application.platform == RuntimePlatform.Android) + { + var path = m_TestRunPath; + UnityWebRequest reader = UnityWebRequest.Get(path); + yield return reader.SendWebRequest(); + + while (!reader.isDone) + { + yield return null; + } + + json = reader.downloadHandler.text; + } + else + { + if (!File.Exists(m_TestRunPath)) + { + m_TestRun = new PerformanceTestRun { PlayerSettings = new Unity.PerformanceTesting.PlayerSettings() }; + yield break; + } + json = File.ReadAllText(m_TestRunPath); + } + + m_TestRun = JsonUtility.FromJson(json); + } + + private static PlayerSystemInfo GetSystemInfo() + { + return new PlayerSystemInfo + { + OperatingSystem = SystemInfo.operatingSystem, + DeviceModel = SystemInfo.deviceModel, + DeviceName = SystemInfo.deviceName, + ProcessorType = SystemInfo.processorType, + ProcessorCount = SystemInfo.processorCount, + XrModel = XRDevice.model, + GraphicsDeviceName = SystemInfo.graphicsDeviceName, + SystemMemorySize = SystemInfo.systemMemorySize, +#if ENABLE_VR + XrDevice = XRSettings.loadedDeviceName +#endif + }; + } + + private static Unity.PerformanceTesting.QualitySettings GetQualitySettings() + { + return new Unity.PerformanceTesting.QualitySettings() + { + Vsync = UnityEngine.QualitySettings.vSyncCount, + AntiAliasing = UnityEngine.QualitySettings.antiAliasing, + ColorSpace = UnityEngine.QualitySettings.activeColorSpace.ToString(), + AnisotropicFiltering = UnityEngine.QualitySettings.anisotropicFiltering.ToString(), + BlendWeights = UnityEngine.QualitySettings.blendWeights.ToString() + }; + } + + private static ScreenSettings GetScreenSettings() + { + return new ScreenSettings + { + ScreenRefreshRate = Screen.currentResolution.refreshRate, + ScreenWidth = Screen.currentResolution.width, + ScreenHeight = Screen.currentResolution.height, + Fullscreen = Screen.fullScreen + }; + } + + public void Setup() + { +#if UNITY_EDITOR + m_TestRun = ReadPerformanceTestRunJson(); + m_TestRun.EditorVersion = GetEditorInfo(); + m_TestRun.PlayerSettings = GetPlayerSettings(m_TestRun.PlayerSettings); + m_TestRun.BuildSettings = GetPlayerBuildInfo(); + m_TestRun.StartTime = Utils.DateToInt(DateTime.Now); + + CreateStreamingAssetsFolder(); + CreatePerformanceTestRunJson(); + } + + private static EditorVersion GetEditorInfo() + { + return new EditorVersion + { + FullVersion = UnityEditorInternal.InternalEditorUtility.GetFullUnityVersion(), + DateSeconds = int.Parse(UnityEditorInternal.InternalEditorUtility.GetUnityVersionDate().ToString()), + Branch = GetEditorBranch(), + RevisionValue = int.Parse(UnityEditorInternal.InternalEditorUtility.GetUnityRevision().ToString()) + }; + } + + private static string GetEditorBranch() + { + foreach (var method in typeof(UnityEditorInternal.InternalEditorUtility).GetMethods()) + { + if (method.Name.Contains("GetUnityBuildBranch")) + { + return (string)method.Invoke(null, null); + } + } + + return "null"; + } + + private static Unity.PerformanceTesting.PlayerSettings GetPlayerSettings( + Unity.PerformanceTesting.PlayerSettings playerSettings) + { + playerSettings.VrSupported = UnityEditor.PlayerSettings.virtualRealitySupported; + playerSettings.MtRendering = UnityEditor.PlayerSettings.MTRendering; + playerSettings.GpuSkinning = UnityEditor.PlayerSettings.gpuSkinning; + playerSettings.GraphicsJobs = UnityEditor.PlayerSettings.graphicsJobs; + playerSettings.GraphicsApi = + UnityEditor.PlayerSettings.GetGraphicsAPIs(EditorUserBuildSettings.activeBuildTarget)[0] + .ToString(); + playerSettings.ScriptingBackend = UnityEditor.PlayerSettings + .GetScriptingBackend(EditorUserBuildSettings.selectedBuildTargetGroup) + .ToString(); + playerSettings.ScriptingRuntimeVersion = UnityEditor.PlayerSettings.scriptingRuntimeVersion.ToString(); + playerSettings.StereoRenderingPath = UnityEditor.PlayerSettings.stereoRenderingPath.ToString(); + playerSettings.RenderThreadingMode = UnityEditor.PlayerSettings.graphicsJobs ? "GraphicsJobs" : + UnityEditor.PlayerSettings.MTRendering ? "MultiThreaded" : "SingleThreaded"; + playerSettings.AndroidMinimumSdkVersion = UnityEditor.PlayerSettings.Android.minSdkVersion.ToString(); + playerSettings.AndroidTargetSdkVersion = UnityEditor.PlayerSettings.Android.targetSdkVersion.ToString(); + playerSettings.Batchmode = UnityEditorInternal.InternalEditorUtility.inBatchMode.ToString(); + return playerSettings; + // Currently no API on 2018.1 + //playerSettings.StaticBatching = TODO + //playerSettings.DynamicBatching = TODO + //PlayerSettings.GetBatchingForPlatform(EditorUserBuildSettings.activeBuildTarget, out pbi.staticBatching, out pbi.dynamicBatching); + } + + private static BuildSettings GetPlayerBuildInfo() + { + var buildSettings = new BuildSettings + { + BuildTarget = EditorUserBuildSettings.activeBuildTarget.ToString(), + DevelopmentPlayer = EditorUserBuildSettings.development, + AndroidBuildSystem = EditorUserBuildSettings.androidBuildSystem.ToString() + }; + return buildSettings; + } + + private void CreateStreamingAssetsFolder() + { + if (!Directory.Exists(Application.streamingAssetsPath)) + { + AssetDatabase.CreateFolder("Assets", "StreamingAssets"); + } + } + + private void CreatePerformanceTestRunJson() + { + string json = JsonUtility.ToJson(m_TestRun, true); + File.WriteAllText(m_TestRunPath, json); + AssetDatabase.Refresh(); +#endif + } +} +#endif \ No newline at end of file diff --git a/PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs.meta b/PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs.meta new file mode 100644 index 00000000..e4b70798 --- /dev/null +++ b/PerformanceTests/UnityPerformanceBenchmark/Assets/Tests/PlaymodeMetadataCollector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 06fe977ac4afab94d9073525b6cdcade +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: