Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
squash: split into two benchmark functions
  • Loading branch information
not-an-aardvark committed Aug 28, 2016
commit 872e56341170fafc4f8a9708ad66dc29c074f25c
34 changes: 18 additions & 16 deletions test/sequential/test-crypto-timing-safe-equal.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,25 @@ assert.throws(function() {
crypto.timingSafeEqual(Buffer.from([1, 2]), 'not a buffer');
}, 'should throw if the second argument is not a buffer');

function runBenchmark(compareFunc, bufferA, bufferB, expectedResult) {
function runEqualBenchmark(compareFunc, bufferA, bufferB) {
const startTime = process.hrtime();
const result = compareFunc(bufferA, bufferB);
const endTime = process.hrtime(startTime);

// Ensure that the result of the function call gets used, so that it doesn't
// get discarded due to engine optimizations.
assert.strictEqual(result, expectedResult);
assert.strictEqual(result, true);
return endTime[0] * 1e9 + endTime[1];
}

// This is almost the same as the runEqualBenchmark function, but it's
// duplicated to avoid timing issues with V8 optimization/inlining.
function runUnequalBenchmark(compareFunc, bufferA, bufferB) {
const startTime = process.hrtime();
const result = compareFunc(bufferA, bufferB);
const endTime = process.hrtime(startTime);

assert.strictEqual(result, false);
return endTime[0] * 1e9 + endTime[1];
}

Expand All @@ -57,17 +68,10 @@ function getTValue(compareFunc) {
const rawUnequalBenches = Array(numTrials);

for (let i = 0; i < numTrials; i++) {

if (i % 2) {
// First benchmark: comparing two equal buffers
rawEqualBenches[i] = runBenchmark(compareFunc, bufferA1, bufferA2, true);
// Second benchmark: comparing two unequal buffers
rawUnequalBenches[i] = runBenchmark(compareFunc, bufferB, bufferC, false);
} else {
// Flip the order of the benchmarks every other iteration
rawUnequalBenches[i] = runBenchmark(compareFunc, bufferB, bufferC, false);
rawEqualBenches[i] = runBenchmark(compareFunc, bufferA1, bufferA2, true);
}
// First benchmark: comparing two equal buffers
rawEqualBenches[i] = runEqualBenchmark(compareFunc, bufferA1, bufferA2);
// Second benchmark: comparing two unequal buffers
rawUnequalBenches[i] = runUnequalBenchmark(compareFunc, bufferB, bufferC);
}

const equalBenches = filterOutliers(rawEqualBenches);
Expand Down Expand Up @@ -99,9 +103,7 @@ function getTValue(compareFunc) {
unequalStd: standardDeviation(unequalBenches),
combinedStd,
standardErr,
t: (equalMean - unequalMean) / standardErr,
rawEqualBenches: JSON.stringify(rawEqualBenches),
rawUnequalBenches: JSON.stringify(rawUnequalBenches)
t: (equalMean - unequalMean) / standardErr
});
}

Expand Down