blob: 746242bbde9dc4d3472ad537a7a2ddd25e993c55 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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)
}
}
|