tind/examples/collisions.go

59 lines
1.3 KiB
Go
Raw Permalink Normal View History

2024-02-24 23:53:14 +00:00
package main
import (
2024-03-03 15:44:42 +00:00
"sync"
2024-02-24 23:53:14 +00:00
2024-04-07 18:27:05 +00:00
log "git.staur.ca/stobbsm/simplelog"
2024-02-24 23:53:14 +00:00
"git.staur.ca/stobbsm/tind"
)
var (
2024-04-07 18:27:05 +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-04-07 18:27:05 +00:00
log.Info("examples.CollisionChecks").Msg("Using multiple configurations, test for collisions multiple times")
sizes := []int{4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
2024-03-03 15:44:42 +00:00
end := len(RuneSet)
2024-04-07 18:27:05 +00:00
2024-03-03 15:44:42 +00:00
for i := 4; i < end; i++ {
for _, v := range sizes {
v := v
2024-04-07 18:27:05 +00:00
rs := RuneSet[:i]
2024-03-03 15:44:42 +00:00
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))
}
2024-04-07 18:27:05 +00:00
log.Info("examples.collisionCheck").
Uint64("average iterations", total/runs).
Int("runesize", runesize).
Int("size", size).
Send()
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{}{}
}
}