Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/ultra/__bench__/progress.bench.ts

Raw
import type { ReducerState, UltraProgressEvent } from "../progress.ts";
import { initialReducerState, reduceProgress } from "../progress.ts";

const ROUNDS = 12;
const MINIMUM_ROUND_NS = 200_000_000;
const EVENTS_PER_AGENT = 32;
let sink = 0;

function progressEvents(agentCount: number): UltraProgressEvent[] {
	const agentIds = Array.from(
		{ length: agentCount },
		(_, index) => `agent#${index}`,
	);
	return [
		{
			kind: "plan",
			startedAt: 1,
			phases: [{ phase: "fanout", status: "resolved" as const, agentIds }],
		},
		...agentIds.flatMap((agentId) => [
			{
				kind: "start" as const,
				agentId,
				phase: "fanout",
			},
			...Array.from({ length: EVENTS_PER_AGENT }, () => ({
				kind: "delta" as const,
				agentId,
				phase: "fanout",
				text: "0123456789abcdef",
			})),
			{
				kind: "end" as const,
				agentId,
				phase: "fanout",
				status: "done",
			},
		]),
	];
}

function fold(events: readonly UltraProgressEvent[]): void {
	let state: ReducerState = initialReducerState();
	for (const event of events) state = reduceProgress(state, event);
	sink ^= state.done + (state.transcripts["agent#0"]?.length ?? 0);
}

function benchmark(name: string, operation: () => void): void {
	for (let index = 0; index < 3; index++) operation();
	let iterations = 1;
	while (true) {
		const started = process.hrtime.bigint();
		for (let index = 0; index < iterations; index++) operation();
		if (Number(process.hrtime.bigint() - started) >= MINIMUM_ROUND_NS) break;
		iterations *= 2;
	}
	for (let round = 0; round < ROUNDS; round++) {
		const started = process.hrtime.bigint();
		for (let index = 0; index < iterations; index++) operation();
		const elapsed = Number(process.hrtime.bigint() - started);
		console.log(
			`Benchmark${name}\t${iterations}\t${(elapsed / iterations).toFixed(0)} ns/op`,
		);
	}
}

console.log(`goos: ${process.platform}`);
console.log(`goarch: ${process.arch}`);
console.log("pkg: pi-ext/ultra");
for (const agentCount of [8, 64, 256]) {
	const events = progressEvents(agentCount);
	benchmark(`Progress${agentCount}`, () => fold(events));
}
if (sink === 42) console.error(sink);