mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(bench): eliminate sanitizeExit overhead (#14361)
This commit is contained in:
parent
602097ab6e
commit
d2c80aa26f
1 changed files with 23 additions and 57 deletions
|
@ -438,27 +438,6 @@
|
|||
};
|
||||
}
|
||||
|
||||
function assertExitSync(fn, isTest) {
|
||||
return function exitSanitizer(...params) {
|
||||
setExitHandler((exitCode) => {
|
||||
assert(
|
||||
false,
|
||||
`${
|
||||
isTest ? "Test case" : "Bench"
|
||||
} attempted to exit with exit code: ${exitCode}`,
|
||||
);
|
||||
});
|
||||
|
||||
try {
|
||||
fn(...new SafeArrayIterator(params));
|
||||
} catch (err) {
|
||||
throw err;
|
||||
} finally {
|
||||
setExitHandler(null);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function assertTestStepScopes(fn) {
|
||||
/** @param step {TestStep} */
|
||||
return async function testStepSanitizer(step) {
|
||||
|
@ -535,18 +514,18 @@
|
|||
};
|
||||
}
|
||||
|
||||
function pledgePermissions(permissions) {
|
||||
return core.opSync(
|
||||
"op_pledge_test_permissions",
|
||||
serializePermissions(permissions),
|
||||
);
|
||||
}
|
||||
|
||||
function restorePermissions(token) {
|
||||
core.opSync("op_restore_test_permissions", token);
|
||||
}
|
||||
|
||||
function withPermissions(fn, permissions) {
|
||||
function pledgePermissions(permissions) {
|
||||
return core.opSync(
|
||||
"op_pledge_test_permissions",
|
||||
serializePermissions(permissions),
|
||||
);
|
||||
}
|
||||
|
||||
function restorePermissions(token) {
|
||||
core.opSync("op_restore_test_permissions", token);
|
||||
}
|
||||
|
||||
return async function applyPermissions(...params) {
|
||||
const token = pledgePermissions(permissions);
|
||||
|
||||
|
@ -749,11 +728,6 @@
|
|||
const AsyncFunction = (async () => {}).constructor;
|
||||
benchDef.async = AsyncFunction === benchDef.fn.constructor;
|
||||
|
||||
benchDef.fn = wrapBenchFnWithSanitizers(
|
||||
benchDef.fn,
|
||||
benchDef,
|
||||
);
|
||||
|
||||
ArrayPrototypePush(benches, benchDef);
|
||||
}
|
||||
|
||||
|
@ -989,10 +963,16 @@
|
|||
|
||||
try {
|
||||
if (bench.permissions) {
|
||||
token = core.opSync(
|
||||
"op_pledge_test_permissions",
|
||||
serializePermissions(bench.permissions),
|
||||
);
|
||||
token = pledgePermissions(bench.permissions);
|
||||
}
|
||||
|
||||
if (bench.sanitizeExit) {
|
||||
setExitHandler((exitCode) => {
|
||||
assert(
|
||||
false,
|
||||
`Bench attempted to exit with exit code: ${exitCode}`,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const benchTimeInMs = 500;
|
||||
|
@ -1003,7 +983,8 @@
|
|||
} catch (error) {
|
||||
return { failed: { ...bench, error: formatError(error) } };
|
||||
} finally {
|
||||
if (token !== null) core.opSync("op_restore_test_permissions", token);
|
||||
if (bench.sanitizeExit) setExitHandler(null);
|
||||
if (token !== null) restorePermissions(token);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1543,21 +1524,6 @@
|
|||
return testFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T {Function}
|
||||
* @param fn {T}
|
||||
* @param opts {{
|
||||
* sanitizeExit: boolean,
|
||||
* }}
|
||||
* @returns {T}
|
||||
*/
|
||||
function wrapBenchFnWithSanitizers(fn, opts) {
|
||||
if (opts.sanitizeExit) {
|
||||
fn = opts.async ? assertExit(fn, false) : assertExitSync(fn, false);
|
||||
}
|
||||
return fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T
|
||||
* @param value {T | undefined}
|
||||
|
|
Loading…
Reference in a new issue