Skip to content

Commit 327bdf9

Browse files
committed
tapdb: add syncer cache memory usage test
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)
1 parent b2ac78f commit 327bdf9

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tapdb/multiverse_cache_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package tapdb
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
7+
"time"
68

79
"github.com/lightninglabs/taproot-assets/asset"
810
"github.com/lightninglabs/taproot-assets/internal/test"
11+
"github.com/lightninglabs/taproot-assets/mssmt"
912
"github.com/lightninglabs/taproot-assets/universe"
1013
"github.com/stretchr/testify/require"
1114
)
@@ -185,6 +188,48 @@ func genRandomAsset(t *testing.T) *universe.Item {
185188
}
186189
}
187190

191+
// TestSyncerCacheMemoryUsage tests the memory usage of the syncer cache.
192+
func TestSyncerCacheMemoryUsage(t *testing.T) {
193+
for _, numRoots := range []uint64{500, 5_000, 50_000} {
194+
allRoots := make([]universe.Root, numRoots)
195+
start := time.Now()
196+
for i := uint64(0); i < numRoots; i++ {
197+
proofType := universe.ProofTypeIssuance
198+
if test.RandBool() {
199+
proofType = universe.ProofTypeTransfer
200+
}
201+
202+
assetGen := asset.RandGenesis(t, asset.Normal)
203+
id := randUniverseID(
204+
t, test.RandBool(), withProofType(proofType),
205+
)
206+
allRoots[i] = universe.Root{
207+
ID: id,
208+
AssetName: assetGen.Tag,
209+
Node: mssmt.NewComputedBranch(
210+
id.Bytes(), 1,
211+
),
212+
}
213+
}
214+
t.Logf("Generated %d roots in %v", numRoots, time.Since(start))
215+
216+
t.Run(fmt.Sprintf("%d roots", numRoots), func(t *testing.T) {
217+
res := testing.Benchmark(func(b *testing.B) {
218+
b.ReportAllocs()
219+
220+
cache := newSyncerRootNodeCache(true, numRoots)
221+
cache.replaceCache(allRoots)
222+
})
223+
224+
t.Logf("Memory usage for %d roots: %d bytes",
225+
numRoots, res.MemBytes)
226+
t.Logf("Memory usage per root: %d bytes",
227+
res.MemBytes/numRoots)
228+
t.Logf("Benchmark took %v", res.T)
229+
})
230+
}
231+
}
232+
188233
func queryRoots(t *testing.T, multiverse *MultiverseStore,
189234
pageSize int32) []universe.Root {
190235

tapdb/universe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func withProofType(proofType universe.ProofType) universeIDOptFunc {
4040
}
4141
}
4242

43-
func randUniverseID(t *testing.T, forceGroup bool,
43+
func randUniverseID(t testing.TB, forceGroup bool,
4444
optFunctions ...universeIDOptFunc) universe.Identifier {
4545

4646
opts := defaultUniverseIdOptions()

0 commit comments

Comments
 (0)