flaska/benchmark/router_flaska_v2.mjs
2024-11-03 09:27:39 +00:00

97 lines
4.4 KiB
JavaScript

import { summary, run, bench } from 'mitata';
import { createRouter, insertItem } from '@mapl/router'
import { compileRouter } from './mapl_compiler.mjs'
import { compilePaths } from "./router_v2.mjs"
import { compilePaths as mainCompiler } from "../router_v2.mjs"
import { FlaskaRouter as FlaskaRouterBuffer } from "../flaska_buffer.mjs"
import { FlaskaRouter as FlaskaRouterFast } from "../flaska_fast.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 = consts.allRoutes.map(x => ({ path: x }))
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))
let func = [[testStrings, mainCompiler(paths)]]
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])
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)
func.push([testStringsMapl, maplMatcher])
for (let [tests, fun] of func) {
console.log(`--- warming up ${fun.name || 'mapl'} ---`)
for (var i = 0; i < 100000; i++) {
tests.forEach(fun)
}
}
for (let [tests, fun] of func) {
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(() => {
func.forEach(function([tests, fun]) {
// console.log(tests, fun, tests.map(fun))
bench(fun.name || 'mapl', function() {
return tests.map(fun)
})
})
})
run().then(function() {
for (let [_, check] of func) {
printCurrentStatus(check);
}
printStatusHelperText()
});