This commit is contained in:
JOLIMAITRE Matthieu 2024-06-03 03:07:55 +02:00
commit c1ff9ee63e
12 changed files with 499 additions and 0 deletions

86
data/generated.ts Normal file
View file

@ -0,0 +1,86 @@
export const program = `import { TextLineStream } from "https://deno.land/std@0.224.0/streams/text_line_stream.ts";
import type { Msg } from "./lib/types.ts";
async function main() {
const [sokcet_path] = Deno.args;
if (sokcet_path === undefined) throw new Error("Feur.");
const socket = Deno.listen({ transport: "unix", path: sokcet_path });
const connection = await socket.accept();
const lines = connection.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
const stats = new Tracker();
try {
for await (const line of lines) stats.update(JSON.parse(line) as Msg);
} catch (_) { /* STDOUT closed */ }
stats.log();
}
class Tracker {
register;
constructor() {
this.register = new Map<string, SectionTracker>();
}
update(message: Msg) {
if (message[0] === 0) this.get_tracker_for(message[1]).tip(message[2]);
if (message[0] === 1) this.get_tracker_for(message[1]).top(message[2]);
if (message[0] === 2) this.log();
}
get_tracker_for(section: string) {
const stored = this.register.get(section);
if (stored !== undefined) return stored;
const new_ = new SectionTracker(section);
this.register.set(section, new_);
return new_;
}
log() {
for (const section of this.register.values()) section.log();
Deno.exit(0);
}
}
class SectionTracker {
name;
runs;
current_tip;
constructor(name: string) {
this.name = name;
this.runs = [] as number[];
this.current_tip = null as number | null;
}
tip(timestamp_ns: number) {
if (this.current_tip !== null) throw new Error(\`Second TIP before TOP on section \${this.name}.\`);
this.current_tip = timestamp_ns;
}
top(timestamp_ns: number) {
if (this.current_tip === null) throw new Error(\`TOP before TIP on section \${this.name}.\`);
const duration = timestamp_ns - this.current_tip;
this.current_tip = null;
this.runs.push(duration);
}
log() {
const count = this.runs.length;
const avg = this.average() / 1_000;
const line = \`\${this.name} :\\t\${count} runs, \${avg} µs avg\`;
console.log(line);
}
average() {
const total = this.runs.reduce((a, b) => a + b);
return total / this.runs.length;
}
}
if (import.meta.main) await main();
`;