-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerformanceTests.cs
196 lines (167 loc) · 6.52 KB
/
PerformanceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
namespace DxMessaging.Tests.Runtime.Benchmarks
{
using System;
using System.Collections;
using System.Diagnostics;
using Core;
using DxMessaging.Core;
using DxMessaging.Core.Extensions;
using NUnit.Framework;
using Scripts.Components;
using Scripts.Messages;
using UnityEngine;
using UnityEngine.TestTools;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
public sealed class PerformanceTests : MessagingTestBase
{
[SetUp]
public override void Setup()
{
base.Setup();
MessagingDebug.LogFunction = (_1, _2) => { };
}
[UnityTest]
public IEnumerator Benchmark()
{
TimeSpan timeout = TimeSpan.FromSeconds(1);
Debug.Log("| Message Tech | Operations / Second |");
Debug.Log("| ------------ | ------------------- |");
ComplexTargetedMessage message = new(Guid.NewGuid());
Stopwatch timer = Stopwatch.StartNew();
RunTest(component => Unity(timer, timeout, component.gameObject, message));
RunTest(component => NormalGameObject(timer, timeout, component, message));
RunTest(component => NormalComponent(timer, timeout, component, message));
RunTest(component => NoCopyGameObject(timer, timeout, component, message));
RunTest(component => NoCopyComponent(timer, timeout, component, message));
SimpleUntargetedMessage untargetedMessage = new();
RunTest(component => NoCopyUntargeted(timer, timeout, component, untargetedMessage));
yield break;
}
private GameObject CreateGameObject()
{
GameObject target = new(
nameof(Benchmark), typeof(EmptyMessageAwareComponent), typeof(SpriteRenderer),
typeof(Rigidbody2D), typeof(CircleCollider2D), typeof(LineRenderer));
_spawned.Add(target);
return target;
}
private void DisplayCount(string testName, int count, TimeSpan timeout)
{
Debug.Log($"| {testName} | {Math.Floor(count / timeout.TotalSeconds):N0} |");
}
private void RunTest(Action<EmptyMessageAwareComponent> test)
{
GameObject go = CreateGameObject();
try
{
test(go.GetComponent<EmptyMessageAwareComponent>());
}
finally
{
_spawned.Remove(go);
Object.Destroy(go);
}
}
private void Unity(Stopwatch timer, TimeSpan timeout, GameObject target, ComplexTargetedMessage message)
{
int count = 0;
var component = target.AddComponent<SimpleMessageAwareComponent>();
component.slowComplexTargetedHandler = () => ++count;
timer.Restart();
do
{
target.SendMessage(nameof(SimpleMessageAwareComponent.HandleSlowComplexTargetedMessage), message);
}
while (timer.Elapsed < timeout);
DisplayCount("Unity", count, timeout);
}
private void NormalGameObject(Stopwatch timer, TimeSpan timeout, EmptyMessageAwareComponent component, ComplexTargetedMessage message)
{
int count = 0;
var token = GetToken(component);
void Handle(ComplexTargetedMessage _)
{
++count;
}
GameObject go = component.gameObject;
token.RegisterGameObjectTargeted<ComplexTargetedMessage>(go, Handle);
timer.Restart();
do
{
message.EmitGameObjectTargeted(go);
}
while (timer.Elapsed < timeout);
DisplayCount("DxMessaging (GameObject) - Normal", count, timeout);
}
private void NormalComponent(Stopwatch timer, TimeSpan timeout, EmptyMessageAwareComponent component, ComplexTargetedMessage message)
{
int count = 0;
var token = GetToken(component);
void Handle(ComplexTargetedMessage _)
{
++count;
}
token.RegisterComponentTargeted<ComplexTargetedMessage>(component, Handle);
timer.Restart();
do
{
message.EmitComponentTargeted(component);
}
while (timer.Elapsed < timeout);
DisplayCount("DxMessaging (Component) - Normal", count, timeout);
}
private void NoCopyGameObject(Stopwatch timer, TimeSpan timeout, EmptyMessageAwareComponent component, ComplexTargetedMessage message)
{
int count = 0;
var token = GetToken(component);
void Handle(ref ComplexTargetedMessage _)
{
++count;
}
GameObject go = component.gameObject;
token.RegisterGameObjectTargeted<ComplexTargetedMessage>(go, Handle);
timer.Restart();
do
{
message.EmitGameObjectTargeted(component.gameObject);
}
while (timer.Elapsed < timeout);
DisplayCount("DxMessaging (GameObject) - No-Copy", count, timeout);
}
private void NoCopyComponent(Stopwatch timer, TimeSpan timeout, EmptyMessageAwareComponent component, ComplexTargetedMessage message)
{
int count = 0;
var token = GetToken(component);
void Handle(ref ComplexTargetedMessage _)
{
++count;
}
token.RegisterComponentTargeted<ComplexTargetedMessage>(component, Handle);
timer.Restart();
do
{
message.EmitComponentTargeted(component);
}
while (timer.Elapsed < timeout);
DisplayCount("DxMessaging (Component) - No-Copy", count, timeout);
}
private void NoCopyUntargeted(Stopwatch timer, TimeSpan timeout, EmptyMessageAwareComponent component, SimpleUntargetedMessage message)
{
int count = 0;
var token = GetToken(component);
void Handle(ref SimpleUntargetedMessage _)
{
++count;
}
token.RegisterUntargeted<SimpleUntargetedMessage>(Handle);
timer.Restart();
do
{
message.EmitUntargeted();
}
while (timer.Elapsed < timeout);
DisplayCount("DxMessaging (Untargeted) - No-Copy", count, timeout);
}
}
}