Skip to content

Commit

Permalink
Add more benchmarks!
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbhmr committed Jul 8, 2023
1 parent a16072a commit d48c124
Show file tree
Hide file tree
Showing 26 changed files with 459 additions and 284 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test-deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Test Deno
on:
push:
branches: "main"
paths-ignore:
- .gitignore
- LICENSE
- README.md
- "!.github/workflows/test-deno.yml"
pull_request:
branches: "main"
paths:
- .gitignore
- LICENSE
- README.md
- "!.github/workflows/test-deno.yml"
concurrency:
group: ${{ github.workflow }}-${{ github.ref_name }}
cancel-in-progress: true
jobs:
test-deno:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: "20"
cache: npm
- uses: denoland/setup-deno@v1
- run: deno eval --node-modules-dir 'import "npm:[email protected]"'
- run: npm ci
- run: npm run test:deno
- run: npm run bench:deno
1 change: 1 addition & 0 deletions .github/workflows/test-nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jobs:
cache: npm
- run: npm ci
- run: npm run test:node
- run: npm run bench:node
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@
],
"scripts": {
"lint": "prettier --ignore-path .gitignore -w .",
"test": "npm run test:node && npm run test:deno",
"test:node": "node --test test/greenlet-node.test.js test/greenlet-node&esmurl.test.js test/redlet-node.test.js test/redlet-node&esmurl.test.js",
"test:deno": "cross-env DENO_NO_PACKAGE_JSON=1 deno test -A --node-modules-dir=false test/redlet-deno.test.js test/redlet-deno+esmurl.test.js",
"test": "npm run test:node && npm run test:deno && npm run bench",
"test:node": "node --test test/e2e.test-node.js",
"test:deno": "deno test -A test/e2e.test-deno.js",
"test:browser": "serve",
"bench": "node test/greenlet-node.bench.js && node test/async-to-sync-comparison.bench.js",
"bench": "npm run bench:node && npm run bench:deno",
"bench:node": "node test/async-to-sync-noop.bench.js; node test/async-to-sync-readFile.bench-node.js; node test/async-vs-sync-concat.bench-node.js",
"bench:deno": "deno run -A test/async-to-sync-readFile.bench-deno.js; deno run -A test/async-vs-sync-concat.bench-deno.js",
"bench:browser": "serve",
"postinstall": "patch-package"
},
"devDependencies": {
"@types/node": "^20.2.5",
"await-sync": "^1.0.1",
"await-sync": "^1.0.3",
"cross-env": "^7.0.3",
"deasync": "^0.1.28",
"esmurl": "^0.5.0",
Expand Down
58 changes: 0 additions & 58 deletions patches/await-sync+1.0.1.patch

This file was deleted.

1 change: 1 addition & 0 deletions src/greenlet-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function getWorker() {
});
const worker = new NodeWorker(`import(${JSON.stringify(u)})`, {
eval: true,
// @ts-ignore
name: "greenlet",
});
worker.unref();
Expand Down
44 changes: 44 additions & 0 deletions test/async-to-sync-noop.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { writeFile } from "node:fs/promises";
import { Bench } from "tinybench";
import { createWorker } from "await-sync";
import makeSynchronous from "make-synchronous";
import { createSyncFn } from "synckit";
import redlet from "../src/redlet-node.js";
import { unlinkSync } from "node:fs";

const bench = new Bench({ time: 3000 });

// prettier-ignore
await writeFile("synckit-runAsWorker (1).mjs", `
import { runAsWorker } from "synckit";
runAsWorker(() => {});
`);
process.on("exit", () => {
unlinkSync("synckit-runAsWorker (1).mjs");
});
const synckit_noop = createSyncFn(
process.cwd() + "/synckit-runAsWorker (1).mjs"
);
bench.add("noop: synckit", () => {
synckit_noop();
});

const tinylet_noop = redlet(() => {});
bench.add("noop: tinylet", () => {
tinylet_noop();
});

const await_sync_noop = createWorker()(() => {});
bench.add("noop: await-sync", () => {
await_sync_noop();
});

const make_synchronous_noop = makeSynchronous(() => {});
bench.add("noop: make-synchronous", () => {
make_synchronous_noop();
});

await bench.warmup();
await bench.run();
console.table(bench.table());
process.exit();
35 changes: 35 additions & 0 deletions test/async-to-sync-readFile.bench-deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Bench } from "npm:tinybench";
import redlet from "../src/redlet-deno+default.js";
import { createWorker } from "npm:await-sync";
import { writeFile } from "node:fs/promises";
import { unlinkSync } from "node:fs";
import process from "node:process";

await writeFile("test.log", "test\n".repeat(1000));
process.on("exit", () => {
unlinkSync("test.log");
});
const testFile = process.cwd() + "/test.log";

const bench = new Bench({ time: 3000 });

const tinylet_readFile = redlet(async (f) => {
const { readFile } = await import("node:fs/promises");
return await readFile(f);
});
bench.add("tinylet readFile()", () => {
tinylet_readFile(testFile);
});

const await_sync_readFile = createWorker()(async (f) => {
const { readFile } = await import("node:fs/promises");
return await readFile(f);
});
bench.add("await-sync readFile()", () => {
await_sync_readFile(testFile);
});

await bench.warmup();
await bench.run();
console.table(bench.table());
process.exit();
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,60 @@ import redlet from "../src/redlet-node.js";
import { createWorker } from "await-sync";
import makeSynchronous from "make-synchronous";
import { createSyncFn } from "synckit";
import { readFile, writeFile } from "node:fs/promises";
import { appendFileSync, unlinkSync } from "node:fs";
import { writeFile } from "node:fs/promises";
import { unlinkSync } from "node:fs";

const bench = new Bench({ time: 3000 });

await writeFile("test.log", "test\n".repeat(1000));
process.on("exit", () => {
unlinkSync("test.log");
});
const testFile = process.cwd() + "/test.log";

const bench = new Bench({ time: 3000 });

// prettier-ignore
await writeFile("synckit-runAsWorker.mjs", `
await writeFile("synckit-runAsWorker (2).mjs", `
import { runAsWorker } from "synckit";
runAsWorker(async (f) => {
const { readFile } = await import("node:fs/promises");
return await readFile(f);
})
`);
process.on("exit", () => {
unlinkSync("synckit-runAsWorker.mjs");
unlinkSync("synckit-runAsWorker (2).mjs");
});
const synckit = createSyncFn(process.cwd() + "/synckit-runAsWorker.mjs");
bench.add("synckit", () => {
synckit(testFile);
const synckit_readFile = createSyncFn(
process.cwd() + "/synckit-runAsWorker (2).mjs"
);
bench.add("readFile(): synckit", () => {
synckit_readFile(testFile);
});

const tinylet = redlet(async (f) => {
const tinylet_readFile = redlet(async (f) => {
const { readFile } = await import("node:fs/promises");
return await readFile(f);
});
bench.add("tinylet", () => {
tinylet(testFile);
bench.add("readFile(): tinylet", () => {
tinylet_readFile(testFile);
});

const await_sync = createWorker()(async (f) => {
const await_sync_readFile = createWorker()(async (f) => {
const { readFile } = await import("node:fs/promises");
return await readFile(f);
});
bench.add("await-sync", () => {
await_sync(testFile);
bench.add("readFile(): await-sync", () => {
await_sync_readFile(testFile);
});

const make_synchronous = makeSynchronous(async (f) => {
const make_synchronous_readFile = makeSynchronous(async (f) => {
const { readFile } = await import("node:fs/promises");
return await readFile(f);
});
bench.add("make-synchronous", () => {
make_synchronous(testFile);
bench.add("readFile(): make-synchronous", () => {
make_synchronous_readFile(testFile);
});

await bench.warmup();
await bench.run();
console.table(bench.table());
process.exit();
37 changes: 37 additions & 0 deletions test/async-vs-sync-concat.bench-deno.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Bench } from "npm:tinybench";
import redlet from "../src/redlet-deno+default.js";
import greenlet from "../src/greenlet-deno+default.js";
import process from "node:process";

const bench = new Bench({ time: 1000 });

const g = greenlet((a, b) => a + b);
const r = redlet((a, b) => a + b);

bench.add("greenlet: 200x 'A' + 200x 'B' concat", async () => {
await g("A".repeat(200), "B".repeat(200));
});

bench.add("redlet: 200x 'A' + 200x 'B' concat", () => {
r("A".repeat(200), "B".repeat(200));
});

bench.add("greenlet: 1000x 'A' + 1000x 'B' concat", async () => {
await g("A".repeat(1000), "B".repeat(1000));
});

bench.add("redlet: 1000x 'A' + 1000x 'B' concat", () => {
r("A".repeat(1000), "B".repeat(1000));
});

bench.add("greenlet: 10kb 'A' + 10kb 'B' concat", async () => {
await g("A".repeat(10 * 1024), "B".repeat(10 * 1024));
});

bench.add("redlet: 10kb 'A' + 10kb 'B' concat", () => {
r("A".repeat(10 * 1024), "B".repeat(10 * 1024));
});

await bench.run();
console.table(bench.table());
process.exit();
35 changes: 35 additions & 0 deletions test/async-vs-sync-concat.bench-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Bench } from "tinybench";
import { redlet, greenlet } from "../src/index.js";

const bench = new Bench({ time: 1000 });

const g = greenlet((a, b) => a + b);
const r = redlet((a, b) => a + b);

bench.add("greenlet: 200x 'A' + 200x 'B' concat", async () => {
await g("A".repeat(200), "B".repeat(200));
});

bench.add("redlet: 200x 'A' + 200x 'B' concat", () => {
r("A".repeat(200), "B".repeat(200));
});

bench.add("greenlet: 1000x 'A' + 1000x 'B' concat", async () => {
await g("A".repeat(1000), "B".repeat(1000));
});

bench.add("redlet: 1000x 'A' + 1000x 'B' concat", () => {
r("A".repeat(1000), "B".repeat(1000));
});

bench.add("greenlet: 10kb 'A' + 10kb 'B' concat", async () => {
await g("A".repeat(10 * 1024), "B".repeat(10 * 1024));
});

bench.add("redlet: 10kb 'A' + 10kb 'B' concat", () => {
r("A".repeat(10 * 1024), "B".repeat(10 * 1024));
});

await bench.run();
console.table(bench.table());
process.exit();
Loading

0 comments on commit d48c124

Please sign in to comment.