Skip to content

Commit

Permalink
chore: ...
Browse files Browse the repository at this point in the history
Signed-off-by: Okiki <okikio.dev@gmail.com>
  • Loading branch information
okikio committed Apr 25, 2024
1 parent e294453 commit 99c1b27
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 58 deletions.
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Okiki Ojo
Copyright (c) 2024 Okiki Ojo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion benchmark/browsers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ConsoleMessage } from '@playwright/test';

Check failure on line 1 in benchmark/browsers.spec.ts

View workflow job for this annotation

GitHub Actions / build

[chromium] › browsers.spec.ts:12:1 › structuredClone (browser)

1) [chromium] › browsers.spec.ts:12:1 › structuredClone (browser) ──────────────────────────────── Test timeout of 3000000ms exceeded.

Check failure on line 1 in benchmark/browsers.spec.ts

View workflow job for this annotation

GitHub Actions / build

[chromium] › browsers.spec.ts:12:1 › structuredClone (browser)

1) [chromium] › browsers.spec.ts:12:1 › structuredClone (browser) ──────────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Test timeout of 3000000ms exceeded.

Check failure on line 1 in benchmark/browsers.spec.ts

View workflow job for this annotation

GitHub Actions / build

[chromium] › browsers.spec.ts:12:1 › structuredClone (browser)

1) [chromium] › browsers.spec.ts:12:1 › structuredClone (browser) ──────────────────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Test timeout of 3000000ms exceeded.

Check failure on line 1 in benchmark/browsers.spec.ts

View workflow job for this annotation

GitHub Actions / build

[chromium] › browsers.spec.ts:40:1 › MessageChannel (browser)

2) [chromium] › browsers.spec.ts:40:1 › MessageChannel (browser) ───────────────────────────────── Test timeout of 3000000ms exceeded.
import { test, expect } from '@playwright/test';
import { WriteFile } from './_fs.ts';
import { WriteFile } from './utils/_fs.ts';

const consoleLog = async (msg: ConsoleMessage) => {
const values: any[] = [];
Expand Down
10 changes: 3 additions & 7 deletions benchmark/fixtures/postMessageMessageChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default async function (e: MouseEvent): Promise<string> {
transferable: data_.transferable.length,
})

const counter = new Map<string, number>();
let counter = 0;
for (let index = 0; index <= Math.log2(MAX_SIZE); index++) {
const num = Math.pow(2, index);
const name = bytes(num, { maximumFractionDigits: 3 });
Expand All @@ -42,19 +42,15 @@ export default async function (e: MouseEvent): Promise<string> {
let channel = new MessageChannel();

const instanceKey = `#${index} ${name} -> ${variant}`;
counter.set(instanceKey, counter.get(instanceKey) ?? 0);
console.log(`${counter.get(instanceKey) ?? 0} - ${instanceKey}`);
console.log(`${counter++} - ${instanceKey}`);

bench(
variant,
async () => {
const prevCount = counter.get(instanceKey) ?? 0;

data = GenerateStub(num, IsClonable);
await AsyncPostMessagePromise?.(channel.port1, {
name, index: prevCount, variant, data: data!
name, index: counter, variant, data: data!
});
counter.set(instanceKey, prevCount + 1);
},
{
warmup: true,
Expand Down
10 changes: 3 additions & 7 deletions benchmark/fixtures/postMessageWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async function (e: MouseEvent): Promise<string> {
transferable: data_.transferable.length,
})

const counter = new Map<string, number>();
let counter = 0;
for (let index = 0; index <= Math.log2(MAX_SIZE) / 5; index += 3) {
const num = Math.pow(2, index);
const name = bytes(num, { maximumFractionDigits: 3 });
Expand All @@ -45,19 +45,15 @@ export default async function (e: MouseEvent): Promise<string> {
let worker: Worker | null = null;

const instanceKey = `#${index} ${name} -> ${variant}`;
counter.set(instanceKey, counter.get(instanceKey) ?? 0);
console.log(`${counter.get(instanceKey) ?? 0} - ${instanceKey}`);
console.log(`${counter++} - ${instanceKey}`);

bench(
variant,
async () => {
const prevCount = counter.get(instanceKey) ?? 0;

data = GenerateStub(num, IsClonable);
await AsyncPostMessagePromise?.(worker!, {
name, index: prevCount, variant, data: data!
name, index: counter, variant, data: data!
});
counter.set(instanceKey, prevCount + 1);
},
{
warmup: true,
Expand Down
6 changes: 3 additions & 3 deletions benchmark/fixtures/structuredClone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getTransferable, getTransferables, hasTransferables } from "../../src/m

import { CreateStructuredCloneVariants } from "../utils/_structuredclone.ts";
import { PrintMarkdownTable, GenerateStub, IsClonable } from "../utils/_utils.ts";
import { BITS_IN_BYTE, MAX_SIZE } from "../utils/_constants.ts";
import { MAX_SIZE } from "../utils/_constants.ts";

export default async function (e: MouseEvent): Promise<string> {
e.preventDefault();
Expand Down Expand Up @@ -33,9 +33,9 @@ export default async function (e: MouseEvent): Promise<string> {
})

const counter = new Map<string, number>();
for (let index = 0; index <= Math.log2(MAX_SIZE / BITS_IN_BYTE); index++) {
for (let index = 0; index <= Math.log2(MAX_SIZE); index++) {
const num = Math.pow(2, index);
const name = bytes(num * BITS_IN_BYTE, { maximumFractionDigits: 3 });
const name = bytes(num, { maximumFractionDigits: 3 });

group(name, () => {
for (const variant of variants) {
Expand Down
12 changes: 4 additions & 8 deletions benchmark/messagechannel.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ console.log({
transferable: data_.transferable.length,
})

const counter = new Map<string, number>();
let counter = 0;
for (let index = 0; index <= Math.log2(MAX_SIZE); index ++) {
const num = Math.pow(2, index);
const name = bytes(num, { maximumFractionDigits: 3 });
Expand All @@ -40,19 +40,15 @@ for (let index = 0; index <= Math.log2(MAX_SIZE); index ++) {
let channel = new MessageChannel();

const instanceKey = `#${index} ${name} -> ${variant}`;
counter.set(instanceKey, counter.get(instanceKey) ?? 0);
console.log(`${counter.get(instanceKey) ?? 0} - ${instanceKey}`);
console.log(`${counter++} - ${instanceKey}`);

bench(
variant,
async () => {
const prevCount = counter.get(instanceKey) ?? 0;

data = GenerateStub(num, IsClonable);
await AsyncPostMessagePromise?.(channel.port1, {
name, index: prevCount, variant, data: data!
});
counter.set(instanceKey, prevCount + 1);
name, index: counter, variant, data: data!
});
},
{
warmup: true,
Expand Down
10 changes: 3 additions & 7 deletions benchmark/structuredclone.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WriteFile } from "./utils/_fs.ts";

import { CreateStructuredCloneVariants } from "./utils/_structuredclone.ts";
import { PrintMarkdownTable, GenerateStub, IsClonable } from "./utils/_utils.ts";
import { BITS_IN_BYTE, MAX_SIZE } from "./utils/_constants.ts";
import { MAX_SIZE } from "./utils/_constants.ts";

const variantsFn = CreateStructuredCloneVariants({
hasTransferables, getTransferable, getTransferables
Expand All @@ -30,7 +30,7 @@ console.log({
transferable: data_.transferable.length,
})

const counter = new Map<string, number>();
let counter = 0;
for (let index = 0; index <= Math.log2(MAX_SIZE); index++) {
const num = Math.pow(2, index);
const name = bytes(num, { maximumFractionDigits: 3 });
Expand All @@ -40,18 +40,14 @@ for (let index = 0; index <= Math.log2(MAX_SIZE); index++) {
let data: ReturnType<typeof GenerateStub> | null = null;

const instanceKey = `#${index} ${name} -> ${variant}`;
counter.set(instanceKey, counter.get(instanceKey) ?? 0);
console.log(`${counter.get(instanceKey) ?? 0} - ${instanceKey}`);
console.log(`${counter++} - ${instanceKey}`);

bench(
variant,
async () => {
const prevCount = counter.get(instanceKey) ?? 0;

data = GenerateStub(num, IsClonable);
const fn = variantsFn[variant];
await fn?.(data!);
counter.set(instanceKey, prevCount + 1);
},
{
warmup: true,
Expand Down
2 changes: 1 addition & 1 deletion benchmark/utils/_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export const BITS_IN_MEGABYTE: number = BITS_IN_BYTE * BYTES_IN_MEGABYTE; // 8 b
* @constant
* @type {number}
*/
export const MAX_SIZE: number = 2 * BITS_IN_MEGABYTE; // 2 MB in bytes
export const MAX_SIZE: number = 0.0005 * BITS_IN_MEGABYTE; // 2 MB in bytes
4 changes: 2 additions & 2 deletions benchmark/utils/_fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export async function WriteFile(result: string, name: string, env: string) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const fileDir = path.join(__dirname, `/results`);
const filePath = path.join(__dirname, `/results/${name.toLowerCase()}.${env}.md`);
const fileDir = path.join(__dirname, "..", `/results`);
const filePath = path.join(__dirname, "..", `/results/${name.toLowerCase()}.${env}.md`);
console.log(`Writing/Appending to ${filePath}`)

const append = await exists(filePath);
Expand Down
14 changes: 5 additions & 9 deletions benchmark/worker.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ const env_ = (() => {
})();

console.log({
type: `MessageChannel (${env_})`,
type: `Worker (${env_})`,
name: name_,
transferable: data_.transferable.length,
})

const counter = new Map<string, number>();
let counter = 0;
for (let index = 0; index <= Math.log2(MAX_SIZE) / 5; index += 3) {
const num = Math.pow(2, index);
const name = bytes(num, { maximumFractionDigits: 3 });
Expand All @@ -42,19 +42,15 @@ for (let index = 0; index <= Math.log2(MAX_SIZE) / 5; index += 3) {
let worker: Worker | null = null;

const instanceKey = `#${index} ${name} -> ${variant}`;
counter.set(instanceKey, counter.get(instanceKey) ?? 0);
console.log(`${counter.get(instanceKey) ?? 0} - ${instanceKey}`);
console.log(`${counter++} - ${instanceKey}`);

bench(
variant,
async () => {
const prevCount = counter.get(instanceKey) ?? 0;

data = GenerateStub(num, IsClonable);
await AsyncPostMessagePromise?.(worker!, {
name, index: prevCount, variant, data: data!
name, index: counter, variant, data: data!
});
counter.set(instanceKey, prevCount + 1);
},
{
warmup: true,
Expand Down Expand Up @@ -87,4 +83,4 @@ const { benchmarks } = await run({
});

const result = PrintMarkdownTable(variants, benchmarks);
WriteFile(result, `MessageChannel`, env_);
WriteFile(result, `Worker`, env_);
9 changes: 1 addition & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,30 @@
"scripts": {
"start": "pnpm browser:fixture",
"dev": "pnpm browser:fixture",

"test": "vitest",
"test:run": "vitest run tests",

"build": "tsup",
"playwright:init": "playwright install --with-deps",
"browser:fixture": "vite build ./benchmark/fixtures --target \"es2022\" && vite preview ./benchmark/fixtures --port 3000",
"browser:fixture:dev": "vite dev ./benchmark/fixtures --port 3000",

"benchmark:browser": "playwright test ./benchmark/browsers.spec.ts",
"benchmark:browser:chrome": "rm -rf ./benchmark/results/chromium.md && playwright test --browser=chromium ./benchmark/browsers.spec.ts",
"benchmark:browser:firefox": "rm -rf ./benchmark/results/firefox.md && playwright test --browser=firefox ./benchmark/browsers.spec.ts",
"benchmark:browser:safari": "rm -rf ./benchmark/results/safari.md && playwright test --browser=webkit ./benchmark/browsers.spec.ts",

"benchmark:node": "tsx ./benchmark/structuredclone.bench.ts",
"benchmark:node:worker": "tsx ./benchmark/worker.bench.ts",
"benchmark:node:messagechannel": "tsx ./benchmark/messagechannel.bench.ts",
"benchmark:node:all": "rm -rf ./benchmark/results/node.md && pnpm benchmark:node && pnpm benchmark:node:worker && pnpm benchmark:node:messagechannel",

"benchmark:deno": "deno run --allow-run --allow-env --allow-read --allow-write --allow-hrtime --allow-sys ./benchmark/structuredclone.bench.ts",
"benchmark:deno:worker": "deno run --allow-run --allow-env --allow-read --allow-write --allow-hrtime --allow-sys ./benchmark/worker.bench.ts",
"benchmark:deno:messagechannel": "deno run --allow-run --allow-env --allow-read --allow-write --allow-hrtime --allow-sys ./benchmark/messagechannel.bench.ts",
"benchmark:deno:all": "rm -rf ./benchmark/results/deno.md && pnpm benchmark:deno && pnpm benchmark:deno:worker && pnpm benchmark:deno:messagechannel",

"benchmark:bun": "bun run ./benchmark/structuredclone.bench.ts",
"benchmark:bun:worker": "bun run ./benchmark/worker.bench.ts",
"benchmark:bun:messagechannel": "bun run ./benchmark/messagechannel.bench.ts",
"benchmark:bun:all": "rm -rf ./benchmark/results/bun.md && pnpm benchmark:bun && pnpm benchmark:bun:worker && pnpm benchmark:bun:messagechannel",

"benchmark": "pnpm benchmark:node:all && pnpm benchmark:deno:all && pnpm benchmark:bun:all",
"benchmark:all": "pnpm benchmark && pnpm benchmark:browser",

"commitlint": "commitlint --edit",
"pre-release": "pnpm test:run && pnpm build",
"semantic-release": "semantic-release",
Expand Down Expand Up @@ -159,6 +151,7 @@
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/git": "^10.0.1",
"@std/fmt": "npm:@jsr/std__fmt@^0.224.0",
"@swc/core": "^1.5.0",
"@types/node": "^20.12.7",
"@types/web": "^0.0.143",
"bun": "^1.1.4",
Expand Down
Loading

0 comments on commit 99c1b27

Please sign in to comment.