-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #54380 Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Claudio Wunder <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
Showing
7 changed files
with
298 additions
and
0 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,65 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { getCallSite } = require('node:util'); | ||
const assert = require('node:assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
method: ['ErrorCallSite', 'ErrorCallSiteSerialized', 'CPP'], | ||
}); | ||
|
||
function ErrorGetCallSite() { | ||
const originalStackFormatter = Error.prepareStackTrace; | ||
Error.prepareStackTrace = (_err, stack) => { | ||
if (stack && stack.length > 1) { | ||
// Remove node:util | ||
return stack.slice(1); | ||
} | ||
return stack; | ||
}; | ||
const err = new Error(); | ||
// With the V8 Error API, the stack is not formatted until it is accessed | ||
err.stack; // eslint-disable-line no-unused-expressions | ||
Error.prepareStackTrace = originalStackFormatter; | ||
return err.stack; | ||
} | ||
|
||
function ErrorCallSiteSerialized() { | ||
const callsite = ErrorGetCallSite(); | ||
const serialized = []; | ||
for (let i = 0; i < callsite.length; ++i) { | ||
serialized.push({ | ||
functionName: callsite[i].getFunctionName(), | ||
scriptName: callsite[i].getFileName(), | ||
lineNumber: callsite[i].getLineNumber(), | ||
column: callsite[i].getColumnNumber(), | ||
}); | ||
} | ||
return serialized; | ||
} | ||
|
||
function main({ n, method }) { | ||
let fn; | ||
switch (method) { | ||
case 'ErrorCallSite': | ||
fn = ErrorGetCallSite; | ||
break; | ||
case 'ErrorCallSiteSerialized': | ||
fn = ErrorCallSiteSerialized; | ||
break; | ||
case 'CPP': | ||
fn = getCallSite; | ||
break; | ||
} | ||
let lastStack = {}; | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) { | ||
const stack = fn(); | ||
lastStack = stack; | ||
} | ||
bench.end(n); | ||
// Attempt to avoid dead-code elimination | ||
assert.ok(lastStack); | ||
} |
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
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,4 @@ | ||
const util = require('node:util'); | ||
const assert = require('node:assert'); | ||
assert.ok(util.getCallSite().length > 1); | ||
process.stdout.write(util.getCallSite()[0].scriptName); |
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,106 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
|
||
const fixtures = require('../common/fixtures'); | ||
const file = fixtures.path('get-call-site.js'); | ||
|
||
const { getCallSite } = require('node:util'); | ||
const { spawnSync } = require('node:child_process'); | ||
const assert = require('node:assert'); | ||
|
||
{ | ||
const callsite = getCallSite(); | ||
assert.ok(callsite.length > 1); | ||
assert.match( | ||
callsite[0].scriptName, | ||
/test-util-getCallSite/, | ||
'node:util should be ignored', | ||
); | ||
} | ||
|
||
{ | ||
const callsite = getCallSite(3); | ||
assert.strictEqual(callsite.length, 3); | ||
assert.match( | ||
callsite[0].scriptName, | ||
/test-util-getCallSite/, | ||
'node:util should be ignored', | ||
); | ||
} | ||
|
||
// Guarantee dot-left numbers are ignored | ||
{ | ||
const callsite = getCallSite(3.6); | ||
assert.strictEqual(callsite.length, 3); | ||
} | ||
|
||
{ | ||
const callsite = getCallSite(3.4); | ||
assert.strictEqual(callsite.length, 3); | ||
} | ||
|
||
{ | ||
assert.throws(() => { | ||
// Max than kDefaultMaxCallStackSizeToCapture | ||
getCallSite(201); | ||
}, common.expectsError({ | ||
code: 'ERR_OUT_OF_RANGE' | ||
})); | ||
assert.throws(() => { | ||
getCallSite(-1); | ||
}, common.expectsError({ | ||
code: 'ERR_OUT_OF_RANGE' | ||
})); | ||
assert.throws(() => { | ||
getCallSite({}); | ||
}, common.expectsError({ | ||
code: 'ERR_INVALID_ARG_TYPE' | ||
})); | ||
} | ||
|
||
{ | ||
const callsite = getCallSite(1); | ||
assert.strictEqual(callsite.length, 1); | ||
assert.match( | ||
callsite[0].scriptName, | ||
/test-util-getCallSite/, | ||
'node:util should be ignored', | ||
); | ||
} | ||
|
||
// Guarantee [eval] will appear on stacktraces when using -e | ||
{ | ||
const { status, stderr, stdout } = spawnSync( | ||
process.execPath, | ||
[ | ||
'-e', | ||
`const util = require('util'); | ||
const assert = require('assert'); | ||
assert.ok(util.getCallSite().length > 1); | ||
process.stdout.write(util.getCallSite()[0].scriptName); | ||
`, | ||
], | ||
); | ||
assert.strictEqual(status, 0, stderr.toString()); | ||
assert.strictEqual(stdout.toString(), '[eval]'); | ||
} | ||
|
||
// Guarantee the stacktrace[0] is the filename | ||
{ | ||
const { status, stderr, stdout } = spawnSync( | ||
process.execPath, | ||
[file], | ||
); | ||
assert.strictEqual(status, 0, stderr.toString()); | ||
assert.strictEqual(stdout.toString(), file); | ||
} | ||
|
||
// Error.stackTraceLimit should not influence callsite size | ||
{ | ||
const originalStackTraceLimit = Error.stackTraceLimit; | ||
Error.stackTraceLimit = 0; | ||
const callsite = getCallSite(); | ||
assert.notStrictEqual(callsite.length, 0); | ||
Error.stackTraceLimit = originalStackTraceLimit; | ||
} |