Skip to content

Commit

Permalink
feat(maps): implement control bench test
Browse files Browse the repository at this point in the history
  • Loading branch information
pfouilloux committed Feb 11, 2025
1 parent 729bfdb commit 4c02c92
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@
!licenses
!licenses/**/*.txt

## Allow go workspace
!go.work
!**/go.mod
!**/go.sum
!**/*.go

## Allow subfolders
!.github
!docs
!maps


3 changes: 3 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go 1.23

use ./maps
1 change: 1 addition & 0 deletions maps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!bench
28 changes: 28 additions & 0 deletions maps/bench/bench_test.go
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()
}
})
}
}
68 changes: 68 additions & 0 deletions maps/bench/test_data.go
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
}
8 changes: 8 additions & 0 deletions maps/go.mod
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
)
4 changes: 4 additions & 0 deletions maps/go.sum
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=
4 changes: 4 additions & 0 deletions mise.toml
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"

0 comments on commit 4c02c92

Please sign in to comment.