diff --git a/examples/arrayLoops.ts b/examples/arrayLoops.ts index f5ba64e..898c2fb 100644 --- a/examples/arrayLoops.ts +++ b/examples/arrayLoops.ts @@ -8,24 +8,24 @@ const RESULT = { _: 0 }; const defaultSuite = preset(); const emptyFunctions = defaultSuite({ for: () => { - for(let i = 0; i < LENGTH; ++i) { + for (let i = 0; i < LENGTH; ++i) { RESULT._ = DATA[i]; } }, - while: () => { + while: () => { let i = -1; - while(++i < LENGTH) { + while (++i < LENGTH) { RESULT._ = DATA[i]; } }, forOf: () => { - for(const v of DATA) { + for (const v of DATA) { RESULT._ = v; } }, forEach: () => { - DATA.forEach((v) => RESULT._ = v); + DATA.forEach((v) => (RESULT._ = v)); }, }); diff --git a/examples/emptyFunctions.ts b/examples/emptyFunctions.ts index 263b82a..cb5d4b9 100644 --- a/examples/emptyFunctions.ts +++ b/examples/emptyFunctions.ts @@ -3,8 +3,12 @@ import { modeMarkdown } from "../src/modes"; const defaultSuite = preset(); const emptyFunctions = defaultSuite({ - emptyAsync: async () => { /* */ }, - emptySync: () => { /* */ }, + emptyAsync: async () => { + /* */ + }, + emptySync: () => { + /* */ + }, }); (async () => { diff --git a/src/constants.ts b/src/constants.ts index 0c0848c..04dc9db 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -25,11 +25,11 @@ export const OPTIONS: Options = { }, offset: { allow: true, - rangePercent: 5 + rangePercent: 5, }, gc: { - allow: true - } + allow: true, + }, }; export const OFFSET: Offset = { diff --git a/src/modes/modeMarkdown.ts b/src/modes/modeMarkdown.ts index 2b59674..cbc1965 100644 --- a/src/modes/modeMarkdown.ts +++ b/src/modes/modeMarkdown.ts @@ -2,56 +2,60 @@ import { writeFile } from "fs/promises"; import { Fn, Results } from "../types"; export async function modeMarkdown( - suite: Fn<[], AsyncGenerator>, - path: string + suite: Fn<[], AsyncGenerator>, + path: string, ) { - const results: Results[] = []; + const results: Results[] = []; - let longestName = 0; - let mostKbs = 0; + let longestName = 0; + let mostKbs = 0; - console.log("Benchmark started"); - console.log(); + console.log("Benchmark started"); + console.log(); - for await (const result of suite()) { - console.log(`-${result.name}`); + for await (const result of suite()) { + console.log(`-${result.name}`); - if(result.name.length > longestName) { - longestName = result.name.length; - } + if (result.name.length > longestName) { + longestName = result.name.length; + } - if(result.ram.median > mostKbs) { - mostKbs = result.ram.median; - } + if (result.ram.median > mostKbs) { + mostKbs = result.ram.median; + } results.push(result); } - console.log(); - console.log("Benchmark ended"); - console.log(); + console.log(); + console.log("Benchmark ended"); + console.log(); - const sorted = results.sort( + const sorted = results.sort( ({ cpu: { median: a } }, { cpu: { median: b } }) => a - b, ); - let markdown = ""; + let markdown = ""; - markdown += `| name | op/s | kbs |\n`; - markdown += `|:---|:---|:---|\n`; + markdown += `| name | op/s | kbs |\n`; + markdown += `|:---|:---|:---|\n`; - for(let i = 0; i < results.length; ++i) { - const result = sorted[i]; + for (let i = 0; i < results.length; ++i) { + const result = sorted[i]; - const ops = - result.cpu.median === 0 - ? 1_000_000_000 - : Math.round(1_000_000_000 / result.cpu.median); + const ops = + result.cpu.median === 0 + ? 1_000_000_000 + : Math.round(1_000_000_000 / result.cpu.median); - markdown += `| ${result.name} | ${ops.toLocaleString("en").replaceAll(",", " ")} | ${result.ram.median.toLocaleString("en").replaceAll(",", " ")} |\n`; - } + markdown += `| ${result.name} | ${ops + .toLocaleString("en") + .replaceAll(",", " ")} | ${result.ram.median + .toLocaleString("en") + .replaceAll(",", " ")} |\n`; + } - await writeFile(path, markdown); + await writeFile(path, markdown); - console.log(`Results saved to ${path}`); + console.log(`Results saved to ${path}`); } diff --git a/src/preset.ts b/src/preset.ts index 03b8125..b34b5c9 100644 --- a/src/preset.ts +++ b/src/preset.ts @@ -33,3 +33,5 @@ export function preset(partialOptions?: DeepPartial) { }; }; } + +export const suite = preset(); diff --git a/src/stats.ts b/src/stats.ts index 30d979c..97286d8 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -1,9 +1,13 @@ import { GLOBAL } from "./constants"; -import { getMedian, getMinMax, positive, removePercent } from "./utils"; +import { getMedian, getMinMax, positive } from "./utils"; import { run } from "./run"; import { Benchmark, Mode, Offsets } from "./types"; -export async function stats(benchmark: Benchmark, mode: Mode, offsets: Offsets) { +export async function stats( + benchmark: Benchmark, + mode: Mode, + offsets: Offsets, +) { const { chunk, main } = GLOBAL.stores[mode]; const { chunkSize, compareSize, rangePercent } = GLOBAL.options[mode]; const type = (benchmark as any) instanceof Promise ? "async" : "sync"; @@ -22,7 +26,10 @@ export async function stats(benchmark: Benchmark, mode: Mode, offsets: Offsets) compareSize, ); - if (removePercent(max, rangePercent) <= min) { + // NOTE: if I'm right it stops collecting stats once the function is hot + // but instead we want to reset the index and start collecting from that point + // since now we're basing out stats on the noise generated before it's hot + if (max - (max / 100) * rangePercent <= min) { break; } } diff --git a/src/types.ts b/src/types.ts index eaca45a..fcbb716 100644 --- a/src/types.ts +++ b/src/types.ts @@ -15,8 +15,8 @@ export type Options = { rangePercent: number; }; offset: { - allow: boolean; - rangePercent: number; + allow: boolean; + rangePercent: number; }; gc: { allow: boolean; diff --git a/src/utils/getOptions.ts b/src/utils/getOptions.ts index c3195d6..54d80fd 100644 --- a/src/utils/getOptions.ts +++ b/src/utils/getOptions.ts @@ -13,11 +13,11 @@ export function getOptions(partialOptions?: DeepPartial) { }, offset: { ...OPTIONS.offset, - ...(partialOptions?.offset || {}) + ...(partialOptions?.offset || {}), }, gc: { ...OPTIONS.gc, - ...(partialOptions?.gc || {}) - } + ...(partialOptions?.gc || {}), + }, } satisfies Options; } diff --git a/src/utils/removePercent.ts b/src/utils/removePercent.ts deleted file mode 100644 index f846bdf..0000000 --- a/src/utils/removePercent.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function removePercent(value: number, percent: number) { - return value - (value / 100) * percent; -}