Skip to content
Open
Changes from all commits
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
85 changes: 47 additions & 38 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
log('All tests took ', StopWatch.now() - T0, 'ms')
})
.catch(log)