diff --git a/data/generated.ts b/data/generated.ts index 2f32f94..87d657c 100644 --- a/data/generated.ts +++ b/data/generated.ts @@ -12,9 +12,11 @@ async function main() { .pipeThrough(new TextLineStream()); const stats = new Tracker(); + stats.update([0, "total", Number(Temporal.Now.instant().epochNanoseconds)]); try { for await (const line of lines) stats.update(JSON.parse(line) as Msg); } catch (_) { /* STDOUT closed */ } + stats.update([1, "total", Number(Temporal.Now.instant().epochNanoseconds)]); stats.log(); } @@ -41,7 +43,8 @@ class Tracker { } log() { - for (const section of this.register.values()) section.log(); + const total = this.register.get("total")?.average(); + for (const section of this.register.values()) section.log(total); Deno.exit(0); } } @@ -69,11 +72,21 @@ class SectionTracker { this.runs.push(duration); } - log() { + log(total_average: number | undefined) { const count = this.runs.length; - const avg = this.average() / 1_000; - const line = \`\${this.name} :\\t\${count} runs, \${avg} µs avg\`; - console.log(line); + const avg = this.average(); + const cumul = count * this.average(); + const fraciton = Math.floor((total_average ? cumul / total_average : 0) * 100); + const show_total = total_average !== undefined && this.name !== "total"; + const columns = [ + this.name, + \`\${count} runs\`, + \`\${to_human(avg)} avg\`, + \`\${to_human(cumul)} cumul\`, + ...(show_total ? [\`\${fraciton} %\`] : []), + ]; + const line = columns.map((e) => e.padStart(18)); + console.log(...line); } average() { @@ -82,5 +95,23 @@ class SectionTracker { } } +function to_human(duration_ns: number) { + const units = [ + [3_600_000_000_000, "h"], + [60_000_000_000, "min"], + [1_000_000_000, "s"], + [1_000_000, "ms"], + [1_000, "µs"], + [1, "ns"], + ] as const; + for (const [fac, name] of units) { + if ((duration_ns / fac) < 1.) continue; + const mapped_duration = duration_ns / fac; + const [integral, decimal] = mapped_duration.toString().split("."); + const formatted = \`\${integral}.\${decimal.slice(0, 3).padEnd(3, "0")}\`; + return \`\${formatted} \${name}\`; + } +} + if (import.meta.main) await main(); `; \ No newline at end of file diff --git a/src/child.ts b/src/child.ts index ad8356d..ba1971d 100644 --- a/src/child.ts +++ b/src/child.ts @@ -12,9 +12,11 @@ async function main() { .pipeThrough(new TextLineStream()); const stats = new Tracker(); + stats.update([0, "total", Number(Temporal.Now.instant().epochNanoseconds)]); try { for await (const line of lines) stats.update(JSON.parse(line) as Msg); } catch (_) { /* STDOUT closed */ } + stats.update([1, "total", Number(Temporal.Now.instant().epochNanoseconds)]); stats.log(); } @@ -41,7 +43,8 @@ class Tracker { } log() { - for (const section of this.register.values()) section.log(); + const total = this.register.get("total")?.average(); + for (const section of this.register.values()) section.log(total); Deno.exit(0); } } @@ -69,11 +72,21 @@ class SectionTracker { this.runs.push(duration); } - log() { + log(total_average: number | undefined) { const count = this.runs.length; - const avg = this.average() / 1_000; - const line = `${this.name} :\t${count} runs, ${avg} µs avg`; - console.log(line); + const avg = this.average(); + const cumul = count * this.average(); + const fraciton = Math.floor((total_average ? cumul / total_average : 0) * 100); + const show_total = total_average !== undefined && this.name !== "total"; + const columns = [ + this.name, + `${count} runs`, + `${to_human(avg)} avg`, + `${to_human(cumul)} cumul`, + ...(show_total ? [`${fraciton} %`] : []), + ]; + const line = columns.map((e) => e.padStart(18)); + console.log(...line); } average() { @@ -82,4 +95,22 @@ class SectionTracker { } } +function to_human(duration_ns: number) { + const units = [ + [3_600_000_000_000, "h"], + [60_000_000_000, "min"], + [1_000_000_000, "s"], + [1_000_000, "ms"], + [1_000, "µs"], + [1, "ns"], + ] as const; + for (const [fac, name] of units) { + if ((duration_ns / fac) < 1.) continue; + const mapped_duration = duration_ns / fac; + const [integral, decimal] = mapped_duration.toString().split("."); + const formatted = `${integral}.${decimal.slice(0, 3).padEnd(3, "0")}`; + return `${formatted} ${name}`; + } +} + if (import.meta.main) await main(); diff --git a/src/lib/observer.ts b/src/lib/observer.ts index 601035f..4c4ff0c 100644 --- a/src/lib/observer.ts +++ b/src/lib/observer.ts @@ -38,6 +38,7 @@ export class Observer { "run", `--allow-read=${socket_path}`, `--allow-write=${socket_path}`, + "--unstable-temporal", "-", socket_path, ];