flaska/benchmark/router_flaska_v2.mjs

96 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-10-27 05:12:19 +00:00
import { summary, run, bench } from 'mitata';
import assert from 'assert'
import { compilePaths } from "../router_v2.mjs"
import * as consts from './const.js'
function printCurrentStatus(fn) {
// console.log(`--- checking optimizations status on ${fn.name} ---`)
let opt = %GetOptimizationStatus(fn)
console.log(`${opt.toString(2).padStart(17, '0').split('').join(' ')} (${opt}) ${fn.name}`)
}
function printStatusHelperText() {
console.log(`┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬ ┬
is function
is never optimized
is always optimized
is maybe deoptimized
is optimized
is optimized by TurboFan
is interpreted
is marked for optimization
is marked for concurrent optimization
is optimizing concurrently
is executing
topmost frame is turbo fanned
lite mode
marked for deoptimization
baseline
topmost frame is interpreted
topmost frame is baseline`)
}
// Warmup (de-optimize `bench()` calls)
bench('noop', () => { });
bench('noop2', () => { });
let paths = [
{ path: '/aa/aa', },
{ path: '/aa/:blabla', },
{ path: '/aa/:blabla/aa', },
{ path: '/aa/:blabla/ab', },
{ path: '/aa/:blabla/bb', },
{ path: '/::rest', },
]
paths = consts.allManyRoutes.map(x => ({ path: x }))
let tests = [
['/', paths[5]],
['/aa', paths[5]],
['/aa/aa', paths[0]],
['/aa/_', paths[1]],
['/aa/_/aa', paths[2]],
['/aa/_/ab', paths[3]],
['/aa/_/bb', paths[4]],
]
tests = paths.map(p => ([p.path.replace(/:[^/]+/g, '_'), p]))
let testStrings = tests.map(x => x[0])
let func = compilePaths(paths)
for (let [_, fun] of func) {
console.log(`--- Sanity checking ${fun.name} ---`)
for (let test of tests) {
let check = fun(test[0])
// console.log(test[0], check)
assert.strictEqual(check.path, test[1])
}
}
for (let [_, fun] of func) {
console.log(`--- warming up ${fun.name} ---`)
for (var i = 0; i < 10000; i++) {
testStrings.forEach(fun)
}
}
console.log('--- sleeping ---')
await new Promise(res => setTimeout(res, 1000))
for (let [org] of func) {
printCurrentStatus(org);
}
printStatusHelperText()
summary(() => {
func.forEach(function([_, fun]) {
bench(fun.name.slice(6), function() {
return testStrings.map(fun)
})
})
})
run().then(function() {
for (let [check] of func) {
printCurrentStatus(check);
}
printStatusHelperText()
});