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(''); }