tind/examples/collisions.go

55 lines
1.2 KiB
Go
Raw Normal View History

2024-02-24 23:53:14 +00:00
package main
import (
"log"
2024-03-03 15:44:42 +00:00
"sync"
2024-02-24 23:53:14 +00:00
"git.staur.ca/stobbsm/tind"
)
var (
2024-03-03 15:44:42 +00:00
RuneSet = []rune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}/';:.>,<`~")
2024-02-24 23:53:14 +00:00
)
2024-03-03 15:44:42 +00:00
const runs uint64 = 10000
// CollisionChecks runs different tests to check for collisions, and get's the average
2024-02-24 23:53:14 +00:00
func CollisionChecks() {
2024-03-03 15:44:42 +00:00
wg := new(sync.WaitGroup)
2024-02-24 23:53:14 +00:00
log.Println("Using multiple configurations, test for collisions multiple times")
2024-03-03 15:44:42 +00:00
sizes := []int{4,5,6,7,8,9,10,11,12,13,14,15,16}
end := len(RuneSet)
for i := 4; i < end; i++ {
for _, v := range sizes {
v := v
rs :=RuneSet[:i]
wg.Add(1)
go collisionCheck(v, rs, wg, len(rs))
}
2024-02-24 23:53:14 +00:00
}
2024-03-03 15:44:42 +00:00
wg.Wait()
}
2024-02-24 23:53:14 +00:00
2024-03-03 15:44:42 +00:00
func collisionCheck(size int, runeset []rune, wg *sync.WaitGroup, runesize int) {
defer wg.Done()
var total uint64
for i := uint64(0); i < runs; i++ {
total += runCheck(tind.New().WithSize(size).WithRuneset(runeset))
}
log.Printf("runset size: %d, id size: %d, average iterations before collision: %d", runesize, size, total/runs)
2024-02-24 23:53:14 +00:00
}
func runCheck(tconfig *tind.Config) uint64 {
var i uint64
tmap := make(map[string]struct{})
for {
i++
t := tconfig.Gen()
if _, ok := tmap[t.String()]; ok {
return i
}
tmap[t.String()] = struct{}{}
}
}