Skip to content

Commit 750bd20

Browse files
authored
[VIndex] Added benchmark for indexing 10k entries (#129)
This gives a target when switching over the MPT implementation. ``` go test -bench=BenchmarkBuild -benchmem ./vindex goos: linux goarch: amd64 pkg: github.com/transparency-dev/incubator/vindex cpu: AMD EPYC 7B12 BenchmarkBuild_InMemory-24 1 1004559670 ns/op 38192304 B/op 588597 allocs/op BenchmarkBuild_OnDisk-24 1 4810209571 ns/op 2268211632 B/op 1541229 allocs/op PASS ok github.com/transparency-dev/incubator/vindex 26.039s ```
1 parent fdb2307 commit 750bd20

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

vindex/map_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/sha256"
2323
"fmt"
2424
"iter"
25+
"math/rand"
2526
"os"
2627
"path"
2728
"sync"
@@ -43,6 +44,9 @@ import (
4344
const (
4445
skey = "PRIVATE+KEY+logandmap+38581672+AXJ0FKWOcO2ch6WC8kP705Ed3Gxu7pVtZLhfHAQwp+FE"
4546
vkey = "logandmap+38581672+Ab/PCr1eCclRPRMBqw/r5An1xO71MCnImLiospEq6b4l"
47+
48+
benchNumEntries = 10000
49+
benchDuplicationRatio = 0.5
4650
)
4751

4852
func TestVerifiableIndex(t *testing.T) {
@@ -338,3 +342,86 @@ func (s *inMemoryTreeSource) Append(leafStr string) {
338342
s.leaves = append(s.leaves, leaf)
339343
s.t.Append(rfc6962.DefaultHasher.HashLeaf(leaf))
340344
}
345+
346+
func runBenchmark(b *testing.B, opts vindex.Options) {
347+
ctx := context.Background()
348+
s, v, err := fnote.NewEd25519SignerVerifier(skey)
349+
if err != nil {
350+
b.Fatal(err)
351+
}
352+
353+
inputLog := &inMemoryTreeSource{
354+
t: testonly.New(rfc6962.DefaultHasher),
355+
leaves: make([][]byte, 0),
356+
s: s,
357+
v: v,
358+
}
359+
360+
rng := rand.New(rand.NewSource(12345))
361+
362+
var uniqueKeys []string
363+
for i := range benchNumEntries {
364+
var key string
365+
if len(uniqueKeys) > 0 && rng.Float64() < benchDuplicationRatio {
366+
key = uniqueKeys[rng.Intn(len(uniqueKeys))]
367+
} else {
368+
key = fmt.Sprintf("key-%d", len(uniqueKeys))
369+
uniqueKeys = append(uniqueKeys, key)
370+
}
371+
inputLog.Append(fmt.Sprintf("%s: %d", key, i))
372+
}
373+
374+
mapFn := func(leaf []byte) [][sha256.Size]byte {
375+
k, _, found := bytes.Cut(leaf, []byte(":"))
376+
if !found {
377+
panic("colon not found")
378+
}
379+
return [][sha256.Size]byte{sha256.Sum256(k)}
380+
}
381+
382+
b.ResetTimer()
383+
b.ReportAllocs()
384+
385+
for b.Loop() {
386+
b.StopTimer()
387+
dir, err := os.MkdirTemp("", "vindex-bench")
388+
if err != nil {
389+
b.Fatal(err)
390+
}
391+
392+
iterCtx, iterCancel := context.WithCancel(ctx)
393+
394+
outputLog, closer, err := vindex.NewOutputLog(iterCtx, path.Join(dir, "outputlog"), s, v, vindex.OutputLogOpts{})
395+
b.Cleanup(func() {
396+
iterCancel()
397+
_ = os.RemoveAll(dir)
398+
if closer != nil {
399+
closer()
400+
}
401+
})
402+
if err != nil {
403+
b.Fatal(err)
404+
}
405+
406+
vi, err := vindex.NewVerifiableIndex(iterCtx, inputLog, mapFn, outputLog, dir, opts)
407+
if err != nil {
408+
b.Fatal(err)
409+
}
410+
411+
b.StartTimer()
412+
if err := vi.Update(iterCtx); err != nil {
413+
b.Fatal(err)
414+
}
415+
if err := vi.Close(); err != nil {
416+
b.Fatal(err)
417+
}
418+
}
419+
}
420+
421+
func BenchmarkBuild_InMemory(b *testing.B) {
422+
runBenchmark(b, vindex.Options{PersistIndex: false})
423+
}
424+
425+
func BenchmarkBuild_OnDisk(b *testing.B) {
426+
runBenchmark(b, vindex.Options{PersistIndex: true})
427+
}

0 commit comments

Comments
 (0)