emoji-picker-element/test/memory/test.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-06-08 02:01:02 +02:00
import puppeteer from 'puppeteer'
import prettyBytes from 'pretty-bytes'
import { markdownTable as table } from 'markdown-table'
2020-06-08 02:24:56 +02:00
import fetch from 'node-fetch'
2020-06-08 02:01:02 +02:00
const scenarios = [
2020-06-08 02:24:56 +02:00
'blank',
2020-06-08 02:01:02 +02:00
'picker',
'compact',
'full'
]
2020-06-20 20:34:29 +02:00
async function waitForServerReady () {
while (true) {
try {
const resp = await fetch('http://localhost:3000')
if (resp.status === 200) {
break
}
} catch (err) {}
console.log('Waiting for localhost:3000 to be available')
await new Promise(resolve => setTimeout(resolve, 1000))
}
}
2020-06-08 02:01:02 +02:00
async function measureMemory (scenario) {
const browser = await puppeteer.launch({
2021-07-02 18:58:00 +02:00
headless: false // required for performance.measureUserAgentSpecificMemory()
2020-06-08 02:01:02 +02:00
})
const page = await browser.newPage()
await page.goto(`http://localhost:3000?${scenario}=1`, {
waitUntil: 'networkidle0'
})
2021-07-02 18:58:00 +02:00
const bytes = await page.evaluate(async () => {
// Taking two measurements and returning the second seems more accurate for some reason
await performance.measureUserAgentSpecificMemory()
return (await performance.measureUserAgentSpecificMemory()).bytes
})
2020-06-08 02:01:02 +02:00
await browser.close()
return bytes
}
2020-06-08 02:24:56 +02:00
function printBytes (bytes) {
return `${prettyBytes(bytes)} (${bytes})`
}
2020-06-08 02:01:02 +02:00
async function main () {
2020-06-20 20:34:29 +02:00
await waitForServerReady()
2020-06-08 02:01:02 +02:00
const results = []
for (const scenario of scenarios) {
const bytes = await measureMemory(scenario)
2020-06-08 02:24:56 +02:00
const relativeBytes = results.length ? (bytes - results[0].bytes) : 0
results.push({
2020-06-08 02:01:02 +02:00
scenario,
bytes,
2020-06-08 02:24:56 +02:00
relativeBytes
})
2020-06-08 02:01:02 +02:00
}
2020-06-08 02:24:56 +02:00
console.log(table([
['Scenario', 'Bytes', 'Relative to blank page'],
...results.map(({ scenario, bytes, relativeBytes }) => (
[scenario, printBytes(bytes), printBytes(relativeBytes)]
))
]))
2020-06-08 02:01:02 +02:00
}
main().catch(err => {
console.error(err)
process.exit(1)
})