Skip to content

Commit 23889f0

Browse files
committed
Test useCPED when on Node 24
1 parent c837e59 commit 23889f0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"mocha": "^10.2.0",
5858
"nan": "^2.22.2",
5959
"nyc": "^15.1.0",
60+
"semver": "^7.7.2",
6061
"sinon": "^15.2.0",
6162
"source-map-support": "^0.5.21",
6263
"tmp": "0.2.1",

ts/test/test-time-profiler.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ import {hrtime} from 'process';
2323
import {Label, Profile} from 'pprof-format';
2424
import {AssertionError} from 'assert';
2525
import {GenerateTimeLabelsArgs, LabelSet} from '../src/v8-types';
26+
import {AsyncLocalStorage} from 'async_hooks';
27+
import {satisfies} from 'semver';
2628

2729
const assert = require('assert');
2830

31+
const useCPED =
32+
satisfies(process.versions.node, '>=24.0.0') &&
33+
!process.execArgv.includes('--no-async-context-frame');
34+
2935
const PROFILE_OPTIONS = {
3036
durationMillis: 500,
3137
intervalMicros: 1000,
@@ -44,11 +50,16 @@ describe('Time Profiler', () => {
4450
this.skip();
4551
}
4652
const startTime = BigInt(Date.now()) * 1000n;
53+
if (useCPED) {
54+
// Ensure an async context frame is created to hold the profiler context.
55+
new AsyncLocalStorage().enterWith(1);
56+
}
4757
time.start({
4858
intervalMicros: 20 * 1_000,
4959
durationMillis: PROFILE_OPTIONS.durationMillis,
5060
withContexts: true,
5161
lineNumbers: false,
62+
useCPED,
5263
});
5364
const initialContext: {[key: string]: string} = {};
5465
time.setContext(initialContext);
@@ -97,11 +108,16 @@ describe('Time Profiler', () => {
97108
this.timeout(3000);
98109

99110
const intervalNanos = PROFILE_OPTIONS.intervalMicros * 1_000;
111+
if (useCPED) {
112+
// Ensure an async context frame is created to hold the profiler context.
113+
new AsyncLocalStorage().enterWith(1);
114+
}
100115
time.start({
101116
intervalMicros: PROFILE_OPTIONS.intervalMicros,
102117
durationMillis: PROFILE_OPTIONS.durationMillis,
103118
withContexts: true,
104119
lineNumbers: false,
120+
useCPED,
105121
});
106122
// By repeating the test few times, we also exercise the profiler
107123
// start-stop overlap behavior.

ts/test/worker.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import {pbkdf2} from 'crypto';
33
import {time} from '../src/index';
44
import {Profile, ValueType} from 'pprof-format';
55
import {getAndVerifyPresence, getAndVerifyString} from './profiles-for-tests';
6+
import {satisfies} from 'semver';
67

78
import assert from 'assert';
9+
import {AsyncLocalStorage} from 'async_hooks';
810

911
const DURATION_MILLIS = 1000;
1012
const intervalMicros = 10000;
1113
const withContexts =
1214
process.platform === 'darwin' || process.platform === 'linux';
15+
const useCPED =
16+
satisfies(process.versions.node, '>=24.0.0') &&
17+
!process.execArgv.includes('--no-async-context-frame');
1318

1419
function createWorker(durationMs: number): Promise<Profile[]> {
1520
return new Promise((resolve, reject) => {
@@ -52,15 +57,20 @@ function getCpuUsage() {
5257
}
5358

5459
async function main(durationMs: number) {
60+
if (useCPED) new AsyncLocalStorage().enterWith(1);
5561
time.start({
5662
durationMillis: durationMs * 3,
5763
intervalMicros,
5864
withContexts,
5965
collectCpuTime: withContexts,
66+
useCPED: useCPED,
6067
});
68+
if (withContexts) {
69+
time.setContext({});
70+
}
6171

6272
const cpu0 = getCpuUsage();
63-
const nbWorkers = Number(process.argv[2] || 2);
73+
const nbWorkers = Number(process.argv[2] ?? 2);
6474

6575
// start workers
6676
const workers = executeWorkers(nbWorkers, durationMs);
@@ -91,12 +101,17 @@ async function main(durationMs: number) {
91101
}
92102

93103
async function worker(durationMs: number) {
104+
if (useCPED) new AsyncLocalStorage().enterWith(1);
94105
time.start({
95106
durationMillis: durationMs,
96107
intervalMicros,
97108
withContexts,
98109
collectCpuTime: withContexts,
110+
useCPED: useCPED,
99111
});
112+
if (withContexts) {
113+
time.setContext({});
114+
}
100115

101116
const deadline = Date.now() + durationMs;
102117
await Promise.all([bar(deadline), foo(deadline)]);

0 commit comments

Comments
 (0)