forked from mtusi2026/mtusi2026lab7
70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package password
|
|
|
|
import (
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func BenchmarkArgonLogin(b *testing.B) {
|
|
workers, e := strconv.Atoi(os.Getenv("KDF_CONCURRENCY"))
|
|
if e != nil || workers < 1 || workers > 4 {
|
|
b.Fatal("explicit concurrency1..4 required")
|
|
}
|
|
a := Argon{}
|
|
hash, e := a.Hash("synthetic-benchmark-password")
|
|
if e != nil {
|
|
b.Fatal(e)
|
|
}
|
|
var next atomic.Int64
|
|
var group sync.WaitGroup
|
|
var mu sync.Mutex
|
|
durations := []float64{}
|
|
var failures atomic.Int64
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < workers; i++ {
|
|
group.Add(1)
|
|
go func() {
|
|
defer group.Done()
|
|
for next.Add(1) <= int64(b.N) {
|
|
start := time.Now()
|
|
if !a.Verify("synthetic-benchmark-password", hash) {
|
|
failures.Add(1)
|
|
}
|
|
ms := float64(time.Since(start)) / float64(time.Millisecond)
|
|
mu.Lock()
|
|
durations = append(durations, ms)
|
|
mu.Unlock()
|
|
}
|
|
}()
|
|
}
|
|
group.Wait()
|
|
b.StopTimer()
|
|
if failures.Load() != 0 {
|
|
b.Fatal("verification failure")
|
|
}
|
|
sort.Float64s(durations)
|
|
if len(durations) > 0 {
|
|
b.ReportMetric(durations[(len(durations)-1)*95/100], "p95-ms")
|
|
}
|
|
if raw, e := os.ReadFile("/proc/self/status"); e == nil {
|
|
for _, line := range strings.Split(string(raw), "\n") {
|
|
if strings.HasPrefix(line, "VmHWM:") {
|
|
parts := strings.Fields(line)
|
|
if len(parts) > 1 {
|
|
rss, e := strconv.ParseFloat(parts[1], 64)
|
|
if e == nil {
|
|
b.ReportMetric(rss/1024, "peak-RSS-MiB")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|