Benchmark performance test (#1661)

* benchmark performance test

* adding hooks for performance test

* using hooks to measure time of Lateupdate

moving code to setup and teardown
This commit is contained in:
James Frowen 2020-04-06 08:49:46 +01:00 committed by GitHub
parent 82d24463f6
commit 52e91e7d1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 160 additions and 2 deletions

View File

@ -573571,7 +573571,7 @@ MonoBehaviour:
m_GameObject: {fileID: 1282001517}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8aab4c8111b7c411b9b92cf3dbc5bd4e, type: 3}
m_Script: {fileID: 11500000, guid: 30267752aaf3a68419dac343faaf0bb9, type: 3}
m_Name:
m_EditorClassIdentifier:
dontDestroyOnLoad: 1

View File

@ -0,0 +1,25 @@
using System;
using Mirror;
namespace Mirror.Examples
{
public class BenchmarkNetworkManager : NetworkManager
{
/// <summary>
/// hook for benchmarking
/// </summary>
public Action BeforeLateUpdate;
/// <summary>
/// hook for benchmarking
/// </summary>
public Action AfterLateUpdate;
public override void LateUpdate()
{
BeforeLateUpdate?.Invoke();
base.LateUpdate();
AfterLateUpdate?.Invoke();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 30267752aaf3a68419dac343faaf0bb9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,110 @@
using System.Collections;
using System.Diagnostics;
using Mirror;
using Mirror.Examples;
using NUnit.Framework;
using Unity.PerformanceTesting;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace Tests
{
[Category("Performance")]
[Category("Benchmark")]
public class BenchmarkPerformance
{
const string ScenePath = "Assets/Mirror/Examples/Benchmarks/10k/Scenes/Scene.unity";
const float Warmup = 1f;
const int MeasureCount = 120;
readonly SampleGroupDefinition NetworkManagerSample = new SampleGroupDefinition("NetworkManagerLateUpdate", SampleUnit.Millisecond, AggregationType.Average);
readonly Stopwatch stopwatch = new Stopwatch();
bool captureMeasurement;
void BeforeLateUpdate()
{
if (!captureMeasurement)
return;
stopwatch.Start();
}
void AfterLateUpdate()
{
if (!captureMeasurement)
return;
stopwatch.Stop();
double time = stopwatch.Elapsed.TotalMilliseconds;
Measure.Custom(NetworkManagerSample, time);
stopwatch.Reset();
}
[UnitySetUp]
public IEnumerator SetUp()
{
captureMeasurement = false;
// load scene
yield return EditorSceneManager.LoadSceneAsyncInPlayMode(ScenePath, new LoadSceneParameters { loadSceneMode = LoadSceneMode.Additive });
Scene scene = SceneManager.GetSceneByPath(ScenePath);
SceneManager.SetActiveScene(scene);
// wait for NetworkManager awake
yield return null;
// load host
NetworkManager.singleton.StartHost();
BenchmarkNetworkManager benchmarker = NetworkManager.singleton as BenchmarkNetworkManager;
if (benchmarker == null)
{
Assert.Fail("Could not find Benchmarker");
yield break;
}
benchmarker.BeforeLateUpdate = BeforeLateUpdate;
benchmarker.AfterLateUpdate = AfterLateUpdate;
}
[UnityTearDown]
public IEnumerator TearDown()
{
// run benchmark
yield return Measure.Frames().MeasurementCount(MeasureCount).Run();
// shutdown
GameObject go = NetworkManager.singleton.gameObject;
NetworkManager.Shutdown();
// unload scene
Scene scene = SceneManager.GetSceneByPath(ScenePath);
yield return SceneManager.UnloadSceneAsync(scene);
if (go != null)
{
UnityEngine.Object.Destroy(go);
}
}
[UnityTest]
#if UNITY_2019_2_OR_NEWER
[Performance]
#else
[PerformanceUnityTest]
#endif
public IEnumerator Benchmark10k()
{
// warmup
yield return new WaitForSecondsRealtime(Warmup);
captureMeasurement = true;
for (int i = 0; i < MeasureCount; i++)
{
yield return null;
}
captureMeasurement = false;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ae7696becbe164441b0402b40e9cbd61
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,7 +4,8 @@
"Unity.PerformanceTesting",
"Mirror",
"Mirror.Components",
"Mirror.Tests.Common"
"Mirror.Tests.Common",
"Mirror.Examples"
],
"optionalUnityReferences": [
"TestAssemblies"