Skip to content

Commit

Permalink
Refactor benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
remko committed Oct 28, 2023
1 parent ec30950 commit c0e82c4
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 39 deletions.
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ if (watch) {
}
}).listen(8080);

console.log("listening on port 8080");
console.log("listening on http://localhost:8080");

const webCtx = await esbuild.context(webBuildOptions);
webCtx.watch();
Expand Down
56 changes: 30 additions & 26 deletions src/benchmarks/benchmarks.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
import mandelbrot from "./mandelbrot.tal";
import Uxn from "../uxn";
import suite from "./suite";

const DEV_OFFSET = 0x10200;
const PROGRAM_OFFSET = 0x100;

document.body.innerHTML = `
<h1>Uxn.wasm Benchmarks</h1>
<div>
<button id="startButton">Start</button>
<table>
<tr id="mandelbrotResults"></tr>
<table id="results">
</table>
</div>
`;
const startButtonEl = document.getElementById("startButton");
const mandelbrotResultsEl = document.getElementById("mandelbrotResults");
const resultsEl = document.getElementById("results");

function runMandelbrot(uxn) {
uxn.load(mandelbrot);
uxn.eval(PROGRAM_OFFSET);
}
startButtonEl.onclick = async () => {
startButtonEl.disabled = true;
resultsEl.replaceChildren();
for (const b of suite) {
const trEl = document.createElement("table");
resultsEl.appendChild(trEl);
const thEl = document.createElement("th");
thEl.appendChild(document.createTextNode(b.name));
trEl.appendChild(thEl);

async function runMandelbrotBenchmark() {
try {
startButtonEl.disabled = true;
const uxn = new Uxn();
await uxn.init({
deo: () => {},
dei: (port) => {
return uxn.ram[DEV_OFFSET + port];
},
});
mandelbrotResultsEl.innerHTML = "<th>Mandelbrot</th>";

for (let i = 0; i < 5; i++) {
const el = document.createElement("td");
mandelbrotResultsEl.appendChild(el);
el.appendChild(document.createTextNode("⌛️"));
const t1 = performance.now();
runMandelbrot(uxn);
const t2 = performance.now();
el.innerHTML = ((t2 - t1) / 1000.0).toFixed(2) + "s";
const tdEl = document.createElement("td");
const valueEl = document.createTextNode("⌛️");
tdEl.appendChild(valueEl);
trEl.appendChild(tdEl);
let value;

try {
const t1 = performance.now();
b.run(uxn);
const t2 = performance.now();
value = ((t2 - t1) / 1000.0).toFixed(2) + "s";
} catch (e) {
console.error(e);
value = "error: " + e;
}
tdEl.replaceChild(document.createTextNode(value), valueEl);
}
} finally {
startButtonEl.disabled = false;
}
}

startButtonEl.onclick = () => {
runMandelbrotBenchmark();
startButtonEl.disabled = false;
};
25 changes: 25 additions & 0 deletions src/benchmarks/benchmarks.node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const suite = require("./suite").default;
const Uxn = require("../uxn");

const DEV_OFFSET = 0x10200;

(async () => {
const uxn = new Uxn();
await uxn.init({
deo: () => {},
dei: (port) => {
return uxn.ram[DEV_OFFSET + port];
},
});
for (const b of suite) {
console.log(b.name);
for (let i = 0; i < 5; i++) {
let value;
const t1 = performance.now();
b.run(uxn);
const t2 = performance.now();
value = ((t2 - t1) / 1000.0).toFixed(2) + "s";
console.log(` run ${i + 1}: ` + value);
}
}
})();
13 changes: 13 additions & 0 deletions src/benchmarks/suite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import mandelbrot from "./mandelbrot.tal";

const PROGRAM_OFFSET = 0x100;

export default [
{
name: "mandelbrot",
run: (uxn) => {
uxn.load(mandelbrot);
uxn.eval(PROGRAM_OFFSET);
},
},
];
54 changes: 42 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ const { uxntalPlugin } = require("./scripts/esbuild/uxntal");
const Mocha = require("mocha");

let watch = false;
let benchmarks = false;
for (const arg of process.argv.slice(2)) {
switch (arg) {
case "--watch":
watch = true;
break;
case "--benchmarks":
benchmarks = true;
break;
}
}

Expand All @@ -26,7 +30,6 @@ function runTests() {
const buildOptions = {
bundle: true,
logLevel: "warning",
entryPoints: [path.join(__dirname, "src", "tests", "tests.node")],
target: "node17",
outdir: path.join(__dirname, "build"),
external: ["fs", "stream", "util", "events", "path"],
Expand All @@ -36,11 +39,15 @@ const buildOptions = {
".rom": "binary",
},
sourcemap: true,
plugins: [
wasmTextPlugin({ debug: true }),
uxntalPlugin(),
plugins: [wasmTextPlugin({ debug: true }), uxntalPlugin()],
};

const testBuildOptions = {
...buildOptions,
entryPoints: [path.join(__dirname, "src", "tests", "tests.node")],
plugins: buildOptions.plugins.concat([
{
name: "watch",
name: "runTests",
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) {
Expand All @@ -50,14 +57,37 @@ const buildOptions = {
});
},
},
],
]),
};

if (watch) {
(async () => {
const ctx = await esbuild.context(buildOptions);
ctx.watch();
})();
const benchmarkBuildOptions = {
...buildOptions,
entryPoints: [path.join(__dirname, "src", "benchmarks", "benchmarks.node")],
plugins: buildOptions.plugins.concat([
{
name: "runBenchmarks",
setup(build) {
build.onEnd((result) => {
if (result.errors.length > 0) {
return;
}
delete require.cache[path.join(__dirname, "build", "benchmarks.js")];
require("./build/benchmarks.js");
});
},
},
]),
};

if (benchmarks) {
esbuild.build(benchmarkBuildOptions);
} else {
esbuild.build(buildOptions);
if (watch) {
(async () => {
const ctx = await esbuild.context(testBuildOptions);
ctx.watch();
})();
} else {
esbuild.build(testBuildOptions);
}
}

0 comments on commit c0e82c4

Please sign in to comment.