generated from okikio/spring-easing
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add browser benchmarks via playwright
- Loading branch information
Showing
15 changed files
with
747 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,3 +107,6 @@ dist | |
lib/ | ||
docs/ | ||
@types/ | ||
/test-results/ | ||
/playwright-report/ | ||
/playwright/.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"deno.enablePaths": [ | ||
"./benchmark/deno.ts", | ||
"./benchmark/deno.worker.ts", | ||
"./benchmark/workers/deno.ts" | ||
], | ||
"deno.unstable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { test, expect } from '@playwright/test'; | ||
|
||
test('Benchmark transferables on browsers', async ({ page }) => { | ||
await page.goto('/'); | ||
|
||
// Expect a title "to contain" a substring. | ||
await expect(page).toHaveTitle(/Benchmark/); | ||
|
||
// create a locator | ||
const structuredCloneBtn = page.locator('button#structuredClone'); | ||
|
||
// Expect an attribute "to be strictly equal" to the value. | ||
await expect(structuredCloneBtn).toHaveAttribute('type', 'button'); | ||
|
||
// Click the get started link. | ||
await structuredCloneBtn.click(); | ||
|
||
// Expects the URL to contain intro. | ||
const structuredCloneResult = page.locator("#structuredCloneResult"); | ||
await structuredCloneResult.waitFor(); | ||
|
||
console.log(`structuredClone (browser)`) | ||
console.log(structuredCloneResult.getAttribute("data-value")) | ||
|
||
// create a locator | ||
const postMessageBtn = page.locator('button#postMessage'); | ||
|
||
// Expect an attribute "to be strictly equal" to the value. | ||
await expect(postMessageBtn).toHaveAttribute('type', 'button'); | ||
|
||
// Click the get started link. | ||
await postMessageBtn.click(); | ||
|
||
// Expects the URL to contain intro. | ||
const postMessageResult = page.locator("#postMessageResult"); | ||
await postMessageResult.waitFor(); | ||
|
||
console.log(`postMessage (browser)`) | ||
console.log(postMessageResult.getAttribute("data-value")) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="color-scheme" content="dark light" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Benchmark</title> | ||
</head> | ||
<body> | ||
<button id="structuredClone" type="button">structuredClone</button> | ||
<button id="postMessage" type="button">postMessage</button> | ||
|
||
<script type="module" src="./script.ts"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { MB, add, perfs, timeFormatter } from "../utils"; | ||
|
||
import bytes from "pretty-bytes"; | ||
import { dmeanstdev } from '@stdlib/stats-base'; | ||
|
||
import { markdownTable } from 'markdown-table'; | ||
|
||
interface IIterationType { | ||
name: string; | ||
variant: string; | ||
cycle: number; | ||
i: number; | ||
obj?: Record<string, unknown>; | ||
} | ||
|
||
interface ICreateWorkerIteratorOptions { | ||
index: number; | ||
variant: string; | ||
cycle?: number; | ||
} | ||
|
||
async function createWorkerPromise({ index, cycle = 0, variant }: ICreateWorkerIteratorOptions) { | ||
const worker = new Worker(new URL("../workers/worker.ts", import.meta.url).href, { type: "module" }); | ||
|
||
const num = Math.pow(2, index); | ||
const sizeStr = bytes(num, { maximumFractionDigits: 3 }); | ||
|
||
const msg = { name: sizeStr, variant, cycle, i: index }; | ||
worker.postMessage(msg); | ||
|
||
const data = await new Promise<IIterationType>(resolve => { | ||
worker.onmessage = ({ data }: MessageEvent<IIterationType>) => { | ||
resolve(data); | ||
} | ||
}); | ||
|
||
worker.terminate(); | ||
return data; | ||
} | ||
|
||
export default async function (e: MouseEvent) { | ||
e.preventDefault(); | ||
|
||
const variants = [`postMessage`, `postMessage (predefined)`, `hasTransferables`, `getTransferable`, `getTransferable(s)`]; | ||
const maxSize = 1.6; | ||
for (let cycle = 0; cycle < 5; cycle++) { | ||
for (let variant of variants) { | ||
for (let index = 0; index <= Math.log2(maxSize * MB); index++) { | ||
const num = Math.pow(2, index); | ||
const sizeStr = bytes(num, { maximumFractionDigits: 3 }); | ||
|
||
await add(sizeStr, variant, async () => { | ||
await createWorkerPromise({ index, variant, cycle }); | ||
}) | ||
} | ||
} | ||
} | ||
|
||
const Head = ["", ...variants]; | ||
const table: Record<string, string[]>[] = []; | ||
|
||
let strVal = 'Map {\n' | ||
perfs.forEach((variants, name) => { | ||
strVal += ` "${name}" => Map { `; | ||
|
||
const obj: Record<string, string[]> = {}; | ||
variants.forEach((durations, variant) => { | ||
const [mean, std] = dmeanstdev(durations.length, 0, new Float64Array(durations), 1, new Float64Array(2), 1); | ||
|
||
obj[name] ??= []; | ||
obj[name].push(`${timeFormatter.format(mean, "seconds")} ± ${timeFormatter.format(std, "seconds").replace("in ", "")}`); | ||
|
||
strVal += `"${variant}" => [${durations.map(x => timeFormatter.format(x, "seconds")).join(", ")}], ` | ||
|
||
}); | ||
|
||
table.push(obj); | ||
strVal += `},\n`; | ||
}) | ||
|
||
const str = table.map((x) => { | ||
const [key] = Object.keys(x); | ||
return [key, ...x[key]] | ||
}) | ||
|
||
strVal += `}`; | ||
|
||
const result = markdownTable([Head, ...str]) | ||
console.log(result); | ||
console.log(strVal); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import postMessageBenchmark from "./postMessage"; | ||
import structuredCloneBenchmark from "./structuredClone"; | ||
|
||
import { micromark } from 'micromark'; | ||
import { gfm, gfmHtml } from 'micromark-extension-gfm'; | ||
|
||
const structuredCloneBtn: HTMLButtonElement = document.querySelector("#structuredClone"); | ||
const postMessageBtn: HTMLButtonElement = document.querySelector("#postMessage"); | ||
|
||
const structuredCloneResultEl: HTMLDivElement = document.createElement("div"); | ||
const postMessageResultEl: HTMLDivElement = document.querySelector("div"); | ||
|
||
structuredCloneBtn?.addEventListener('click', async function (e: MouseEvent) { | ||
const result = await structuredCloneBenchmark(e); | ||
structuredCloneResultEl.id = "structuredCloneResult"; | ||
structuredCloneResultEl.dataset.value = result; | ||
structuredCloneResultEl.innerHTML = micromark(result, { | ||
extensions: [gfm()], | ||
htmlExtensions: [gfmHtml()] | ||
}); | ||
document.body.appendChild(structuredCloneResultEl); | ||
}) | ||
|
||
postMessageBtn?.addEventListener('click', async function (e: MouseEvent) { | ||
const result = await postMessageBenchmark(e); | ||
postMessageResultEl.id = "postMessageResult"; | ||
postMessageResultEl.dataset.value = result; | ||
postMessageResultEl.innerHTML = micromark(result, { | ||
extensions: [gfm()], | ||
htmlExtensions: [gfmHtml()] | ||
}); | ||
document.body.appendChild(postMessageResultEl); | ||
}) |
Oops, something went wrong.