Skip to content

Commit ca18525

Browse files
Trottrvagg
authored andcommitted
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: #25984 Refs: #26229 PR-URL: #26237 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yang Guo <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 7e26ca6 commit ca18525

File tree

2 files changed

+15
-29
lines changed

2 files changed

+15
-29
lines changed

‎test/fixtures/crypto-timing-safe-equal-benchmark-func.js‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎test/pummel/test-crypto-timing-safe-equal-benchmarks.js‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ if (!common.hasCrypto)
66
if(!common.enoughTestMem)
77
common.skip('memory-intensive test');
88

9-
constfixtures=require('../common/fixtures');
109
constassert=require('assert');
1110
constcrypto=require('crypto');
1211

13-
constBENCHMARK_FUNC_PATH=
14-
`${fixtures.fixturesDir}/crypto-timing-safe-equal-benchmark-func`;
15-
functionrunOneBenchmark(...args){
16-
constbenchmarkFunc=require(BENCHMARK_FUNC_PATH);
17-
constresult=benchmarkFunc(...args);
18-
19-
// Don't let the comparison function get cached. This avoid a timing
20-
// inconsistency due to V8 optimization where the function would take
21-
// less time when called with a specific set of parameters.
22-
deleterequire.cache[require.resolve(BENCHMARK_FUNC_PATH)];
23-
returnresult;
12+
functionrunOneBenchmark(compareFunc,firstBufFill,secondBufFill,bufSize){
13+
returneval(`
14+
const firstBuffer = Buffer.alloc(bufSize, firstBufFill);
15+
const secondBuffer = Buffer.alloc(bufSize, secondBufFill);
16+
17+
const startTime = process.hrtime();
18+
const result = compareFunc(firstBuffer, secondBuffer);
19+
const endTime = process.hrtime(startTime);
20+
21+
// Ensure that the result of the function call gets used, so it doesn't
22+
// get discarded due to engine optimizations.
23+
assert.strictEqual(result, firstBufFill === secondBufFill);
24+
25+
endTime[0] * 1e9 + endTime[1];
26+
`);
2427
}
2528

2629
functiongetTValue(compareFunc){

0 commit comments

Comments
(0)