summaryrefslogtreecommitdiffstats
path: root/tools/finditer/finditer.go
blob: f5bf75fae274c1fc79f19d36e3f74d352d065504 (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
54
55
56
57
58
59
60
// Copyright 2026 Rose Hogenson <r@roseh.moe>
//
// This file is part of roseh.moe.
//
// roseh.moe is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// roseh.moe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with roseh.moe. If not,
// see <https://www.gnu.org/licenses/>.

package main

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

	"roseh.moe/pkg/roseh.moe/internal/pwhash"
)

func mustHex(s string) []byte {
	b, err := hex.DecodeString(s)
	if err != nil {
		panic(err)
	}
	return b
}

var salt = mustHex("34539b5dd2591415")

var iter int

func BenchmarkHashIter(b *testing.B) {
	const password = "wavy upend 4z jk linda k zing four atop pi"
	for b.Loop() {
		pwhash.HashIter(password, salt, iter)
	}
}

func main() {
	const targetTime = 5 * time.Second
	for iter = 8192; time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) < targetTime; iter *= 2 {
	}
	lo := iter / 2
	hi := iter
	fmt.Println(lo + sort.Search(hi-lo, func(n int) bool {
		iter = lo + n
		return time.Duration(testing.Benchmark(BenchmarkHashIter).NsPerOp()) > targetTime
	}))
}