Repository: mlange-42/arche
Pull request #14 (2023-02-06 23:26:45)

Exclude test setup from benchmark timing


Patch:
diff --git a/ecs/storage.go b/ecs/storage.go
index c2ba7ce..d7f2402 100644
--- a/ecs/storage.go
+++ b/ecs/storage.go
@@ -137,8 +137,8 @@ func (s *storage) Len() uint32 {
 	return s.len
 }
 
-// ToSlice converts the content of a storage to a slice of structs
-func ToSlice[T any](s storage) []T {
+// toSlice converts the content of a storage to a slice of structs
+func toSlice[T any](s storage) []T {
 	res := make([]T, s.Len())
 	for i := 0; i < int(s.Len()); i++ {
 		ptr := (*T)(s.Get(uint32(i)))


Test patch:
diff --git a/benchmark/add/ggecs_test.go b/benchmark/add/ggecs_test.go
index fde6aa7..cabf087 100644
--- a/benchmark/add/ggecs_test.go
+++ b/benchmark/add/ggecs_test.go
@@ -7,10 +7,12 @@ import (
 )
 
 func addGameEngineEcs(b *testing.B, count int) {
+	b.StopTimer()
 	comps := []ecs.ComponentConfig{
 		{ID: 0, Component: position{}},
 		{ID: 1, Component: rotation{}},
 	}
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		b.StopTimer()
diff --git a/benchmark/iterate/arche_test.go b/benchmark/iterate/arche_test.go
index 17ea575..be55db5 100644
--- a/benchmark/iterate/arche_test.go
+++ b/benchmark/iterate/arche_test.go
@@ -7,6 +7,7 @@ import (
 )
 
 func runArcheQuery(b *testing.B, count int) {
+	b.StopTimer()
 	world := ecs.NewWorld()
 
 	posID := ecs.RegisterComponent[position](&world)
@@ -16,6 +17,7 @@ func runArcheQuery(b *testing.B, count int) {
 		entity := world.NewEntity()
 		world.Add(entity, posID, rotID)
 	}
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		b.StopTimer()
@@ -29,6 +31,7 @@ func runArcheQuery(b *testing.B, count int) {
 }
 
 func runArcheWorld(b *testing.B, count int) {
+	b.StopTimer()
 	world := ecs.NewWorld()
 
 	posID := ecs.RegisterComponent[position](&world)
@@ -38,6 +41,7 @@ func runArcheWorld(b *testing.B, count int) {
 		entity := world.NewEntity()
 		world.Add(entity, posID, rotID)
 	}
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		b.StopTimer()
@@ -51,18 +55,26 @@ func runArcheWorld(b *testing.B, count int) {
 	}
 }
 
-func BenchmarkArcheIterQuery_1000(b *testing.B) {
+func BenchmarkArcheIterQuery_1_000(b *testing.B) {
 	runArcheQuery(b, 1000)
 }
 
-func BenchmarkArcheIterQuery_10000(b *testing.B) {
+func BenchmarkArcheIterQuery_10_000(b *testing.B) {
 	runArcheQuery(b, 10000)
 }
 
-func BenchmarkArcheIterWorld_1000(b *testing.B) {
+func BenchmarkArcheIterQuery_100_000(b *testing.B) {
+	runArcheQuery(b, 100000)
+}
+
+func BenchmarkArcheIterWorld_1_000(b *testing.B) {
 	runArcheWorld(b, 1000)
 }
 
-func BenchmarkArcheIterWorld_10000(b *testing.B) {
+func BenchmarkArcheIterWorld_10_000(b *testing.B) {
 	runArcheWorld(b, 10000)
 }
+
+func BenchmarkArcheIterWorld_100_000(b *testing.B) {
+	runArcheWorld(b, 100000)
+}
diff --git a/benchmark/iterate/ggecs_test.go b/benchmark/iterate/ggecs_test.go
index fe834c3..06aa0d3 100644
--- a/benchmark/iterate/ggecs_test.go
+++ b/benchmark/iterate/ggecs_test.go
@@ -7,6 +7,7 @@ import (
 )
 
 func runGameEngineEcs(b *testing.B, count int) {
+	b.StopTimer()
 	comps := []ecs.ComponentConfig{
 		{ID: 0, Component: position{}},
 		{ID: 1, Component: rotation{}},
@@ -17,6 +18,7 @@ func runGameEngineEcs(b *testing.B, count int) {
 		entity := world.NewEntity()
 		world.Assign(entity, 0, 1)
 	}
+	b.StartTimer()
 
 	filter := world.NewFilter(0, 1)
 
@@ -28,10 +30,14 @@ func runGameEngineEcs(b *testing.B, count int) {
 	}
 }
 
-func BenchmarkGGEcsIter_1000(b *testing.B) {
+func BenchmarkGGEcsIter_1_000(b *testing.B) {
 	runGameEngineEcs(b, 1000)
 }
 
-func BenchmarkGGEcsIter_10000(b *testing.B) {
+func BenchmarkGGEcsIter_10_000(b *testing.B) {
 	runGameEngineEcs(b, 10000)
 }
+
+func BenchmarkGGEcsIter_100_000(b *testing.B) {
+	runGameEngineEcs(b, 100000)
+}
diff --git a/ecs/archetype_test.go b/ecs/archetype_test.go
index 2996129..e872a90 100644
--- a/ecs/archetype_test.go
+++ b/ecs/archetype_test.go
@@ -88,6 +88,7 @@ func TestNewArchetype(t *testing.T) {
 }
 
 func BenchmarkArchetypeAccess_1000(b *testing.B) {
+	b.StopTimer()
 	comps := []componentType{
 		{ID: 0, Type: reflect.TypeOf(position{})},
 		{ID: 1, Type: reflect.TypeOf(rotation{})},
@@ -102,6 +103,7 @@ func BenchmarkArchetypeAccess_1000(b *testing.B) {
 			component{ID: 1, Component: &rotation{3}},
 		)
 	}
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		for j := 0; j < 1000; j++ {
diff --git a/ecs/storage_test.go b/ecs/storage_test.go
index 52ceb17..602ed32 100644
--- a/ecs/storage_test.go
+++ b/ecs/storage_test.go
@@ -44,10 +44,10 @@ func storageAddGet(t *testing.T, s storage) {
 	ret2 = (*testStruct)(s.Get(1))
 	assert.Equal(t, testStruct{1, 1001, true, false}, *ret2, "Manipulating element does not change data")
 
-	assert.Equal(t, []testStruct{{}, {1, 1001, true, false}}, ToSlice[testStruct](s), "Wrong extracted struct slice")
+	assert.Equal(t, []testStruct{{}, {1, 1001, true, false}}, toSlice[testStruct](s), "Wrong extracted struct slice")
 
 	s.Alloc()
-	assert.Equal(t, []testStruct{{}, {1, 1001, true, false}, {}}, ToSlice[testStruct](s), "Wrong extracted struct slice")
+	assert.Equal(t, []testStruct{{}, {1, 1001, true, false}, {}}, toSlice[testStruct](s), "Wrong extracted struct slice")
 }
 
 func TestReflectStorageRemove(t *testing.T) {
@@ -67,11 +67,11 @@ func storageRemove(t *testing.T, s storage) {
 
 	s.Remove(4)
 	assert.Equal(t, uint32(4), s.Len(), "Wrong storage length")
-	assert.Equal(t, []simpleStruct{{0}, {1}, {2}, {3}}, ToSlice[simpleStruct](s), "Wrong slice after remove")
+	assert.Equal(t, []simpleStruct{{0}, {1}, {2}, {3}}, toSlice[simpleStruct](s), "Wrong slice after remove")
 
 	s.Remove(1)
 	assert.Equal(t, uint32(3), s.Len(), "Wrong storage length")
-	assert.Equal(t, []simpleStruct{{0}, {3}, {2}}, ToSlice[simpleStruct](s), "Wrong slice after remove")
+	assert.Equal(t, []simpleStruct{{0}, {3}, {2}}, toSlice[simpleStruct](s), "Wrong slice after remove")
 }
 
 func TestReflectStorageDataSize(t *testing.T) {
@@ -104,12 +104,14 @@ func TestNewReflectStorage(t *testing.T) {
 }
 
 func BenchmarkIterReflectStorage_1000(b *testing.B) {
+	b.StopTimer()
 	ref := testStruct{}
 	s := newStorage(reflect.TypeOf(ref), 128)
 	for i := 0; i < 1000; i++ {
 		s.Add(&testStruct{})
 	}
 	assert.Equal(b, 1000, int(s.Len()))
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		for j := 0; j < int(s.Len()); j++ {
@@ -120,11 +122,13 @@ func BenchmarkIterReflectStorage_1000(b *testing.B) {
 }
 
 func BenchmarkIterSlice_1000(b *testing.B) {
+	b.StopTimer()
 	s := []testStruct{}
 	for i := 0; i < 1000; i++ {
 		s = append(s, testStruct{})
 	}
 	assert.Equal(b, 1000, len(s))
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		for j := 0; j < len(s); j++ {
@@ -135,11 +139,13 @@ func BenchmarkIterSlice_1000(b *testing.B) {
 }
 
 func BenchmarkIterSliceInterface_1000(b *testing.B) {
+	b.StopTimer()
 	s := []interface{}{}
 	for i := 0; i < 1000; i++ {
 		s = append(s, testStruct{})
 	}
 	assert.Equal(b, 1000, len(s))
+	b.StartTimer()
 
 	for i := 0; i < b.N; i++ {
 		for j := 0; j < len(s); j++ {
@@ -213,11 +219,13 @@ func BenchmarkRemoveReflectStorage_1000(b *testing.B) {
 }
 
 func BenchmarkRemoveSlice_1000(b *testing.B) {
+	b.StopTimer()
 	ref := testStruct{}
 	template := make([]testStruct, 0)
 	for i := 0; i < 1000; i++ {
 		template = append(template, ref)
 	}
+	b.StartTimer()
 	for i := 0; i < b.N; i++ {
 		b.StopTimer()
 		s := append([]testStruct{}, template...)
@@ -237,11 +245,13 @@ func BenchmarkRemoveSlice_1000(b *testing.B) {
 }
 
 func BenchmarkRemoveSliceInterface_1000(b *testing.B) {
+	b.StopTimer()
 	ref := testStruct{}
 	template := make([]interface{}, 0)
 	for i := 0; i < 1000; i++ {
 		template = append(template, ref)
 	}
+	b.StartTimer()
 	for i := 0; i < b.N; i++ {
 		b.StopTimer()
 		s := append([]interface{}{}, template...)