* i wanted to make sure `undefined` was handled properly. seems like it is. * tested with Bun, works. Deno runs faster though, by about 1.8µs/message construction! * Removed skeletons from closet (bug fixes) * Sources can now optionally specify their own time format and/or log timing * Requests and Responses now can be converted and displayed (no bodies)
64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import chalk from "chalk";
|
|
import Logging, { MessageType } from "../mod.ts";
|
|
|
|
interface BenchStats {
|
|
avg: number,
|
|
med: number,
|
|
rng: number,
|
|
min: number,
|
|
max: number,
|
|
}
|
|
|
|
function createBench(n: number) {
|
|
const log = new Logging("Bench");
|
|
const data: number[] = [];
|
|
|
|
for (let i = 0; i < 30; i++) {
|
|
const last = performance.now();
|
|
for (let i = 0; i < n; i++)
|
|
log.bench(MessageType.Info, chalk.black, 'a');
|
|
data.push(performance.now() - last);
|
|
}
|
|
|
|
const sorted = [...data].sort((a, b) => a - b);
|
|
return {
|
|
n,
|
|
avg: data.reduce((sum, val) => sum + val, 0) / data.length,
|
|
med: sorted.length % 2 === 0
|
|
? (sorted[sorted.length / 2 - 1] + sorted[sorted.length / 2]) / 2
|
|
: sorted[Math.floor(sorted.length / 2)],
|
|
rng: Math.max(...data) - Math.min(...data),
|
|
min: Math.min(...data),
|
|
max: Math.max(...data)
|
|
} as BenchStats;
|
|
}
|
|
|
|
const benches = [
|
|
createBench(1),
|
|
createBench(100),
|
|
createBench(1000),
|
|
createBench(10000),
|
|
createBench(100000),
|
|
];
|
|
|
|
function trimNumber(n: number) {
|
|
const split = n.toString().split('.');
|
|
if (!split[1]) return n;
|
|
else if (split[1].length > 4) return `${split[0]}.${split[1].substring(0, 4)}`;
|
|
else if (split[1].length == 0) return n.toString();
|
|
else return split.join('.');
|
|
}
|
|
|
|
for (const stats of benches) {
|
|
for (const value of Object.values(stats)) {
|
|
|
|
const label = value < 1 ? 'µs' : 'ms';
|
|
const val = value < 1 ? value * 1000 : value;
|
|
|
|
const i = Object.values(stats).indexOf(value);
|
|
const key = Object.keys(stats).find((_val, index) => index == i);
|
|
|
|
console.log(`${key}: ${trimNumber(val)}${key !== 'n' ? label : ''}`);
|
|
}
|
|
console.log('');
|
|
} |