Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/intellij/__bench__/catalog.bench.ts

Raw
import { cpus } from "node:os";
import {
	acceptsProjectPath,
	schemaWithoutInjectedProjectPath,
	validateRemoteTool,
} from "../implementation.ts";

const TOOL_COUNT = 512;
const ITERATIONS = 30;
const ROUNDS = 12;
const schema = {
	type: "object" as const,
	properties: {
		projectPath: { type: "string" },
		query: { type: "string", minLength: 1 },
		options: {
			type: "object",
			properties: {
				scopes: {
					type: "array",
					items: {
						type: "object",
						properties: {
							name: { type: "string" },
							enabled: { type: "boolean" },
							limits: {
								type: "object",
								properties: {
									maximum: { type: "integer" },
									ratio: { type: "number" },
								},
							},
						},
						required: ["name"],
					},
				},
			},
		},
	},
	required: ["projectPath", "query"],
};
const tools = Array.from({ length: TOOL_COUNT }, (_, index) => ({
	name: `tool_${index}`,
	inputSchema: schema,
}));
let sink = 0;

function validateCatalog(): void {
	for (const candidate of tools)
		sink += validateRemoteTool(candidate).name.length;
}

function projectCatalog(): void {
	for (const candidate of tools) {
		const tool = validateRemoteTool(candidate);
		sink += acceptsProjectPath(tool.inputSchema) ? 1 : 0;
		sink += JSON.stringify(
			schemaWithoutInjectedProjectPath(tool.inputSchema),
		).length;
	}
}

function bench(name: string, operation: () => void): void {
	for (let index = 0; index < 10; index++) operation();
	for (let round = 0; round < ROUNDS; round++) {
		const start = performance.now();
		for (let index = 0; index < ITERATIONS; index++) operation();
		const nanoseconds = ((performance.now() - start) * 1_000_000) / ITERATIONS;
		console.log(
			`Benchmark${name}-${cpus().length}\t${ITERATIONS}\t${nanoseconds.toFixed(0)} ns/op`,
		);
	}
}

bench("ValidateCatalog512", validateCatalog);
bench("PrepareCatalog512", projectCatalog);
if (sink === 0) throw new Error("benchmark result was not consumed");