generated from HibiscusCollective/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(maps): implement control bench test
- Loading branch information
1 parent
729bfdb
commit 4c02c92
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
go 1.23 | ||
|
||
use ./maps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
!bench |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package bench | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jaswdr/faker/v2" | ||
) | ||
|
||
func BenchmarkInvertControl(b *testing.B) { | ||
fake := faker.New() | ||
|
||
testData := generateTestData(4, 16, generateFakesMap(fake.Int64, fake.Lorem().Word)) | ||
|
||
for name, items := range testData { | ||
b.Run(name, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
b.RunParallel(func(pb *testing.PB) { | ||
inverted := make(map[int64]string) | ||
for key, val := range items { | ||
inverted[key] = val | ||
} | ||
}) | ||
|
||
b.ReportAllocs() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package bench | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gofrs/uuid/v5" | ||
"github.com/jaswdr/faker/v2" | ||
) | ||
|
||
type character struct { | ||
Name string | ||
Level int | ||
Health float64 | ||
Inventory []string | ||
Skills map[string]int | ||
IsAlive bool | ||
} | ||
|
||
type generate[K comparable, V any] func(num uint) map[K]V | ||
type testData[K comparable, V any] map[string]map[K]V | ||
|
||
// generateTestData creates a map of test cases for a given map with an increasing number of elements | ||
// A min and max exponent must be specified. The length of the map will be a power of 2. Ex: 2^4 = 16 | ||
func generateTestData[K comparable, V any](minExp, maxExp uint, fn generate[K, V]) testData[K, V] { | ||
data := make(testData[K, V], maxExp-minExp+1) | ||
|
||
for exp := minExp; exp <= maxExp; exp++ { | ||
numChars := 1 << exp | ||
|
||
data[fmt.Sprintf("given %d items", numChars)] = fn(uint(numChars)) | ||
} | ||
|
||
return data | ||
} | ||
|
||
func generateFakesMap[K comparable, V any](key func() K, val func() V) generate[K, V] { | ||
return func(num uint) map[K]V { | ||
fakeMap := make(map[K]V) | ||
|
||
for i := uint(0); i < num; i++ { | ||
fakeMap[key()] = val() | ||
} | ||
|
||
return fakeMap | ||
} | ||
} | ||
|
||
func generateCharacters(num uint) map[uuid.UUID]character { | ||
faker := faker.New() | ||
|
||
chars := make(map[uuid.UUID]character) | ||
for i := uint(0); i < num; i++ { | ||
chars[uuid.Must(uuid.NewV4())] = character{ | ||
Name: faker.Person().Name(), | ||
Level: faker.IntBetween(1, 100), | ||
Health: faker.Float(2, 0, 100), | ||
Inventory: []string{"Sword", "Shield", "Potion"}, | ||
Skills: map[string]int{ | ||
"Strength": faker.IntBetween(1, 20), | ||
"Intelligence": faker.IntBetween(1, 20), | ||
"Spirit": faker.IntBetween(1, 20), | ||
}, | ||
IsAlive: faker.Bool(), | ||
} | ||
} | ||
|
||
return chars | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/HibiscusCollective/go-toolbox/maps | ||
|
||
go 1.23 | ||
|
||
require ( | ||
github.com/gofrs/uuid/v5 v5.3.1 | ||
github.com/jaswdr/faker/v2 v2.3.3 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/gofrs/uuid/v5 v5.3.1 h1:aPx49MwJbekCzOyhZDjJVb0hx3A0KLjlbLx6p2gY0p0= | ||
github.com/gofrs/uuid/v5 v5.3.1/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= | ||
github.com/jaswdr/faker/v2 v2.3.3 h1:0mA+B5YGjqgpOPdDY/72d6pDv7Z/5t6F1XzIfkUfgC4= | ||
github.com/jaswdr/faker/v2 v2.3.3/go.mod h1:ROK8xwQV0hYOLDUtxCQgHGcl10jbVzIvqHxcIDdwY2Q= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
[tools] | ||
go = "1.23.6" | ||
|
||
[tasks.benchmark] | ||
description = "Run benchmarks for all libraries" | ||
run = "go test ./maps/... -bench . -test.benchmem" |