package main import ( "fmt" "math/rand/v2" "os" "sort" "testing" "time" "gitlab.com/rhogenson/roseh.moe/internal/cryptoutil" ) var ( iterations int salt = func() []byte { r := rand.New(new(rand.PCG)) salt := make([]byte, cryptoutil.SaltSize) for i := range salt { salt[i] = byte(r.IntN(256)) } return salt }() ) func BenchmarkHashIter(b *testing.B) { const password = "oboe shortness ether ideology undesired fresh freezable catching mashing glimpse" for b.Loop() { cryptoutil.HashIter(password, salt, iterations) } } func run() error { const targetDuration = 2 * time.Second for iterations = 8192; time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) < targetDuration; iterations *= 2 { } lo := iterations / 2 hi := iterations fmt.Println(lo + sort.Search(hi-lo, func(i int) bool { iterations = lo + i return time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) > targetDuration })) return nil } func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }