Skip to content

Commit

Permalink
test garbage collector
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardoventurini committed Oct 16, 2024
1 parent 1012a49 commit 0d95c67
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 1 addition & 2 deletions async-interceptor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import async_hooks from 'node:async_hooks';

// 50% of map limit
export const GC_LIMIT = Math.pow(2, 23);
export const GC_LIMIT = Meteor.isPackageTest ? 7 : Math.pow(2, 23);

export const AsyncResourceMap = new Map();

Expand All @@ -17,7 +17,6 @@ function captureResource(asyncId, type) {
stack = `${type}\n${stack}`;

if (AsyncResourceMap.size > GC_LIMIT) {
console.log('Meteor Perf: Reached AsyncResourceMap limit, garbage collecting');
garbageCollectAsyncResources();
}

Expand Down
1 change: 0 additions & 1 deletion save-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export function saveOutput() {

async_traces.push({
count: info.count,
types: [...info.types],
stack,
});
});
Expand Down
55 changes: 52 additions & 3 deletions tests/async-interceptor.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai';
import { AsyncInterceptor, AsyncResourceMap, stackTrace } from '../async-interceptor';
import { AsyncInterceptor, AsyncResourceMap, GC_LIMIT, stackTrace } from '../async-interceptor';
import Benchmarkify from "benchmarkify";

const benchmark = new Benchmarkify("Meteor Perf", { chartImage: true }).printHeader();
Expand All @@ -14,7 +14,6 @@ benchmark.createSuite("Stack Trace", { time: 1000 })
}).join('\n');
});


await benchmark.run();

describe('Async Interceptor', () => {
Expand All @@ -35,4 +34,54 @@ describe('Async Interceptor', () => {

expect(AsyncResourceMap.size).to.be.greaterThan(0);
})
})

it('should not capture async operations when disabled', async () => {
AsyncInterceptor.disable();

AsyncResourceMap.clear();

await new Promise((resolve) => {
setTimeout(resolve, 100);
});

expect(AsyncResourceMap.size).to.equal(0);
});

it('should garbage collect async resources', async () => {
AsyncInterceptor.enable();

for (let i = 0; i < GC_LIMIT + 1; i++) {
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
await new Promise((resolve) => {
setTimeout(resolve, 1);
});
}

AsyncInterceptor.disable();

expect(AsyncResourceMap.size).to.be.lessThanOrEqual(GC_LIMIT);
})
});

0 comments on commit 0d95c67

Please sign in to comment.