test: improve performance of test-crypto-timing-safe-equal-benchmarks
Using `eval()` rather than `require()`'ing a fixture and deleting it from the cache results in a roughtly 10x improvement in running time. Fixes: https://github.com/nodejs/node/issues/25984 Refs: https://github.com/nodejs/node/issues/26229 PR-URL: https://github.com/nodejs/node/pull/26237 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Yang Guo <yangguo@chromium.org> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Teddy Katz <teddy.katz@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com>
This commit is contained in:
parent
d1011f6bbf
commit
eb9e6c0157
@ -1,17 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const assert = require('assert');
|
|
||||||
module.exports = (compareFunc, firstBufFill, secondBufFill, bufSize) => {
|
|
||||||
const firstBuffer = Buffer.alloc(bufSize, firstBufFill);
|
|
||||||
const secondBuffer = Buffer.alloc(bufSize, secondBufFill);
|
|
||||||
|
|
||||||
const startTime = process.hrtime();
|
|
||||||
const result = compareFunc(firstBuffer, secondBuffer);
|
|
||||||
const endTime = process.hrtime(startTime);
|
|
||||||
|
|
||||||
// Ensure that the result of the function call gets used, so it doesn't
|
|
||||||
// get discarded due to engine optimizations.
|
|
||||||
assert.strictEqual(result, firstBufFill === secondBufFill);
|
|
||||||
|
|
||||||
return endTime[0] * 1e9 + endTime[1];
|
|
||||||
};
|
|
@ -6,21 +6,24 @@ if (!common.hasCrypto)
|
|||||||
if (!common.enoughTestMem)
|
if (!common.enoughTestMem)
|
||||||
common.skip('memory-intensive test');
|
common.skip('memory-intensive test');
|
||||||
|
|
||||||
const fixtures = require('../common/fixtures');
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
|
|
||||||
const BENCHMARK_FUNC_PATH =
|
function runOneBenchmark(compareFunc, firstBufFill, secondBufFill, bufSize) {
|
||||||
`${fixtures.fixturesDir}/crypto-timing-safe-equal-benchmark-func`;
|
return eval(`
|
||||||
function runOneBenchmark(...args) {
|
const firstBuffer = Buffer.alloc(bufSize, firstBufFill);
|
||||||
const benchmarkFunc = require(BENCHMARK_FUNC_PATH);
|
const secondBuffer = Buffer.alloc(bufSize, secondBufFill);
|
||||||
const result = benchmarkFunc(...args);
|
|
||||||
|
|
||||||
// Don't let the comparison function get cached. This avoid a timing
|
const startTime = process.hrtime();
|
||||||
// inconsistency due to V8 optimization where the function would take
|
const result = compareFunc(firstBuffer, secondBuffer);
|
||||||
// less time when called with a specific set of parameters.
|
const endTime = process.hrtime(startTime);
|
||||||
delete require.cache[require.resolve(BENCHMARK_FUNC_PATH)];
|
|
||||||
return result;
|
// Ensure that the result of the function call gets used, so it doesn't
|
||||||
|
// get discarded due to engine optimizations.
|
||||||
|
assert.strictEqual(result, firstBufFill === secondBufFill);
|
||||||
|
|
||||||
|
endTime[0] * 1e9 + endTime[1];
|
||||||
|
`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTValue(compareFunc) {
|
function getTValue(compareFunc) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user