Skip to content

Commit

Permalink
test: add examples and run them in the ci
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jun 21, 2023
1 parent 9b3bf03 commit 3ba37c8
Show file tree
Hide file tree
Showing 16 changed files with 2,695 additions and 5,445 deletions.
28 changes: 23 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,34 @@ jobs:
with:
run: pnpm moon run :bench

list-examples:
runs-on: "ubuntu-latest"
name: List examples
outputs:
examples: ${{ steps.list-examples.outputs.examples }}
steps:
- uses: "actions/checkout@v3"
# list the directories in ./examples and output them to a github action workflow variables as a JSON array
- run: |
examples=$(find ./examples -maxdepth 1 -mindepth 1 -type d -printf '%f\n' | jq -R -s -c 'split("\n") | map(select(length > 0))')
echo "::set-output name=examples::$examples"
id: list-examples
node-versions:
runs-on: "ubuntu-latest"
name: "Node ${{ matrix.node-version }}"
name: "${{ matrix.example }} on Node ${{ matrix.node-version }}"
needs: list-examples
strategy:
matrix:
node-version: ["14", "16", "18", "20"]
example: ${{ fromJson(needs.list-examples.outputs.examples) }}
include:
- node-version: "14"
pnpm-version: "7"
skip-tinybench: true
exclude:
- node-version: "14"
example: with-typescript-module
fail-fast: false
steps:
- uses: "actions/checkout@v3"
Expand All @@ -66,16 +84,16 @@ jobs:
- run: pnpm install --frozen-lockfile --prefer-offline
- run: pnpm moon run :build

- name: Run benchmarks on tinybench-plugin
- name: Run benchmarks with tinybench-plugin
if: matrix.skip-tinybench != true
uses: CodSpeedHQ/action@feat/node-script
with:
run: pnpm moon run tinybench-plugin:bench
run: pnpm --filter ${{ matrix.example }} bench-tinybench
env:
SKIP_UPLOAD: true
- name: Run benchmarks on benchmark.js-plugin
- name: Run benchmarks with benchmark.js-plugin
uses: CodSpeedHQ/action@feat/node-script
with:
run: pnpm moon run benchmark.js-plugin:bench
run: pnpm --filter ${{ matrix.example }} bench-benchmark-js
env:
SKIP_UPLOAD: true
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
18 changes: 18 additions & 0 deletions examples/with-javascript-cjs/benchmark-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { withCodSpeed } = require("@codspeed/benchmark.js-plugin");
const Benchmark = require("benchmark");

const suite = withCodSpeed(new Benchmark.Suite());

suite
.add("RegExp#test", function () {
/o/.test("Hello World!");
})
.add("String#indexOf", function () {
"Hello World!".indexOf("o") > -1;
})
// add listeners
.on("cycle", function (event) {
console.log(String(event.target));
})
// run async
.run({ async: true });
14 changes: 14 additions & 0 deletions examples/with-javascript-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "with-javascript-cjs",
"private": true,
"scripts": {
"bench-benchmark-js": "node benchmark-js.js",
"bench-tinybench": "node tinybench.js"
},
"devDependencies": {
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0",
"@codspeed/tinybench-plugin": "workspace:^1.1.0",
"benchmark": "^2.1.4",
"tinybench": "^2.5.0"
}
}
30 changes: 30 additions & 0 deletions examples/with-javascript-cjs/tinybench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { withCodSpeed } = require("@codspeed/tinybench-plugin");
const { Bench } = require("tinybench");

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

bench
.add("switch 1", () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add("switch 2", () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

bench.run().then(() => {
console.table(
bench.tasks.map(({ name, result }) => ({
"Task Name": name,
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A",
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A",
}))
);
});
18 changes: 18 additions & 0 deletions examples/with-javascript-esm/benchmark-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withCodSpeed } from "@codspeed/benchmark.js-plugin";
import Benchmark from "benchmark";

const suite = withCodSpeed(new Benchmark.Suite());

suite
.add("RegExp#test", function () {
/o/.test("Hello World!");
})
.add("String#indexOf", function () {
"Hello World!".indexOf("o") > -1;
})
// add listeners
.on("cycle", function (event) {
console.log(String(event.target));
})
// run async
.run({ async: true });
15 changes: 15 additions & 0 deletions examples/with-javascript-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "with-javascript-esm",
"private": true,
"type": "module",
"scripts": {
"bench-benchmark-js": "node benchmark-js.js",
"bench-tinybench": "node tinybench.js"
},
"devDependencies": {
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0",
"@codspeed/tinybench-plugin": "workspace:^1.1.0",
"benchmark": "^2.1.4",
"tinybench": "^2.5.0"
}
}
30 changes: 30 additions & 0 deletions examples/with-javascript-esm/tinybench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { withCodSpeed } from "@codspeed/tinybench-plugin";
import { Bench } from "tinybench";

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

bench
.add("switch 1", () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add("switch 2", () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

bench.run().then(() => {
console.table(
bench.tasks.map(({ name, result }) => ({
"Task Name": name,
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A",
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A",
}))
);
});
18 changes: 18 additions & 0 deletions examples/with-typescript-module/benchmark-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withCodSpeed } from "@codspeed/benchmark.js-plugin";
import Benchmark from "benchmark";

const suite = withCodSpeed(new Benchmark.Suite());

suite
.add("RegExp#test", function () {
/o/.test("Hello World!");
})
.add("String#indexOf", function () {
"Hello World!".indexOf("o") > -1;
})
// add listeners
.on("cycle", function (event) {
console.log(String(event.target));
})
// run async
.run({ async: true });
17 changes: 17 additions & 0 deletions examples/with-typescript-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "with-typescript-module",
"private": true,
"type": "module",
"scripts": {
"bench-benchmark-js": "node --loader esbuild-register/loader -r esbuild-register benchmark-js.ts",
"bench-tinybench": "node --loader esbuild-register/loader -r esbuild-register tinybench.ts"
},
"devDependencies": {
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0",
"@codspeed/tinybench-plugin": "workspace:^1.1.0",
"benchmark": "^2.1.4",
"esbuild-register": "^3.4.2",
"tinybench": "^2.5.0",
"typescript": "^5.1.3"
}
}
30 changes: 30 additions & 0 deletions examples/with-typescript-module/tinybench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { withCodSpeed } from "@codspeed/tinybench-plugin";
import { Bench } from "tinybench";

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

bench
.add("switch 1", () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add("switch 2", () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

bench.run().then(() => {
console.table(
bench.tasks.map(({ name, result }) => ({
"Task Name": name,
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A",
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A",
}))
);
});
18 changes: 18 additions & 0 deletions examples/with-typescript/benchmark-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withCodSpeed } from "@codspeed/benchmark.js-plugin";
import Benchmark from "benchmark";

const suite = withCodSpeed(new Benchmark.Suite());

suite
.add("RegExp#test", function () {
/o/.test("Hello World!");
})
.add("String#indexOf", function () {
"Hello World!".indexOf("o") > -1;
})
// add listeners
.on("cycle", function (event) {
console.log(String(event.target));
})
// run async
.run({ async: true });
16 changes: 16 additions & 0 deletions examples/with-typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "with-typescript",
"private": true,
"scripts": {
"bench-benchmark-js": "node -r esbuild-register benchmark-js.ts",
"bench-tinybench": "node -r esbuild-register tinybench.ts"
},
"devDependencies": {
"@codspeed/benchmark.js-plugin": "workspace:^1.1.0",
"@codspeed/tinybench-plugin": "workspace:^1.1.0",
"benchmark": "^2.1.4",
"esbuild-register": "^3.4.2",
"tinybench": "^2.5.0",
"typescript": "^5.1.3"
}
}
30 changes: 30 additions & 0 deletions examples/with-typescript/tinybench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { withCodSpeed } from "@codspeed/tinybench-plugin";
import { Bench } from "tinybench";

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

bench
.add("switch 1", () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add("switch 2", () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

bench.run().then(() => {
console.table(
bench.tasks.map(({ name, result }) => ({
"Task Name": name,
"Average Time (ps)": result?.mean ? result.mean * 1000 : "N/A",
"Variance (ps)": result?.variance ? result.variance * 1000 : "N/A",
}))
);
});
Loading

0 comments on commit 3ba37c8

Please sign in to comment.