-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.go
More file actions
218 lines (184 loc) · 5.96 KB
/
query_test.go
File metadata and controls
218 lines (184 loc) · 5.96 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ecs
import (
"math/rand"
"reflect"
"sort"
"testing"
)
func TestFilter_Run(t *testing.T) {
w := NewWorld()
// Create 10 entities.
var entities [10]Entity
for i := range entities {
entities[i] = NewEntity(w)
}
// Create 2 Components.
c1 := NewComponent(w)
c2 := NewComponent(w)
// Add Components to entities.
for i, e := range entities[:5] {
SetComp(w, e, c1, i)
}
for i, e := range entities[3:7] {
SetComp(w, e, c2, i+3)
}
// id: [0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
filters := [][]Component{{c1}, {c2}, {c1, c2}}
var wants [][]Entity
testAll := func(t *testing.T) {
var result []Entity
for i, want := range wants {
result = result[:0]
QueryAll(filters[i]...).Run(w, func(entities []Entity, data []any) {
result = append(result, entities...)
})
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
if !reflect.DeepEqual(result, want) {
t.Errorf("get: %v, want: %v", result, want)
}
}
}
testAny := func(t *testing.T) {
var result []Entity
for i, want := range wants {
result = result[:0]
QueryAny(filters[i]...).Run(w, func(entities []Entity, data []any) {
result = append(result, entities...)
})
sort.Slice(result, func(i, j int) bool { return result[i] < result[j] })
if !reflect.DeepEqual(result, want) {
t.Errorf("get: %v, want: %v", result, want)
}
}
}
wants = [][]Entity{{entities[0], entities[1], entities[2], entities[3], entities[4]}, {entities[3], entities[4], entities[5], entities[6]}, {entities[3], entities[4]}}
t.Run("All", testAll)
wants = [][]Entity{{entities[0], entities[1], entities[2], entities[3], entities[4]}, {entities[3], entities[4], entities[5], entities[6]}, {entities[0], entities[1], entities[2], entities[3], entities[4], entities[5], entities[6]}}
t.Run("Any", testAny)
// change the entities
SetComp(w, entities[6], c1, 6)
DelComp(w, entities[3], c2)
// id: [0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 6 ]
// c2: [ _ 4 5 6 ]
wants = [][]Entity{{entities[0], entities[1], entities[2], entities[3], entities[4], entities[6]}, {entities[4], entities[5], entities[6]}, {entities[4], entities[6]}}
t.Run("All", testAll)
wants = [][]Entity{{entities[0], entities[1], entities[2], entities[3], entities[4], entities[6]}, {entities[4], entities[5], entities[6]}, {entities[0], entities[1], entities[2], entities[3], entities[4], entities[5], entities[6]}}
t.Run("Any", testAny)
c3 := NewComponent(w)
for i, e := range entities[5:8] {
SetComp(w, e, c3, i+5)
}
// id: [0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 6 ]
// c2: [ _ 4 5 6 ]
// c3: [ 5 6 7 ]
wants = [][]Entity{{entities[0], entities[1], entities[2], entities[3], entities[4], entities[6]}, {entities[4], entities[5], entities[6]}, {entities[4], entities[6]}}
t.Run("All", testAll)
wants = [][]Entity{{entities[0], entities[1], entities[2], entities[3], entities[4], entities[6]}, {entities[4], entities[5], entities[6]}, {entities[0], entities[1], entities[2], entities[3], entities[4], entities[5], entities[6]}}
t.Run("Any", testAny)
}
func TestFilter_Cache(t *testing.T) {
w := NewWorld()
var entities [10]Entity
for i := range entities {
entities[i] = NewEntity(w)
}
c1 := NewComponent(w)
for i, e := range entities[:5] {
SetComp(w, e, c1, i)
}
c2 := NewComponent(w)
for i, e := range entities[3:7] {
SetComp(w, e, c2, i+3)
}
// id: [0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 ]
// c2: [ 3 4 5 6 ]
// Create the cached query
queryBoth := QueryAll(c1, c2).Cache(w)
var result []int
var want []int
judge := func() {
result = result[:0]
queryBoth.Run(func(entities []Entity, data []any) {
result = append(result, *data[0].(*[]int)...)
})
sort.Ints(result)
if !reflect.DeepEqual(result, want) {
t.Errorf("get: %v, want: %v", result, want)
}
}
// Test the basic query
want = []int{3, 4}
judge()
// Test if the cached query gets up to date when entities are moved
SetComp(w, entities[6], c1, 6)
DelComp(w, entities[3], c2)
// id: [0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 6 ]
// c2: [ _ 4 5 6 ]
want = []int{4, 6}
judge()
// Test if the cached query gets up to date when a new archetype is created.
c3 := NewComponent(w)
for i, e := range entities[5:8] {
SetComp(w, e, c3, i+5)
}
// id: [0 1 2 3 4 5 6 7 8 9]
// c1: [0 1 2 3 4 6 ]
// c2: [ _ 4 5 6 ]
// c3: [ 5 6 7 ]
want = []int{4, 6}
judge()
}
func BenchmarkFilter_All(b *testing.B) {
const EntityCount = 1_000_000
const ComponentCount = 16
const QueryCount = 3
w := NewWorld()
var components [ComponentCount]Component
for i := range components {
components[i] = NewComponent(w)
}
// count of tables before creating entities
tableCount := len(w.Archetypes)
for i := 0; i < EntityCount; i++ {
e := NewEntity(w)
coins := rand.Int() // we know len(Components) < bitsOf(int)
for i, c := range components {
if coins&(1<<i) != 0 {
AddComp(w, e, c)
}
}
}
b.Logf("entities created: %d (w/%d randomized Components)", EntityCount, ComponentCount)
b.Logf("tables created : %d", len(w.Archetypes)-tableCount)
b.Logf("setup time : %v", b.Elapsed())
b.Logf("queriying for %d Components", QueryCount)
rand.Shuffle(len(components), func(i, j int) {
components[i], components[j] = components[j], components[i]
})
b.Run("uncached", func(b *testing.B) {
var tableMatched int64
for i := 0; i < b.N; i++ {
QueryAll(components[:QueryCount]...).Run(w, func(entities []Entity, data []any) {
tableMatched++
})
}
b.ReportMetric(float64(b.Elapsed().Nanoseconds()/(tableMatched)), "ns/table")
})
b.Run("cached", func(b *testing.B) {
cachedQuery := QueryAll(components[:QueryCount]...).Cache(w)
b.ResetTimer()
var tableMatched int64
for i := 0; i < b.N; i++ {
cachedQuery.Run(func(entities []Entity, data []any) {
tableMatched++
})
}
b.ReportMetric(float64(b.Elapsed().Nanoseconds()/(tableMatched)), "ns/table")
})
}