From e2a63de0be29f93f459ad202b4e3edc0bf0ebaf5 Mon Sep 17 00:00:00 2001 From: Neek Sandhu Date: Tue, 26 Jun 2018 23:32:50 -0700 Subject: [PATCH] switch to serious multi-byte testing --- benchmark.js | 85 +++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 38 deletions(-) diff --git a/benchmark.js b/benchmark.js index c05f1e1..caf7cd2 100644 --- a/benchmark.js +++ b/benchmark.js @@ -10,47 +10,56 @@ const wasmBin = fs.readFileSync(path.join(__dirname, './lib/onigasm.wasm')).buff const StopWatch = typeof performance === 'undefined' ? Date : performance const log = console.log +const mkArray = (size, filler) => Array(size).fill(undefined).map(filler) -loadWASM(wasmBin).then(() => { - const str = fs.readFileSync('node_modules/typescript/lib/typescriptServices.js', 'utf8').repeat(2) - const scanner = new OnigScanner(['searchTillTheEndButYouWontFindMe']) - var T0 = StopWatch.now() - - log() - const test = (charCount) => { - let t0 = StopWatch.now() - let stepCount = 50 - let stepFactor = charCount / 5 - for (let i = 0; i < 100; i++) { - for (let j = 0; j < stepCount; j++) { - scanner.findNextMatchSync(str.slice(0, j * stepFactor)) +loadWASM(wasmBin) + .then(() => { + const UNICODE_RANGE_FOR_SAMPLE = [100, 500] + const getRandChar = () => + String.fromCodePoint(Math.floor(Math.random() * (UNICODE_RANGE_FOR_SAMPLE[1] - UNICODE_RANGE_FOR_SAMPLE[0])) + UNICODE_RANGE_FOR_SAMPLE[0]) + + const strChunk100Chars = mkArray(100, getRandChar).join('') + + const scanner = new OnigScanner(['searchTillTheEndButYouWontFindMe' /* this is to stress test and compare libonig before version upgrade */ ]) + var T0 = StopWatch.now() + + log(`\nStress testing OnigScanner.findNextMatchSync with:\n`) + const test = (charCount) => { + const str = strChunk100Chars.repeat(charCount / strChunk100Chars.length) + // we test 5 levels of charCount, starting from fifth, then fourth, third and so on + const splitCount = 5 + const stepFactor = charCount / splitCount + const literalStringSamples = mkArray(splitCount, (_, i) => str.slice(0, i * stepFactor)) + const onigStringSamples = mkArray(splitCount, (_, i) => new OnigString(str.slice(0, stepFactor * i))) + + let t0 = StopWatch.now() + for (let i = 0; i < 100; i++) { + for (let j = 0; j < splitCount; j++) { + scanner.findNextMatchSync(literalStringSamples[j]) + } } - } - log(`Uncached strings < ${charCount} characters\n:`, StopWatch.now() - t0, 'ms') - - t0 = StopWatch.now() - var onig_strs = Array(stepCount).fill(undefined).map((_, i) => new OnigString(str.slice(0, stepFactor * i))) - for (let i = 0; i < 100; i++) { - for(let j = 0; j < stepCount; j++) { - scanner.findNextMatchSync(onig_strs[j]) + log(`Single use literal strings < ${charCount} characters\n:`, StopWatch.now() - t0, 'ms') + + t0 = StopWatch.now() + for (let i = 0; i < 100; i++) { + for (let j = 0; j < splitCount; j++) { + scanner.findNextMatchSync(onigStringSamples[j]) + } } + log(`Reused OnigString objects < ${charCount} characters\n:`, StopWatch.now() - t0, 'ms') + log() } - log(`Cached strings < ${charCount} characters\n:`, StopWatch.now() - t0, 'ms') - log() - } - - [ - 100, - 1000, - 10000, - 50000, - 100000, - 1000000, - 2000000, - ].forEach(test) - + [ + 100, + 1000, + 10000, + 50000, + 100000, + 1000000, + 2000000, + ].forEach(test) - console.log('All tests took ', StopWatch.now() - T0, 'ms') -}) -.catch(console.log) \ No newline at end of file + log('All tests took ', StopWatch.now() - T0, 'ms') + }) + .catch(log) \ No newline at end of file