-
Notifications
You must be signed in to change notification settings - Fork 9
/
optimizer_benchmark_test.go
134 lines (109 loc) · 2.49 KB
/
optimizer_benchmark_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
package ecs
import (
"math/rand"
"testing"
"time"
)
const (
testOptimizerDummyMaxFor = 10
testOptimizerEntityMax = 1000000
)
type __optimizer_Bench_C_1 struct {
Component[__optimizer_Bench_C_1]
Test1 int
}
type __optimizer_Bench_C_2 struct {
Component[__optimizer_Bench_C_2]
Test2 int
}
type __optimizer_Bench_S_1 struct {
System[__optimizer_Bench_S_1]
}
func (t *__optimizer_Bench_S_1) Init(si SystemInitConstraint) {
t.SetRequirements(si, &__optimizer_Bench_C_1{}, &__optimizer_Bench_C_2{})
}
func (t *__optimizer_Bench_S_1) Update(event Event) {
iter := GetComponentAll[__optimizer_Bench_C_1](t)
for c := iter.Begin(); !iter.End(); c = iter.Next() {
c2 := GetRelated[__optimizer_Bench_C_2](t, c.owner)
if c2 == nil {
continue
}
for i := 0; i < testOptimizerDummyMaxFor; i++ {
c.Test1 += i
}
for i := 0; i < testOptimizerDummyMaxFor; i++ {
c2.Test2 += i
}
}
}
type __optimizer_Bench_GameECS struct {
world *SyncWorld
entities []Entity
}
func (g *__optimizer_Bench_GameECS) init() {
println("init")
config := NewDefaultWorldConfig()
g.world = NewSyncWorld(config)
RegisterSystem[__optimizer_Bench_S_1](g.world)
for i := 0; i < testOptimizerEntityMax; i++ {
c := &__optimizer_Bench_C_1{}
e := g.world.newEntity()
g.world.Add(e.Entity(), c)
g.entities = append(g.entities, e.Entity())
}
rand.Seed(0)
rand.Shuffle(len(g.entities), func(i, j int) { g.entities[i], g.entities[j] = g.entities[j], g.entities[i] })
for i := 0; i < testOptimizerEntityMax; i++ {
c := &__optimizer_Bench_C_2{}
g.world.addComponent(g.entities[i], c)
}
}
func BenchmarkNoOptimizer(b *testing.B) {
//go func() {
// http.ListenAndServe(":6060", nil)
//}()
println("start")
game := &__optimizer_Bench_GameECS{}
game.init()
game.world.update()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
game.world.update()
}
}
func BenchmarkWithOptimizer(b *testing.B) {
//go func() {
// http.ListenAndServe(":6060", nil)
//}()
game := &__optimizer_Bench_GameECS{}
game.init()
game.world.update()
game.world.optimize(time.Second*10, true)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
game.world.update()
}
}
func BenchmarkTest(b *testing.B) {
arr := make([]int, 0, 100)
for i, _ := range arr {
arr[i] = i
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
}
}
func BenchmarkTest2(b *testing.B) {
type test struct {
Name string
Age int
}
t := test{Name: "test", Age: 1}
m := map[test]int{t: 1}
for i := 0; i < b.N; i++ {
_ = m[t]
}
}