Skip to content

Commit

Permalink
tapdb: add syncer cache memory usage test
Browse files Browse the repository at this point in the history
This commit adds a test that calculates the approximate memory
consumption of a full syncer cache.
The results look pretty good:

=== RUN   TestSyncerCacheMemoryUsage
    multiverse_cache_test.go:214: Generated 500 roots in 26.583318ms
=== RUN   TestSyncerCacheMemoryUsage/500_roots
    multiverse_cache_test.go:224: Memory usage for 500 roots: 139496 bytes
    multiverse_cache_test.go:226: Memory usage per root: 278 bytes
    multiverse_cache_test.go:228: Benchmark took 8.836µs
--- PASS: TestSyncerCacheMemoryUsage/500_roots (0.01s)
    multiverse_cache_test.go:214: Generated 5000 roots in 257.823179ms
=== RUN   TestSyncerCacheMemoryUsage/5000_roots
    multiverse_cache_test.go:224: Memory usage for 5000 roots: 1073384 bytes
    multiverse_cache_test.go:226: Memory usage per root: 214 bytes
    multiverse_cache_test.go:228: Benchmark took 104.568µs
--- PASS: TestSyncerCacheMemoryUsage/5000_roots (0.01s)
    multiverse_cache_test.go:214: Generated 170000 roots in 8.847171795s
=== RUN   TestSyncerCacheMemoryUsage/170000_roots
    multiverse_cache_test.go:224: Memory usage for 170000 roots: 34259176 bytes
    multiverse_cache_test.go:226: Memory usage per root: 201 bytes
    multiverse_cache_test.go:228: Benchmark took 1.820929ms
--- PASS: TestSyncerCacheMemoryUsage/170000_roots (0.08s)
--- PASS: TestSyncerCacheMemoryUsage (9.23s)
  • Loading branch information
guggero committed Nov 14, 2024
1 parent a410232 commit b04ddf1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 45 additions & 0 deletions tapdb/multiverse_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package tapdb

import (
"context"
"fmt"
"testing"
"time"

"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightninglabs/taproot-assets/mssmt"
"github.com/lightninglabs/taproot-assets/universe"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -185,6 +188,48 @@ func genRandomAsset(t *testing.T) *universe.Item {
}
}

// TestSyncerCacheMemoryUsage tests the memory usage of the syncer cache.
func TestSyncerCacheMemoryUsage(t *testing.T) {
for _, numRoots := range []uint64{500, 5_000, 50_000} {
allRoots := make([]universe.Root, numRoots)
start := time.Now()
for i := uint64(0); i < numRoots; i++ {
proofType := universe.ProofTypeIssuance
if test.RandBool() {
proofType = universe.ProofTypeTransfer
}

assetGen := asset.RandGenesis(t, asset.Normal)
id := randUniverseID(
t, test.RandBool(), withProofType(proofType),
)
allRoots[i] = universe.Root{
ID: id,
AssetName: assetGen.Tag,
Node: mssmt.NewComputedBranch(
id.Bytes(), 1,
),
}
}
t.Logf("Generated %d roots in %v", numRoots, time.Since(start))

t.Run(fmt.Sprintf("%d roots", numRoots), func(t *testing.T) {
res := testing.Benchmark(func(b *testing.B) {
b.ReportAllocs()

cache := newSyncerRootNodeCache(true, numRoots)
cache.replaceCache(allRoots)
})

t.Logf("Memory usage for %d roots: %d bytes",
numRoots, res.MemBytes)
t.Logf("Memory usage per root: %d bytes",
res.MemBytes/numRoots)
t.Logf("Benchmark took %v", res.T)
})
}
}

func queryRoots(t *testing.T, multiverse *MultiverseStore,
pageSize int32) []universe.Root {

Expand Down
2 changes: 1 addition & 1 deletion tapdb/universe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func withProofType(proofType universe.ProofType) universeIDOptFunc {
}
}

func randUniverseID(t *testing.T, forceGroup bool,
func randUniverseID(t testing.TB, forceGroup bool,
optFunctions ...universeIDOptFunc) universe.Identifier {

opts := defaultUniverseIdOptions()
Expand Down

0 comments on commit b04ddf1

Please sign in to comment.