import { performance } from 'node:perf_hooks' import { chromium } from 'playwright' import { imhotep } from 'imhotep' import { mkdtempSync, writeFileSync, rmSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' const tmpdir2 = mkdtempSync(join(tmpdir(), 'imhotep-cache-hit-')) const html = `
${Array.from({length: 50}, (_, i) => `
${i}
`).join('')}
` writeFileSync(join(tmpdir2, 'page.html'), html) const browser = await chromium.launch() const page = await browser.newPage() await page.goto(`file:${join(tmpdir2, 'page.html')}`) const selectors = Array.from({length: 50}, (_, i) => `[data-testid="item-${i}"]`) // Warmup: run once to populate cache const ui1 = await imhotep(page) for (const sel of selectors) { ui1.expect(sel).to.be.leftOf('[data-testid="item-0"]') } await ui1.checkAll() console.log('=== Cache Hit Performance ===\n') for (const count of [1, 5, 10, 25, 50]) { const ui = await imhotep(page) for (let i = 0; i < count; i++) { ui.expect(selectors[i]).to.be.leftOf('[data-testid="item-0"]') } const times = [] for (let run = 0; run < 10; run++) { const start = performance.now() await ui.checkAll() times.push(performance.now() - start) } const mean = times.reduce((a,b) => a+b, 0) / times.length console.log(`${count} selectors (cache hit): ${mean.toFixed(2)}ms (avg of 10 runs)`) } await browser.close() rmSync(tmpdir2, { recursive: true, force: true })