package lattice import "testing" // benchTree is a filled in-memory tree: n inserts into one branch with keys // spaced so the tree is shallow and the split path runs. func benchTree(b *testing.B, n int) (*Tree, Key) { b.Helper() tr := NewTree(n) var target Key for i := 0; i < n; i++ { k := Key(uint64(i)*0x9E3779B97F4A7C15 + 1) var rec Record rec.DataLen = 4 rec.Inline[0] = byte(i) if _, err := tr.Insert(Bgrammatical, k, rec); err != nil { b.Fatal(err) } if i == n/2 { target = k } } return tr, target } func BenchmarkWalk(b *testing.B) { tr, target := benchTree(b, 46000) b.ResetTimer() for i := 0; i < b.N; i++ { _, _, found, err := tr.Walk(Bgrammatical, target) if err != nil || !found { b.Fatal("miss") } } } func BenchmarkWalkMiss(b *testing.B) { tr, _ := benchTree(b, 46000) miss := Key(0xDEADBEEF) b.ResetTimer() for i := 0; i < b.N; i++ { if _, _, found, _ := tr.Walk(Bgrammatical, miss); found { b.Fatal("unexpected hit") } } } func BenchmarkInsert(b *testing.B) { tr := NewTree(b.N + C) b.ResetTimer() for i := 0; i < b.N; i++ { var rec Record rec.DataLen = 1 if _, err := tr.Insert(Bgrammatical, Key(uint64(i)*2+1), rec); err != nil { b.Fatal(err) } } }