Skip to content

Commit

Permalink
feat: add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Jul 3, 2024
1 parent 5b74e19 commit fa938ab
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion pkg/btree/btree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package btree

import (
"encoding/binary"
"fmt"
"github.com/kevmo314/appendable/pkg/buftest"
"github.com/kevmo314/appendable/pkg/hnsw"
"github.com/kevmo314/appendable/pkg/pagefile"
"github.com/kevmo314/appendable/pkg/pointer"
"io"
"math"
"testing"
)

Expand Down Expand Up @@ -36,7 +38,12 @@ func (m *testMetaPage) write() error {
return nil
}

func newTestMetaPage(t *testing.T, pf *pagefile.PageFile) *testMetaPage {
type Test interface {
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
}

func newTestMetaPage(t Test, pf *pagefile.PageFile) *testMetaPage {
meta := &testMetaPage{pf: pf}
offset, err := pf.NewPage([]byte{0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
Expand Down Expand Up @@ -217,3 +224,38 @@ func TestBTree_SequentialInsertionTest(t *testing.T) {
}
}
}

func BenchmarkBTree(b *testing.B) {
for i := 0; i <= 20; i++ {
numRecords := int(math.Pow(2, float64(i)))

b.Run(fmt.Sprintf("btree search %d_records", numRecords), func(b *testing.B) {
buf := buftest.NewSeekableBuffer()
p, err := pagefile.NewPageFile(buf)
if err != nil {
b.Fatal(err)
}
tree := &BTree{PageFile: p, MetaPage: newTestMetaPage(b, p), VectorDim: 2}

for rec := range numRecords {
if err := tree.Insert(pointer.ReferencedId{Value: hnsw.Id(rec)}, hnsw.Point{float32(rec), float32(rec)}); err != nil {
b.Fatalf("failed to insert record %d", rec)
}
}

q := numRecords / 2
b.ResetTimer()

for i := 0; i < b.N; i++ {
k, _, err := tree.Find(pointer.ReferencedId{Value: hnsw.Id(q)})
if err != nil {
b.Fatalf("failed to find record %d", q)
}

if k.Value != hnsw.Id(q) {
b.Fatalf("expected to find key %d", q)
}
}
})
}
}

0 comments on commit fa938ab

Please sign in to comment.