-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueryTest.cs
163 lines (143 loc) · 3.98 KB
/
QueryTest.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
using System.Collections;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using revecs.Core;
using revecs.Extensions.Generator.Components;
using revecs.Querying;
using Xunit;
using Xunit.Abstractions;
namespace revecs.Tests;
public partial struct ComponentA : ISparseComponent
{
public int Value;
}
public partial struct ComponentB : ISparseComponent
{
public int Value;
}
public partial struct ComponentC : ISparseComponent
{
public int Value;
}
public partial struct MyQuery : IQuery<(
Write<ComponentA> A,
Write<ComponentB> B,
Write<ComponentC> C)>
{
}
public class QueryTest : TestBase
{
public void Test1()
{
var world = new RevolutionWorld();
var compA = ComponentA.Type.GetOrCreate(world);
for (var i = 0; i < 100_000; i++)
{
var ent = world.CreateEntity();
world.AddComponent(ent, compA, default);
}
}
public void Test2()
{
var world = new RevolutionWorld();
for (var i = 0; i < 100_000; i++)
{
var ent = world.CreateEntity();
world.AddComponentA(ent);
}
}
public QueryTest(ITestOutputHelper output) : base(output)
{
}
[Fact]
public void BenchmarkComponent()
{
void Bench(Action ac)
{
GC.Collect();
var sw = new Stopwatch();
sw.Start();
ac();
sw.Stop();
output.WriteLine($"{ac.Method.Name} took {sw.Elapsed.TotalMilliseconds}ms");
}
for (var ok = 0; ok < 50; ok++)
{
output.WriteLine("Direct");
for (var i = 0; i < 4; i++)
{
Bench(Test1);
}
output.WriteLine("Via Extension Methods");
for (var i = 0; i < 4; i++)
{
Bench(Test2);
}
output.WriteLine(" ");
}
}
[Fact]
public void BenchmarkQuery()
{
using var world = new RevolutionWorld();
void Test1()
{
var query = new ArchetypeQuery(world, new[]
{
ComponentA.ToComponentType(world),
ComponentB.ToComponentType(world),
ComponentC.ToComponentType(world),
});
var accessorA = world.AccessSparseSet(query.All[0].UnsafeCast<ComponentA>());
var accessorB = world.AccessSparseSet(query.All[1].UnsafeCast<ComponentB>());
var accessorC = world.AccessSparseSet(query.All[2].UnsafeCast<ComponentC>());
foreach (var entity in query.GetEnumerator())
{
accessorA[entity] = default;
accessorB[entity] = default;
accessorC[entity] = default;
}
}
void Test2()
{
var query = new MyQuery(world);
foreach (var entity in query)
{
entity.A = default;
entity.B = default;
entity.C = default;
}
}
void Bench(Action ac)
{
GC.Collect();
var sw = new Stopwatch();
sw.Start();
ac();
sw.Stop();
output.WriteLine($"{ac.Method.Name} took {sw.Elapsed.TotalMilliseconds}ms");
}
for (var i = 0; i < 10_000; i++)
{
var ent = world.CreateEntity();
world.AddComponentA(ent);
world.AddComponentB(ent);
world.AddComponentC(ent);
}
for (var ok = 0; ok < 50; ok++)
{
output.WriteLine("Direct");
for (var i = 0; i < 4; i++)
{
Bench(Test1);
}
output.WriteLine("Via Extension Methods");
for (var i = 0; i < 4; i++)
{
Bench(Test2);
}
output.WriteLine(" ");
}
}
}