askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
resources.mjs · 4.0 KB · 49 lines
| 1 | // What the browser is using: memory and CPU per tab, cache and disk, and which requests failed. |
| 2 | export default async function (ctx) { |
| 3 | const { call: T, check, base, sleep } = ctx; |
| 4 | const cdp = await ctx.chrome(); |
| 5 | const q = (js) => cdp.ev(`(() => { ${js} })()`); |
| 6 | |
| 7 | await T("page.goto", { url: base + "/free" }); |
| 8 | const r = await T("resources.state"); |
| 9 | check(r.totalMemoryMB > 50 && r.byType.browser?.memoryMB > 10 && r.byType.pages?.memoryMB > 5 && r.byType.graphics, "the status reports memory in total and by kind (pages, browser, graphics)", JSON.stringify(r.byType)); |
| 10 | const mine = r.tabs.find((t) => t.active); |
| 11 | check(mine && mine.pid > 0 && mine.memoryMB > 10 && typeof mine.cpu === "number", "each tab has its own page process with memory and CPU", JSON.stringify(mine)); |
| 12 | check(typeof r.cacheBytes === "number" && r.disk.bytes > 0 && Array.isArray(r.disk.top) && r.disk.top.length > 0 && r.disk.files > 0, "it reports the cache size and what the data folder is made of", JSON.stringify({ cache: r.cacheBytes, disk: r.disk.bytes, top: r.disk.top.slice(0, 3).map((d) => d.name) })); |
| 13 | |
| 14 | // a tab that holds a lot of memory shows up as using a lot |
| 15 | const before = (await T("resources.state")).tabs.find((t) => t.active).memoryMB; |
| 16 | const hog = await T("tabs.new", { url: base + "/hog" }); |
| 17 | await sleep(1500); |
| 18 | let after = 0; |
| 19 | for (let i = 0; i < 20 && after < before + 200; i++) { after = ((await T("resources.state")).tabs.find((t) => t.id === (hog.id ?? hog.tab?.id)) ?? {}).memoryMB ?? 0; await sleep(300); } |
| 20 | const state2 = await T("resources.state"); |
| 21 | const hogTab = state2.tabs.find((t) => /hog/.test(t.url)); |
| 22 | check(hogTab && hogTab.memoryMB > 300, "a tab that holds about 400 MB is shown holding that much", `${hogTab?.memoryMB} MB`); |
| 23 | check(state2.totalMemoryMB > r.totalMemoryMB + 250, "and the total goes up with it", `${r.totalMemoryMB} → ${state2.totalMemoryMB} MB`); |
| 24 | |
| 25 | // the window shows it |
| 26 | await cdp.ev(`setRes(false)`); |
| 27 | await sleep(2500); |
| 28 | check(await q(`return /^Memory [\\d.]+ (MB|GB)$/.test(document.getElementById('resBtn').textContent.trim())`), "the status bar shows the total memory in use", await q(`return document.getElementById('resBtn').textContent`)); |
| 29 | await cdp.ev(`setRes(true)`); |
| 30 | for (let i = 0; i < 40 && !(await q(`return document.querySelectorAll('#resPanel .trow').length >= 3`)); i++) await sleep(100); |
| 31 | const panel = await q(`return document.getElementById('resPanel').innerText`); |
| 32 | check(/what the browser is using/i.test(panel) && /tabs/i.test(panel) && /hog/i.test(panel) && /cache/i.test(panel) && /clear cache/i.test(panel) && /data folder/i.test(panel), "the panel lists every tab with its memory, the cache and the data folder", panel.replace(/\s+/g, " ").slice(0, 700)); |
| 33 | check(await q(`const rows = [...document.querySelectorAll('#resPanel .trow')].slice(0, 2); return /hog/i.test(rows[0].textContent)`), "the biggest tab is listed first"); |
| 34 | await q(`document.querySelector('#resPanel .trow .sbtn').click()`); // Close the biggest |
| 35 | for (let i = 0; i < 40 && (await T("tabs.list")).some((t) => /hog/.test(t.url)); i++) await sleep(100); |
| 36 | check(!(await T("tabs.list")).some((t) => /hog/.test(t.url)), "Close in the panel closes that tab"); |
| 37 | await cdp.ev(`setRes(false)`); |
| 38 | |
| 39 | // clearing the cache |
| 40 | const cleared = await T("resources.clearCache"); |
| 41 | check(typeof cleared.freedBytes === "number", "the cache can be cleared and says how much it held", JSON.stringify(cleared)); |
| 42 | |
| 43 | // which requests failed, named without their query |
| 44 | await T("tabs.activate", {}); |
| 45 | const v = await T("page.goto", { url: base + "/resources?secret=abc", settle: { timeoutMs: 8000 } }); |
| 46 | const res = v.settle.resources; |
| 47 | check(res.failedExamples?.some((f) => /127\.0\.0\.1:1\/x\.png$/.test(f.url)) && res.errorStatusExamples?.some((f) => /\/status\/404$/.test(f.url) && f.status === 404), "the load report names which requests failed and which got an error status", JSON.stringify({ failed: res.failedExamples, status: res.errorStatusExamples })); |
| 48 | check(!JSON.stringify(res).includes("secret=abc"), "and leaves out their query strings"); |
| 49 | } |