Skip to content

Commit

Permalink
test: improve performance of test-crypto-timing-safe-equal-benchmarks
Browse files Browse the repository at this point in the history
Using `eval()` rather than `require()`'ing a fixture and deleting it
from the cache results in a roughtly 10x improvement in running time.

Fixes: nodejs#25984
Refs: nodejs#26229

PR-URL: nodejs#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]>
  • Loading branch information
Trott committed Feb 22, 2019
1 parent d1011f6 commit eb9e6c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
17 changes: 0 additions & 17 deletions test/fixtures/crypto-timing-safe-equal-benchmark-func.js

This file was deleted.

27 changes: 15 additions & 12 deletions test/pummel/test-crypto-timing-safe-equal-benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ if (!common.hasCrypto)
if (!common.enoughTestMem)
common.skip('memory-intensive test');

const fixtures = require('../common/fixtures');
const assert = require('assert');
const crypto = require('crypto');

const BENCHMARK_FUNC_PATH =
`${fixtures.fixturesDir}/crypto-timing-safe-equal-benchmark-func`;
function runOneBenchmark(...args) {
const benchmarkFunc = require(BENCHMARK_FUNC_PATH);
const result = benchmarkFunc(...args);

// Don't let the comparison function get cached. This avoid a timing
// inconsistency due to V8 optimization where the function would take
// less time when called with a specific set of parameters.
delete require.cache[require.resolve(BENCHMARK_FUNC_PATH)];
return result;
function runOneBenchmark(compareFunc, firstBufFill, secondBufFill, bufSize) {
return eval(`
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);
endTime[0] * 1e9 + endTime[1];
`);
}

function getTValue(compareFunc) {
Expand Down

0 comments on commit eb9e6c0

Please sign in to comment.