flaska/benchmark/router_flaska_v2.mjs

98 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2024-11-02 12:44:54 +00:00
import { summary, run, bench } from 'mitata';
import { createRouter, insertItem } from '@mapl/router'
import { compileRouter } from './mapl_compiler.mjs'
import { compilePaths } from "./router_v2.mjs"
2024-11-03 09:16:11 +00:00
import { compilePaths as mainCompiler } from "../router_v2.mjs"
import { FlaskaRouter as FlaskaRouterBuffer } from "../flaska_buffer.mjs"
import { FlaskaRouter as FlaskaRouterFast } from "../flaska_fast.mjs"
2024-11-02 12:44:54 +00:00
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', () => { });
2024-11-03 09:16:11 +00:00
let paths = consts.allRoutes.map(x => ({ path: x }))
2024-11-02 12:44:54 +00:00
let tests = paths.map(p => ([p.path.replace(/:[^/]+/g, '_'), p]))
let testStrings = tests.map(x => x[0])
let testStringsMapl = testStrings.map(x => x.slice(1))
2024-11-03 09:16:11 +00:00
let func = [[testStrings, mainCompiler(paths)]]
2024-11-02 12:44:54 +00:00
2024-11-03 09:16:11 +00:00
let flaskaRouterBuffer = new FlaskaRouterBuffer()
flaskaRouterBuffer.paths = paths.slice()
flaskaRouterBuffer.compile()
func.push([testStrings, flaskaRouterBuffer.match])
let flaskaRouterFast = new FlaskaRouterFast()
flaskaRouterFast.paths = paths.slice()
flaskaRouterFast.compile()
func.push([testStrings, flaskaRouterFast.match])
2024-11-02 12:44:54 +00:00
let maplPaths = paths.map(x => x.path.replace(/::[^\/]+/g, '**').replace(/:[^\/]+/g, '*'))
const maplRouter = createRouter();
for (let route of maplPaths) {
insertItem(maplRouter, route, { path: route })
}
let maplMatcher = compileRouter(maplRouter)
2024-11-03 09:16:11 +00:00
func.push([testStringsMapl, maplMatcher])
2024-11-02 12:44:54 +00:00
2024-11-03 09:16:11 +00:00
for (let [tests, fun] of func) {
2024-11-02 12:44:54 +00:00
console.log(`--- warming up ${fun.name || 'mapl'} ---`)
2024-11-03 09:16:11 +00:00
for (var i = 0; i < 100000; i++) {
2024-11-02 12:44:54 +00:00
tests.forEach(fun)
}
}
2024-11-03 09:16:11 +00:00
for (let [tests, fun] of func) {
2024-11-02 12:44:54 +00:00
console.log(`--- Sanity checking ${fun.name || 'mapl'} ---`)
for (let a of tests) {
// console.log(a, fun(a))
}
}
console.log('--- sleeping ---')
await new Promise(res => setTimeout(res, 1000))
for (let [_, org] of func) {
printCurrentStatus(org);
}
printStatusHelperText()
summary(() => {
2024-11-03 09:16:11 +00:00
func.forEach(function([tests, fun]) {
2024-11-02 12:44:54 +00:00
// console.log(tests, fun, tests.map(fun))
2024-11-03 09:16:11 +00:00
bench(fun.name || 'mapl', function() {
2024-11-02 12:44:54 +00:00
return tests.map(fun)
})
})
})
run().then(function() {
for (let [_, check] of func) {
printCurrentStatus(check);
}
printStatusHelperText()
});