diff --git a/Assets/MonoInjectorTestScripts/PerformanceTester.cs b/Assets/MonoInjectorTestScripts/PerformanceTester.cs new file mode 100644 index 0000000..31bcaca --- /dev/null +++ b/Assets/MonoInjectorTestScripts/PerformanceTester.cs @@ -0,0 +1,45 @@ +using MonoInjector; +using MonoInjector.Test; +using System.Collections.Generic; +using System.Diagnostics; +using UnityEngine; + +namespace Assets.MonoInjectorTestScripts +{ + public class PerformanceTester : MonoBehaviour + { + public void Test( + List fieldsConsumers, + List properiesConsumers, + List methodConsumers, + List allConsumers, + Stopwatch timer) + { + timer = new Stopwatch(); + timer.Start(); + + foreach (var item in fieldsConsumers) + { + item.Resolve(); + } + + foreach (var item in properiesConsumers) + { + item.Resolve(); + } + + foreach (var item in methodConsumers) + { + item.Resolve(); + } + + foreach (var item in allConsumers) + { + item.Resolve(); + } + + timer.Stop(); + UnityEngine.Debug.Log($"Resolved in {timer.ElapsedMilliseconds} ms."); + } + } +} diff --git a/Assets/MonoInjectorTestScripts/PerformanceTester.cs.meta b/Assets/MonoInjectorTestScripts/PerformanceTester.cs.meta new file mode 100644 index 0000000..a23cc1d --- /dev/null +++ b/Assets/MonoInjectorTestScripts/PerformanceTester.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 6bbb3aabdd04b3346be08afa7cff0bdd \ No newline at end of file diff --git a/Assets/MonoInjectorTestScripts/Usage.cs b/Assets/MonoInjectorTestScripts/Usage.cs index 79984f5..76d26a0 100644 --- a/Assets/MonoInjectorTestScripts/Usage.cs +++ b/Assets/MonoInjectorTestScripts/Usage.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using Assets.MonoInjectorTestScripts; +using System.Collections; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using UnityEngine; @@ -17,7 +19,14 @@ public class Usage : MonoBehaviour private bool _methodConsumer; [SerializeField] private bool _allConsumer; + [SerializeField] + private PerformanceTester _tester; + private List _fieldsConsumers; + private List _properiesConsumers; + private List _methodConsumers; + private List _allConsumers; + private Stopwatch _stopwatch; private void Start() { @@ -30,49 +39,27 @@ private void Start() injected3.Inject(); injected4.Inject(); - var _fieldsConsumers = _fieldsConsumer + _fieldsConsumers = _fieldsConsumer ? Enumerable.Repeat(new FieldsConsumer(), _instancesCount).ToList() : new List(); - var _properiesConsumers = _propertiesConsumer + _properiesConsumers = _propertiesConsumer ? Enumerable.Repeat(new PropertiesConsumer(), _instancesCount).ToList() : new List(); - var _methodConsumers = _methodConsumer + _methodConsumers = _methodConsumer ? Enumerable.Repeat(new MethodConsumer(), _instancesCount).ToList() : new List(); - var _allConsumers = _allConsumer + _allConsumers = _allConsumer ? Enumerable.Repeat(new AllConsumer(), _instancesCount).ToList() : new List(); + _stopwatch = new Stopwatch(); - Stopwatch timer = new Stopwatch(); - int accumulator = 0; - timer.Start(); - - foreach (var item in _fieldsConsumers) - { - item.Resolve(); - accumulator += item.GetSumField(); - } - - foreach (var item in _properiesConsumers) - { - item.Resolve(); - accumulator += item.GetSumProp(); - } - - foreach (var item in _methodConsumers) - { - item.Resolve(); - accumulator += item.GetSumMethod(); - } - - foreach (var item in _allConsumers) - { - item.Resolve(); - accumulator += item.GetSumAll(); - } + StartCoroutine(WaitFrameAndDoTest()); + } - timer.Stop(); - UnityEngine.Debug.Log($"{accumulator} accumulated in: {timer.ElapsedMilliseconds} ms." ); + private IEnumerator WaitFrameAndDoTest() + { + yield return null; + _tester.Test(_fieldsConsumers, _properiesConsumers, _methodConsumers, _allConsumers, _stopwatch); } } } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 7f1c09d..a3b3b68 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -222,6 +222,7 @@ GameObject: - component: {fileID: 1249362804} - component: {fileID: 1249362805} - component: {fileID: 1249362806} + - component: {fileID: 1249362807} m_Layer: 0 m_Name: GameObject m_TagString: Untagged @@ -269,10 +270,23 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: _instancesCount: 100 - _fieldsConsumer: 1 + _fieldsConsumer: 0 _propertiesConsumer: 1 - _methodConsumer: 1 - _allConsumer: 1 + _methodConsumer: 0 + _allConsumer: 0 + _tester: {fileID: 1249362807} +--- !u!114 &1249362807 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1249362802} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6bbb3aabdd04b3346be08afa7cff0bdd, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1660057539 &9223372036854775807 SceneRoots: m_ObjectHideFlags: 0