Skip to content

Commit

Permalink
Format and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Mar 4, 2023
1 parent 5c77b79 commit c6f3e6b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 53 deletions.
10 changes: 5 additions & 5 deletions examples/arrayLoops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
});

Expand Down
8 changes: 6 additions & 2 deletions examples/emptyFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { modeMarkdown } from "../src/modes";

const defaultSuite = preset();
const emptyFunctions = defaultSuite({
emptyAsync: async () => { /* */ },
emptySync: () => { /* */ },
emptyAsync: async () => {
/* */
},
emptySync: () => {
/* */
},
});

(async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export const OPTIONS: Options = {
},
offset: {
allow: true,
rangePercent: 5
rangePercent: 5,
},
gc: {
allow: true
}
allow: true,
},
};

export const OFFSET: Offset = {
Expand Down
68 changes: 36 additions & 32 deletions src/modes/modeMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,60 @@ import { writeFile } from "fs/promises";
import { Fn, Results } from "../types";

export async function modeMarkdown(
suite: Fn<[], AsyncGenerator<Results>>,
path: string
suite: Fn<[], AsyncGenerator<Results>>,
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}`);
}
2 changes: 2 additions & 0 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ export function preset(partialOptions?: DeepPartial<Options>) {
};
};
}

export const suite = preset();
13 changes: 10 additions & 3 deletions src/stats.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export type Options = {
rangePercent: number;
};
offset: {
allow: boolean;
rangePercent: number;
allow: boolean;
rangePercent: number;
};
gc: {
allow: boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/getOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function getOptions(partialOptions?: DeepPartial<Options>) {
},
offset: {
...OPTIONS.offset,
...(partialOptions?.offset || {})
...(partialOptions?.offset || {}),
},
gc: {
...OPTIONS.gc,
...(partialOptions?.gc || {})
}
...(partialOptions?.gc || {}),
},
} satisfies Options;
}
3 changes: 0 additions & 3 deletions src/utils/removePercent.ts

This file was deleted.

0 comments on commit c6f3e6b

Please sign in to comment.