Skip to content

Commit

Permalink
improve memory test logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mceachen committed Dec 16, 2024
1 parent d2a5dd5 commit d5ac699
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import {
isHidden,
setHidden,
} from "./index.js";
import { isMacOS } from "./platform.js";
import { randomLetters } from "./random.js";
import { validateHidden } from "./test-utils/hidden-tests.js";
import { tmpDirNotHidden } from "./test-utils/platform.js";
import { MiB } from "./units.js";
import { fmtBytes, MiB } from "./units.js";

// Enable garbage collection access
declare const global: {
Expand All @@ -32,37 +31,43 @@ describeMemory("Memory Tests", () => {
const iterations = 200;

// Helper to get memory usage after GC
function getMemoryUsage(): number {
async function getMemoryUsage(): Promise<number> {
await delay(100);
global.gc();
await delay(100);
return process.memoryUsage().heapUsed;
}

// Helper to check if memory usage is stable
async function checkMemoryUsage(
operation: () => Promise<unknown>,
errorMarginBytes: number = (isMacOS ? 7 : 5) * MiB,
errorMarginBytes: number = 5 * MiB,
) {
const initialMemory = getMemoryUsage();
// warm up memory consumption:
await operation();
// __then__ take a snapshot
const initialMemory = await getMemoryUsage();

// Run operations
for (let i = 0; i < iterations; i++) {
await operation();

// Check every 10 iterations
if (i % Math.floor(iterations / 5) === 0) {
await delay(1); // < Allow GC to settle
const currentMemory = getMemoryUsage();
console.log(`Memory after iteration ${i}: ${currentMemory} bytes`);
const currentMemory = await getMemoryUsage();
console.log(
`Memory after iteration ${i}: ${fmtBytes(currentMemory)} (delta: ${fmtBytes(currentMemory - initialMemory)})`,
);
// Allow some variance but fail on large increases
expect(currentMemory - initialMemory).toBeLessThan(errorMarginBytes);
}
}

// Final memory check
const finalMemory = getMemoryUsage();
console.log(`Initial memory: ${initialMemory} bytes`);
console.log(`Final memory: ${finalMemory} bytes`);
console.log(`Difference: ${finalMemory - initialMemory} bytes`);
const finalMemory = await getMemoryUsage();
console.log(`Initial memory: ${fmtBytes(initialMemory)}`);
console.log(`Final memory: ${fmtBytes(finalMemory)}`);
console.log(`Difference: ${fmtBytes(finalMemory - initialMemory)}`);

expect(finalMemory - initialMemory).toBeLessThan(errorMarginBytes);
}
Expand Down
13 changes: 13 additions & 0 deletions src/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ export const MiB = 1024 * KiB;
* @see https://en.wikipedia.org/wiki/Gibibyte
*/
export const GiB = 1024 * MiB;


export function fmtBytes(bytes: number): string {
if (bytes < KiB) {
return `${bytes} B`;
} else if (bytes < MiB) {
return `${(bytes / KiB).toFixed(2)} KiB`;
} else if (bytes < GiB) {
return `${(bytes / MiB).toFixed(2)} MiB`;
} else {
return `${(bytes / GiB).toFixed(2)} GiB`;
}
}

0 comments on commit d5ac699

Please sign in to comment.