package main import ( "encoding/hex" "fmt" "os" "sort" "testing" "time" "gitlab.com/rhogenson/roseh.moe/internal/pwhash" ) var ( iterations int salt, _ = hex.DecodeString("3fb84513fc3afcd6d3b230bf9ece91aaae2d2a99da17efbf7de83b21fafe3f08") ) func BenchmarkHashIter(b *testing.B) { const password = "oboe shortness ether ideology undesired fresh freezable catching mashing glimpse" for b.Loop() { pwhash.HashIter(password, salt, iterations) } } func run() error { for iterations = 8192; time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) < 500*time.Millisecond; 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()) > 500*time.Millisecond })) return nil } func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } }