-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from meteor/testing
test: testing github workflow
- Loading branch information
Showing
8 changed files
with
238 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Test Meteor Perf Package | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
env: | ||
METEOR_VERSION: 3.0.4 | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 20 | ||
|
||
- name: Cache Meteor Installation | ||
id: meteor-cache | ||
uses: actions/cache@v3 | ||
with: | ||
path: ~/.meteor | ||
key: ${{ runner.os }}-meteor-${{ env.METEOR_VERSION }} | ||
restore-keys: | | ||
${{ runner.os }}-meteor- | ||
- name: Install Meteor (if not cached) | ||
if: steps.meteor-cache.outputs.cache-hit != 'true' | ||
run: | | ||
npx meteor@${{ env.METEOR_VERSION }} | ||
- name: Export Path | ||
run: | | ||
echo "$HOME/.meteor" >> $GITHUB_PATH | ||
- name: Run Tests | ||
run: | | ||
TEST_CLIENT=0 meteor test-packages ./ --driver-package meteortesting:mocha --no-release-check --once |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.vscode | ||
node_modules | ||
node_modules | ||
.npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,45 @@ | ||
import async_hooks from 'node:async_hooks'; | ||
|
||
export const asyncResources = new Map(); | ||
// 50% of map limit | ||
export const GC_LIMIT = Meteor.isPackageTest ? 7 : Math.pow(2, 23); | ||
|
||
export const AsyncResourceMap = new Map(); | ||
|
||
export const AsyncInterceptor = async_hooks.createHook({ | ||
init(asyncId, type, triggerAsyncId, resource) { | ||
logResourceCreation(asyncId, type, triggerAsyncId, resource); | ||
init(asyncId, type) { | ||
captureResource(asyncId, type); | ||
}, | ||
}); | ||
|
||
function logResourceCreation(asyncId, type, triggerAsyncId, resource) { | ||
const stack = (new Error()).stack.split('\n').slice(2).filter(line => { | ||
return !['AsyncHook.init', 'node:internal/async_hooks'].some(fn => line.includes(fn)); | ||
}).join('\n'); | ||
function captureResource(asyncId, type) { | ||
let stack = stackTrace(); | ||
|
||
stack = `${type}\n${stack}`; | ||
|
||
if (AsyncResourceMap.size > GC_LIMIT) { | ||
garbageCollectAsyncResources(); | ||
} | ||
|
||
if (!asyncResources.has(stack)) { | ||
asyncResources.set(stack, { count: 0, types: new Set() }); | ||
if (!AsyncResourceMap.has(stack)) { | ||
AsyncResourceMap.set(stack, { count: 0, ts: Date.now() }); | ||
} | ||
|
||
const resourceInfo = asyncResources.get(stack); | ||
const resourceInfo = AsyncResourceMap.get(stack); | ||
resourceInfo.count++; | ||
resourceInfo.types.add(type); | ||
} | ||
|
||
function garbageCollectAsyncResources() { | ||
AsyncResourceMap.forEach((info, stack) => { | ||
if (info.count <= 1) { | ||
AsyncResourceMap.delete(stack); | ||
} | ||
}); | ||
} | ||
|
||
export function stackTrace () { | ||
return (new Error()).stack.split('\n').slice(3).filter(line => { | ||
return !['AsyncHook.init', 'node:internal/async_hooks'].some(fn => line.includes(fn)); | ||
}).join('\n') | ||
} | ||
|
||
setInterval(garbageCollectAsyncResources, 10000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { expect } from 'chai'; | ||
import { AsyncInterceptor, AsyncResourceMap, GC_LIMIT, stackTrace } from '../async-interceptor'; | ||
import Benchmarkify from "benchmarkify"; | ||
|
||
const benchmark = new Benchmarkify("Meteor Perf").printHeader(); | ||
|
||
benchmark.createSuite("Stack Trace", { time: 1000 }) | ||
.add("default error stack trace", () => { | ||
return new Error().stack; | ||
}) | ||
.ref("clean stack trace", () => { | ||
return (new Error()).stack.split('\n').slice(2).filter(line => { | ||
return !['AsyncHook.init', 'node:internal/async_hooks'].some(fn => line.includes(fn)); | ||
}).join('\n'); | ||
}); | ||
|
||
benchmark.createSuite("Interceptor Overhead", { time: 3000 }) | ||
.setup(() => { | ||
AsyncInterceptor.enable(); | ||
}) | ||
.tearDown(() => { | ||
AsyncInterceptor.disable(); | ||
}) | ||
.add("on", async (done) => { | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 0); | ||
}) | ||
|
||
done() | ||
}) | ||
.ref("off", async (done) => { | ||
AsyncInterceptor.disable(); | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(resolve, 0); | ||
}) | ||
|
||
done() | ||
}); | ||
|
||
|
||
|
||
await benchmark.run(); | ||
|
||
describe('Async Interceptor', () => { | ||
beforeEach(() => { | ||
AsyncResourceMap.clear(); | ||
}) | ||
|
||
it('get stack trace', async () => { | ||
const stack = stackTrace(); | ||
|
||
expect(stack).to.be.a('string'); | ||
}); | ||
|
||
it('should capture async operations', async () => { | ||
AsyncInterceptor.enable(); | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(resolve, 100); | ||
}); | ||
|
||
AsyncInterceptor.disable(); | ||
|
||
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); | ||
}); | ||
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); | ||
}) | ||
|
||
it('should have ts timestamp attribute for all trace entries', async () => { | ||
AsyncInterceptor.enable(); | ||
|
||
await new Promise((resolve) => { | ||
setTimeout(resolve, 100); | ||
}); | ||
|
||
AsyncInterceptor.disable(); | ||
|
||
AsyncResourceMap.forEach((info) => { | ||
expect(info).to.have.property('ts'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './observer.test'; | ||
import './observer.test'; | ||
import './async-interceptor.test'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters