...
1 package gohan
2
3 import "testing"
4
5 type positionComponent struct {
6 componentID componentID
7
8 X, Y float64
9 }
10
11 func (c *positionComponent) ComponentID() componentID {
12 return c.componentID
13 }
14
15 type velocityComponent struct {
16 componentID componentID
17
18 X, Y float64
19 }
20
21 func (c *velocityComponent) ComponentID() componentID {
22 return c.componentID
23 }
24
25 func BenchmarkComponent(b *testing.B) {
26 Reset()
27
28 e := NewEntity()
29
30 e.AddComponent(&positionComponent{
31 X: 108,
32 Y: 0,
33 })
34 positionComponentID := componentID(1)
35
36 b.StopTimer()
37 b.ResetTimer()
38 b.ReportAllocs()
39 b.StartTimer()
40
41 for i := 0; i < b.N; i++ {
42 _ = e.getComponent(positionComponentID)
43 }
44 }
45
46 func BenchmarkAddComponent(b *testing.B) {
47 Reset()
48
49 e := NewEntity()
50
51 c := &positionComponent{
52 X: 108,
53 Y: 0,
54 }
55
56 b.StopTimer()
57 b.ResetTimer()
58 b.ReportAllocs()
59 b.StartTimer()
60
61 for i := 0; i < b.N; i++ {
62 e.AddComponent(c)
63 }
64 }
65
View as plain text