Skip to content
Snippets Groups Projects
puppeteer.js 1.99 KiB
Newer Older
import puppeteer from "puppeteer-core"
import fs from "fs"
;(async () => {
  const browser = await puppeteer.launch({
    executablePath: "google-chrome",
    headless: true,
    args: ["--js-flags=--expose-gc"],
  })
  const page = await browser.newPage()

  const done = new Promise((resolve) => {
    page.on("console", (msg) => {
      if (msg.type() == "debug") {
        process.stderr.write(msg.text())
      } else if (msg.type() == "info") {
        const { filename, title, iterations, results } = JSON.parse(msg.text())

        if (title) {
          const columns = ["iterations"]

          for (const title of results) {
            columns.push(`${title}_timeLoc`)
            columns.push(`${title}_encodeRAM`)
            columns.push(`${title}_packets`)
            columns.push(`${title}_size`)
            columns.push(`${title}_timeRem`)
            columns.push(`${title}_decodeRAM`)
            columns.push(`${title}_events`)
          }

          fs.writeFileSync(
            filename,
            `# Benchmark: ${title}\n# ${columns.join("\t")}\n`,
          )
        } else {
          const columns = [iterations]

          for (const title in results) {
            const {
              timeLoc,
              encodeRAM,
              packets,
              size,
              timeRem,
              decodeRAM,
              events,
            } = results[title]

            columns.push(timeLoc)
            columns.push(encodeRAM)
            columns.push(packets)
            columns.push(size)
            columns.push(timeRem)
            columns.push(decodeRAM)
            columns.push(events)
          }

          fs.appendFileSync(filename, `${columns.join("\t")}\n`)
        }
      } else if (msg.type() == "log") {
        if (msg.text().startsWith("# failure")) {
          resolve()
        }

        process.stdout.write(msg.text() + "\n")
      }
    })
  })

  await page.goto("http://localhost:3000/benchmarks.html").catch(console.error)

  await done

  await browser.close()
})()