improve result logging

This commit is contained in:
JOLIMAITRE Matthieu 2024-06-10 10:54:53 +02:00
parent 02d19fce2a
commit 0ba41d5aea
3 changed files with 73 additions and 10 deletions

View file

@ -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();
`;

View file

@ -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();

View file

@ -38,6 +38,7 @@ export class Observer {
"run",
`--allow-read=${socket_path}`,
`--allow-write=${socket_path}`,
"--unstable-temporal",
"-",
socket_path,
];