package oltp
import (
"fmt"
"path/filepath"
"sort"
"testing"
"time"
)
// BenchmarkSecretsByShape measures what a login card costs the server
// before its signature is compared, by the shape of the name it claims.
func BenchmarkSecretsByShape(b *testing.B) {
benchShapes(b, func(db *DB, name string) { db.Secrets("alpha", name) })
}
// BenchmarkLetterlessByShape is the same for the push refusal's second look.
func BenchmarkLetterlessByShape(b *testing.B) {
benchShapes(b, func(db *DB, name string) { db.LetterlessSecrets("alpha", name) })
}
func benchShapes(b *testing.B, call func(db *DB, name string)) {
for _, shape := range []struct {
name string
tokens int
known bool
}{
{"unknown", 0, false},
{"known-no-token", 0, true},
{"known-1-token", 1, true},
{"known-4-tokens", 4, true},
{"known-8-tokens", 8, true},
} {
b.Run(shape.name, func(b *testing.B) {
db := benchDB(b)
if shape.known {
if err := db.AddAccount("jan", "x", false); err != nil {
b.Fatal(err)
}
if err := db.Grant("jan", "alpha", Write); err != nil {
b.Fatal(err)
}
for i := 0; i < shape.tokens; i++ {
if _, _, err := db.MintToken("jan", TokenSpec{Name: fmt.Sprintf("t%d", i), Scopes: []string{ScopeSync}}); err != nil {
b.Fatal(err)
}
}
}
name := "jan"
if !shape.known {
name = "ghost"
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
call(db, name)
}
})
}
}
func benchDB(b testing.TB) *DB {
b.Helper()
dir := b.TempDir()
db, err := Open(filepath.Join(dir, "oltp.db"))
if err != nil {
b.Fatal(err)
}
b.Cleanup(func() { db.Close() })
if err := db.AddRepo("alpha", filepath.Join(dir, "alpha"), "", "public"); err != nil {
b.Fatal(err)
}
db.SyncProjectCode = func(repo string) (string, bool) { return "CODE-" + repo, true }
return db
}
// What a login card costs the server before its signature is compared
// must not say whether the name holds a credential, or how many tokens
// it holds. The shapes are timed interleaved and compared by median, with
// a margin far wider than scheduling noise and far narrower than the
// per-token cost this replaced.
func TestSecretsCostDoesNotTellTheNameApart(t *testing.T) {
db := benchDB(t)
for _, n := range []struct {
name string
tokens int
}{{"one", 1}, {"many", 8}} {
if err := db.AddAccount(n.name, "x", false); err != nil {
t.Fatal(err)
}
if err := db.Grant(n.name, "alpha", Write); err != nil {
t.Fatal(err)
}
for i := 0; i < n.tokens; i++ {
if _, _, err := db.MintToken(n.name, TokenSpec{Name: fmt.Sprintf("t%d", i), Scopes: []string{ScopeSync}}); err != nil {
t.Fatal(err)
}
}
}
names := []string{"ghost", "one", "many"}
samples := map[string][]time.Duration{}
for i := 0; i < 60; i++ {
for _, name := range names {
start := time.Now()
db.Secrets("alpha", name)
samples[name] = append(samples[name], time.Since(start))
}
}
median := func(name string) time.Duration {
d := samples[name]
sort.Slice(d, func(i, j int) bool { return d[i] < d[j] })
return d[len(d)/2]
}
base := median("ghost")
for _, name := range names[1:] {
if got := median(name); got > base*2 {
t.Errorf("FAIL: a name with %s token(s) costs %v against %v for no credential at all", name, got, base)
}
}
if !t.Failed() {
t.Log("ok")
}
}