mirror of
https://github.com/stablyai/orca.git
synced 2026-11-02 10:08:13 +00:00
* test: strengthen transport recovery and benchmark assertions * test: use preferred array type in reconnect fixture --------- Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { performance } from 'node:perf_hooks'
|
|
import { extractIconHref } from '../../src/main/repo-icon-source-href.ts'
|
|
|
|
// Regex comparator retained for the before/after measurement.
|
|
const html =
|
|
/<link\b(?=[^>]*\brel=["'](?:icon|shortcut icon)["'])(?=[^>]*\bhref=["']([^"'?]+))[^>]*>/i
|
|
const object =
|
|
/(?=[^}]*\brel\s*:\s*["'](?:icon|shortcut icon)["'])(?=[^}]*\bhref\s*:\s*["']([^"'?]+))[^}]*/i
|
|
const original = (source) => source.match(html)?.[1] ?? source.match(object)?.[1] ?? null
|
|
|
|
function measurePair(source) {
|
|
original(source)
|
|
extractIconHref(source)
|
|
const beforeSamples = []
|
|
const afterSamples = []
|
|
for (let run = 0; run < 5; run++) {
|
|
const measurements = [
|
|
[original, beforeSamples],
|
|
[extractIconHref, afterSamples]
|
|
]
|
|
if (run % 2 === 1) {
|
|
measurements.reverse()
|
|
}
|
|
for (const [fn, samples] of measurements) {
|
|
const started = performance.now()
|
|
fn(source)
|
|
samples.push(performance.now() - started)
|
|
}
|
|
}
|
|
return {
|
|
beforeMs: beforeSamples.sort((a, b) => a - b)[2],
|
|
afterMs: afterSamples.sort((a, b) => a - b)[2]
|
|
}
|
|
}
|
|
|
|
for (const [source, expected] of [
|
|
['<link rel="icon" href="/favicon.ico?version=1">', '/favicon.ico'],
|
|
["<link href='/shortcut.svg' rel='shortcut icon'>", '/shortcut.svg'],
|
|
['{ rel: "icon", href: "/object.png?version=1" }', '/object.png'],
|
|
["{ href: '/shortcut-object.svg', rel: 'shortcut icon' }", '/shortcut-object.svg']
|
|
]) {
|
|
assert.equal(original(source), expected)
|
|
assert.equal(extractIconHref(source), expected)
|
|
}
|
|
|
|
const results = []
|
|
for (const size of [8192, 16384, 32768]) {
|
|
for (const variant of ['no icon', 'rel without href', 'unterminated link starts']) {
|
|
const source =
|
|
variant === 'unterminated link starts'
|
|
? '<link '.repeat(Math.floor(size / 6))
|
|
: 'a'.repeat(size) + (variant === 'rel without href' ? ' rel:"icon"' : '')
|
|
assert.equal(extractIconHref(source), original(source))
|
|
const { beforeMs, afterMs } = measurePair(source)
|
|
results.push({
|
|
variant,
|
|
bytes: Buffer.byteLength(source),
|
|
beforeMs,
|
|
afterMs,
|
|
speedup: beforeMs / afterMs
|
|
})
|
|
}
|
|
}
|
|
console.log(JSON.stringify({ node: process.version, platform: process.platform, results }, null, 2))
|