From 044e050f8fe05bdc9bdaa6978ccb73c5702610ef Mon Sep 17 00:00:00 2001 From: user_name Date: Sun, 3 Mar 2024 08:44:42 -0700 Subject: [PATCH] new benchmarking method --- examples/collisions.go | 32 +++++++++++++++++++++++++------- examples/default.go | 1 + examples/main.go | 6 +++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/examples/collisions.go b/examples/collisions.go index 61d8e1e..54042ce 100644 --- a/examples/collisions.go +++ b/examples/collisions.go @@ -2,24 +2,42 @@ package main import ( "log" + "sync" "git.staur.ca/stobbsm/tind" ) var ( - longRuneSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{}/';:.>,<`~" - shortRuneSet = "0123456789" + RuneSet = []rune("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=[]{}/';:.>,<`~") ) +const runs uint64 = 10000 + +// CollisionChecks runs different tests to check for collisions, and get's the average func CollisionChecks() { + wg := new(sync.WaitGroup) log.Println("Using multiple configurations, test for collisions multiple times") - - for i := 0; i < 10; i++ { - log.Printf("Check %d", i) - log.Printf("Using default values, collision after %d iterations", runCheck(tind.Gen().Config())) + 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)) + } } + wg.Wait() +} - +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) } func runCheck(tconfig *tind.Config) uint64 { diff --git a/examples/default.go b/examples/default.go index 0053320..560162e 100644 --- a/examples/default.go +++ b/examples/default.go @@ -6,6 +6,7 @@ import ( "git.staur.ca/stobbsm/tind" ) +// GenWithDefaults demonstrates how to generate a TinD using the default configuration func GenWithDefaults() { fmt.Println("Generate 16 different TinD ID's, and print them in both string and byte representations using the default config") diff --git a/examples/main.go b/examples/main.go index 5c7b311..881640e 100644 --- a/examples/main.go +++ b/examples/main.go @@ -3,5 +3,9 @@ package main import "log" func main() { - log.Println("Running the examples") + log.Println("Example: GenWithDefaults") + GenWithDefaults() + + log.Println("Example: CollisionChecks") + CollisionChecks() }