Skip to content

Commit

Permalink
+time metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Mar 15, 2024
1 parent b7eded3 commit 34d17d2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
20 changes: 13 additions & 7 deletions source/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { existsSync } from "https://deno.land/[email protected]/fs/mod.ts";
import * as colors from "https://deno.land/[email protected]/fmt/colors.ts";

import Function from "~/compiler/function.ts";
import Package from "./compiler/package.ts";
import { Panic } from "~/helper.ts";
import Package from "~/compiler/package.ts";
import Project from "~/compiler/project.ts";
import { DisplayTimers, TimerStart, TimerEnd } from "~/helper.ts";
import { Panic } from "~/helper.ts";

if (Deno.args.includes("--version")) {
console.log("version: 0.0.0");
Expand Down Expand Up @@ -35,28 +36,33 @@ if (!(mainFunc instanceof Function)) {
Panic(`Main namespace is not a function: ${mainFunc.constructor.name}`);
}


TimerStart("compilation");
mainFunc.compile();
TimerEnd("compilation");

if (project.failed) Panic(`Compilation ${colors.red("Failed")}`);

if (!mainFunc.ref) Panic(`Main function not compiled correctly`);
project.module.exportFunction("main", mainFunc.ref);

TimerStart("serialize");
await Deno.writeFile("out.wasm", project.module.toBinary());
TimerEnd("serialize");

TimerStart("wasm2wat");
const command = new Deno.Command(
"wasm2wat",
{
args: ["-v", "out.wasm", "-o", "out.wat"]
}
{ args: ["-v", "out.wasm", "-o", "out.wat"] }
);
const { code, stdout, stderr } = await command.output();
if (code !== 0) {
console.error("Invalid wasm generated");
console.error(new TextDecoder().decode(stderr));
Deno.exit(1);
}
TimerEnd("wasm2wat");
console.log(new TextDecoder().decode(stdout));

console.log(` out: ${"out.wasm"}`);
console.log(` out: ${"out.wasm"}\n`);

if (Deno.args.includes("--time")) DisplayTimers();
36 changes: 36 additions & 0 deletions source/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,40 @@ export function AlignDownInteger(x: number, multiple: number) {
return remainder !== 0
? x - remainder
: x;
}




const _timer: { [key: string]: { start: number, end: number } } = {};
export function TimerStart(key: string) {
const now = Date.now();
_timer[key] = { start: now, end: now-1 };
}

export function TimerEnd(key: string) {
const now = Date.now();
if (!_timer[key]) return;
_timer[key].end = now;
}

export function DisplayTimers() {
const lines: string[][] = [];
const max = [0, 0];
for (const key in _timer) {
const entry = _timer[key];
const line = [key, (entry.end - entry.start)+"ms"];

max[0] = Math.max(max[0], line[0].length);
max[1] = Math.max(max[1], line[1].length);

lines.push(line);
}

let str = "Timers:";
for (const line of lines) {
str += `\n ${line[0].padEnd(max[0])} ${line[1].padStart(max[1])}`;
}

console.info(str);
}

0 comments on commit 34d17d2

Please sign in to comment.