refactor(tui/bench): consolidated redundant benchmark timing logic into
- Consolidated redundant benchmark timing logic into a shared `makeBench` utility. - Removed duplicate local implementation of `bench` from individual benchmark scripts.
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Micro-benchmark harness shared by the tui bench scripts.
|
||||
*
|
||||
* `makeBench(iterations)` returns a `bench(name, fn)` that runs `fn` the given
|
||||
* number of times, prints `<name>: <total>ms total (<perOp>ms/op)`, and returns
|
||||
* the total elapsed milliseconds.
|
||||
*/
|
||||
export function makeBench(iterations: number): (name: string, fn: () => void) => number {
|
||||
return function bench(name: string, fn: () => void): number {
|
||||
const start = Bun.nanoseconds();
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
fn();
|
||||
}
|
||||
const elapsed = (Bun.nanoseconds() - start) / 1e6;
|
||||
const perOp = (elapsed / iterations).toFixed(6);
|
||||
console.log(`${name}: ${elapsed.toFixed(2)}ms total (${perOp}ms/op)`);
|
||||
return elapsed;
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { parseKey as nativeParseKey } from "@oh-my-pi/pi-natives";
|
||||
import * as native from "../src/keys";
|
||||
import * as js from "./_jskey";
|
||||
import { makeBench } from "./_harness";
|
||||
|
||||
const ITERATIONS = 2000;
|
||||
|
||||
@@ -56,16 +57,7 @@ const samples = [
|
||||
{ name: "symbol /", data: "/", expected: "/" },
|
||||
];
|
||||
|
||||
function bench(name: string, fn: () => void): number {
|
||||
const start = Bun.nanoseconds();
|
||||
for (let i = 0; i < ITERATIONS; i++) {
|
||||
fn();
|
||||
}
|
||||
const elapsed = (Bun.nanoseconds() - start) / 1e6;
|
||||
const perOp = (elapsed / ITERATIONS).toFixed(6);
|
||||
console.log(`${name}: ${elapsed.toFixed(2)}ms total (${perOp}ms/op)`);
|
||||
return elapsed;
|
||||
}
|
||||
const bench = makeBench(ITERATIONS);
|
||||
|
||||
// Set to legacy mode for consistent comparison
|
||||
js.setKittyProtocolActive(true);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { sanitizeText as currentSanitizeText } from "@oh-my-pi/pi-utils/sanitize-text";
|
||||
import { makeBench } from "./_harness";
|
||||
|
||||
const STRIP_RE = new RegExp(
|
||||
[
|
||||
@@ -297,16 +298,7 @@ const samples = {
|
||||
|
||||
const wrapWidth = 40;
|
||||
|
||||
function bench(name: string, fn: () => void): number {
|
||||
const start = Bun.nanoseconds();
|
||||
for (let i = 0; i < ITERATIONS; i++) {
|
||||
fn();
|
||||
}
|
||||
const elapsed = (Bun.nanoseconds() - start) / 1e6;
|
||||
const perOp = (elapsed / ITERATIONS).toFixed(6);
|
||||
console.log(`${name}: ${elapsed.toFixed(2)}ms total (${perOp}ms/op)`);
|
||||
return elapsed;
|
||||
}
|
||||
const bench = makeBench(ITERATIONS);
|
||||
|
||||
console.log(`Text layout benchmark (${ITERATIONS} iterations)\n`);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { visibleWidth, wrapTextWithAnsi, truncateToWidth, sliceWithWidth, extractSegments, Ellipsis } from "../src/utils";
|
||||
import { matchesKey } from "../src/keys";
|
||||
import { makeBench } from "./_harness";
|
||||
|
||||
const ITERATIONS = 2000;
|
||||
|
||||
@@ -13,16 +14,7 @@ const samples = {
|
||||
|
||||
const wrapWidth = 40;
|
||||
|
||||
function bench(name: string, fn: () => void): number {
|
||||
const start = Bun.nanoseconds();
|
||||
for (let i = 0; i < ITERATIONS; i++) {
|
||||
fn();
|
||||
}
|
||||
const elapsed = (Bun.nanoseconds() - start) / 1e6;
|
||||
const perOp = (elapsed / ITERATIONS).toFixed(6);
|
||||
console.log(`${name}: ${elapsed.toFixed(2)}ms total (${perOp}ms/op)`);
|
||||
return elapsed;
|
||||
}
|
||||
const bench = makeBench(ITERATIONS);
|
||||
|
||||
console.log(`Text layout benchmark (${ITERATIONS} iterations)\n`);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user