-
Notifications
You must be signed in to change notification settings - Fork 9
/
entity_test.go
164 lines (142 loc) · 2.93 KB
/
entity_test.go
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
package ecs
import (
"math/rand"
"testing"
"time"
"unsafe"
)
func TestEntityIDGenerator_NewID(t *testing.T) {
t.Run("test1", func(t *testing.T) {
e := NewEntityIDGenerator(10, 3)
id1 := e.NewID()
id2 := e.NewID()
id3 := e.NewID()
e.FreeID(id2)
id4 := e.NewID()
e.FreeID(id1)
e.FreeID(id4)
e.FreeID(id3)
var m []Entity
for i := 0; i < 11; i++ {
newID := e.NewID()
m = append(m, newID)
}
for _, id := range m {
e.FreeID(id)
}
})
}
type _EntityTest struct {
seq int32
freeList map[RealID]struct{}
}
func (e *_EntityTest) NewID() Entity {
id := RealID{}
if len(e.freeList) > 0 {
for i, _ := range e.freeList {
id = i
delete(e.freeList, i)
}
} else {
id = RealID{index: e.seq, reuse: 0}
e.seq++
}
return id.ToEntity()
}
func (e *_EntityTest) FreeID(id Entity) {
real := *(*RealID)(unsafe.Pointer(&id))
e.freeList[real] = struct{}{}
}
func BenchmarkEntityIDGenerator_New(b *testing.B) {
e := NewEntityIDGenerator(1024, 10)
idmap := map[Entity]struct{}{}
e2 := &_EntityTest{freeList: map[RealID]struct{}{}}
idmap2 := map[Entity]struct{}{}
for i := 0; i < 10000; i++ {
id := e.NewID()
idmap[id] = struct{}{}
id = e2.NewID()
idmap2[id] = struct{}{}
}
b.Run("map", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e.NewID()
}
})
b.Run("gen", func(b *testing.B) {
for i := 0; i < b.N; i++ {
e2.NewID()
}
})
}
func TestEntityIDGenerator_Free(t *testing.T) {
size := 100000
e := NewEntityIDGenerator(1024, 100)
idmap := map[Entity]struct{}{}
for i := 0; i < size; i++ {
id := e.NewID()
idmap[id] = struct{}{}
}
e2 := &_EntityTest{freeList: map[RealID]struct{}{}}
idmap2 := map[Entity]struct{}{}
for i := 0; i < size; i++ {
id := e2.NewID()
idmap2[id] = struct{}{}
}
t.Run("gen", func(t *testing.T) {
start := time.Now()
for i, _ := range idmap {
e.FreeID(i)
}
println(time.Since(start).Nanoseconds() / int64(size))
})
t.Run("map", func(t *testing.T) {
start := time.Now()
for i, _ := range idmap2 {
e2.FreeID(i)
}
println(time.Since(start).Nanoseconds() / int64(size))
})
}
func BenchmarkEntityIDGenerator_NewFreeRandom(b *testing.B) {
e := NewEntityIDGenerator(1024, 100)
idmap := map[Entity]struct{}{}
e2 := &_EntityTest{freeList: map[RealID]struct{}{}}
idmap2 := map[Entity]struct{}{}
for i := 0; i < 1000000; i++ {
id := e.NewID()
idmap[id] = struct{}{}
id = e2.NewID()
idmap2[id] = struct{}{}
}
b.Run("map", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r := rand.Intn(100)
if r%2 == 0 {
id := e.NewID()
idmap[id] = struct{}{}
} else {
for i2, _ := range idmap {
e.FreeID(i2)
delete(idmap, i2)
break
}
}
}
})
b.Run("gen", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r := rand.Intn(100)
if r%2 == 0 {
id := e2.NewID()
idmap[id] = struct{}{}
} else {
for i2, _ := range idmap {
e2.FreeID(i2)
delete(idmap2, i2)
break
}
}
}
})
}