import { beforeEach, describe, expect, it, } from "vitest"; import { MemDB, } from "../db/mem-db.js"; import { createTestDb, type TestDbContext, } from "../db/test-db-factory.js"; import { ImmutableTree, } from "../immutable-tree.js"; import { MutableTree, } from "../mutable-tree.js"; import { createStatistics, } from "../types/options.js"; import { stringToBytes, } from "../utils/bytes.js"; import { NodeDB, } from "./node-db.js"; /** * Tests for statistics tracking * Based on Go IAVL v1.3.5 options.go Statistics */ describe("Statistics Tracking", () => { let ctx: TestDbContext; let ndb: NodeDB; let tree: MutableTree; let stats: ReturnType; beforeEach(async () => { ctx = await createTestDb(); stats = createStatistics(); ndb = new NodeDB(ctx.db, 100, { statistics: stats, }); await ndb.initialize(); const workingTree = new ImmutableTree(null, ndb, 0n); tree = new MutableTree(ndb, workingTree, true); }); afterEach(async () => { await ctx.cleanup(); }); describe("node cache statistics", () => { it("should track cache hits and misses for getNode", async () => { // Add some data and save to create nodes await tree.set(stringToBytes("key1"), stringToBytes("value1")); const { version, } = await tree.saveVersion(); // Get the root node key const rootKey = await ndb.getRoot(version); expect(rootKey).not.toBeNull(); // Create a new NodeDB to ensure clean cache const db2 = new MemDB(); // Copy data from old db to new db const iter = await ctx.db.iterator(); while (iter.valid()) { await db2.set(iter.key(), iter.value()); iter.next(); } iter.close(); const stats2 = createStatistics(); const ndb2 = new NodeDB(db2, 100, { statistics: stats2, }); await ndb2.initialize(); // First access - should be cache miss await ndb2.getNode(rootKey!); expect(stats2.cacheMissCnt).toBe(1); expect(stats2.cacheHitCnt).toBe(0); // Second access - should be cache hit await ndb2.getNode(rootKey!); expect(stats2.cacheHitCnt).toBe(1); expect(stats2.cacheMissCnt).toBe(1); }); it("should track cache hits for repeated node access", async () => { // Create a tree with multiple nodes for (let i = 0; i < 10; i++) { await tree.set(stringToBytes(`key${i}`), stringToBytes(`value${i}`)); } await tree.saveVersion(); // Reset statistics stats.cacheHitCnt = 0; stats.cacheMissCnt = 0; // Access same key multiple times for (let i = 0; i < 5; i++) { await tree.get(stringToBytes("key5")); } // Should have more cache hits than misses expect(stats.cacheHitCnt).toBeGreaterThan(stats.cacheMissCnt); }); }); describe("fast node cache statistics", () => { it("should track fast cache hits and misses", async () => { // Enable fast storage by setting version metadata const versionKey = new Uint8Array([0x6d, ...stringToBytes("storage_version")]); await ctx.db.set(versionKey, stringToBytes("1.1.0-0")); // Reinitialize NodeDB to pick up fast storage version ndb = new NodeDB(ctx.db, 100, { statistics: stats, skipFastStorageUpgrade: false, }); await ndb.initialize(); const workingTree2 = new ImmutableTree(null, ndb, 0n); const tree2 = new MutableTree(ndb, workingTree2, false); // Add data with fast storage await tree2.set(stringToBytes("fastkey1"), stringToBytes("fastvalue1")); await tree2.saveVersion(); // Reset statistics stats.fastCacheHitCnt = 0; stats.fastCacheMissCnt = 0; // First getFastNode - should increment counters await ndb.getFastNode(stringToBytes("fastkey1")); const firstMissCount = stats.fastCacheMissCnt; expect(firstMissCount).toBeGreaterThanOrEqual(0); // Second getFastNode - should have cache hit await ndb.getFastNode(stringToBytes("fastkey1")); expect(stats.fastCacheHitCnt).toBeGreaterThanOrEqual(0); // At least one of the accesses should have triggered a counter update expect(stats.fastCacheHitCnt + stats.fastCacheMissCnt).toBeGreaterThan(0); }); }); describe("statistics retrieval", () => { it("should retrieve statistics from NodeDB", () => { const retrievedStats = ndb.getStatistics(); expect(retrievedStats).toBeDefined(); expect(retrievedStats).toBe(stats); expect(retrievedStats?.cacheHitCnt).toBe(stats.cacheHitCnt); }); it("should return undefined if statistics not enabled", async () => { const db2 = new MemDB(); const ndb2 = new NodeDB(db2, 100, { // No statistics provided }); await ndb2.initialize(); const retrievedStats = ndb2.getStatistics(); expect(retrievedStats).toBeUndefined(); }); }); describe("statistics counters", () => { it("should start at zero", () => { const newStats = createStatistics(); expect(newStats.cacheHitCnt).toBe(0); expect(newStats.cacheMissCnt).toBe(0); expect(newStats.fastCacheHitCnt).toBe(0); expect(newStats.fastCacheMissCnt).toBe(0); }); it("should increment independently", () => { const newStats = createStatistics(); newStats.cacheHitCnt++; expect(newStats.cacheHitCnt).toBe(1); expect(newStats.cacheMissCnt).toBe(0); newStats.fastCacheMissCnt++; expect(newStats.fastCacheMissCnt).toBe(1); expect(newStats.cacheHitCnt).toBe(1); }); }); });