summaryrefslogtreecommitdiffstats
path: root/tools/finditers/finditers.go
blob: 31a7e33c9f4584e3f1d69fdd12be5ab5249e4200 (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
package main

import (
	"encoding/hex"
	"fmt"
	"sort"
	"testing"
	"time"

	"gitlab.com/rhogenson/roseh.moe/internal/pwhash"
)

func mustHex(t testing.TB, s string) []byte {
	t.Helper()
	b, err := hex.DecodeString(s)
	if err != nil {
		t.Fatalf("Invalid hex %q: %s", s, err)
	}
	return b
}

var iterations int

func BenchmarkHashIter(b *testing.B) {
	const pw = "atypical evasion foyer roulette throng awning stability exchange humorless vowed"
	salt := mustHex(b, "4250af599e07cde7")
	for b.Loop() {
		pwhash.HashIter(pw, salt, iterations)
	}
}

func main() {
	const targetDuration = 5 * time.Second
	for iterations = 4096; 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
	}))
}