Skip to content

Commit

Permalink
Do refactoring and add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Mar 4, 2023
1 parent c257bf5 commit 5c77b79
Show file tree
Hide file tree
Showing 31 changed files with 202 additions and 214 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
node_modules
dist
coverage
examples/*.md
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# TS Package
# TS Package

Template for creating publishable TypeScript package. It uses SWC for compilation.

## TODOs

- [ ] Implement output mode
- [x] Implement output mode

- [ ] Add ability to stop benchmark execution after N ms
- [ ] Add ability to stop benchmark execution after N iterations
- [ ] Clean up and restructure/refactor the codebase
- [ ] Figure out ways to optimize the lib
- [ ] Optimize the code (the less users have to wait the better)
- [ ] Create examples (should also work as tests)

- [ ] Write docs
- [ ] Compare this lib to other libs (definitely against benchmark.js)
- [ ] Publish alpha version

## Notes
Expand All @@ -23,23 +18,25 @@ Template for creating publishable TypeScript package. It uses SWC for compilatio
- Static mode should be the main focus
- I should design TUI for the interactive mode

## Interactive mode
## Modes

### Interactive mode

- Shows real-time data from all benchmark iterations
- It should have different "screen" per each benchmark
- I can switch between different screens
- I should be able to also show all results in one big graph
- I should be able to re-run a benchmark from the TUI
- The re-run data should be merged instead of using the latest (maybe??)
- I should be able to re-stats a benchmark from the TUI
- The re-stats data should be merged instead of using the latest (maybe??)
- The cold part should have different color from the hot results

## Static mode
### Static mode

- Shows real-time data but can't use keyboard
- Doesn't show a graph, only the basic info
- Should be clear what is happening and why is user waiting

## Output mode
### Output mode

- Outputs markdown into a file
- Data should be in a table with all important data
Expand Down
34 changes: 34 additions & 0 deletions examples/arrayLoops.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { preset } from "../src";
import { modeMarkdown } from "../src/modes";

const LENGTH = 1_000;
const DATA = [...new Array(LENGTH)].map(() => Math.random() * 10);
const RESULT = { _: 0 };

const defaultSuite = preset();
const emptyFunctions = defaultSuite({
for: () => {
for(let i = 0; i < LENGTH; ++i) {
RESULT._ = DATA[i];
}
},
while: () => {
let i = -1;

while(++i < LENGTH) {
RESULT._ = DATA[i];
}
},
forOf: () => {
for(const v of DATA) {
RESULT._ = v;
}
},
forEach: () => {
DATA.forEach((v) => RESULT._ = v);
},
});

(async () => {
await modeMarkdown(emptyFunctions, "./examples/arrayLoops.md");
})();
12 changes: 12 additions & 0 deletions examples/emptyFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { preset } from "../src";
import { modeMarkdown } from "../src/modes";

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

(async () => {
await modeMarkdown(emptyFunctions, "./examples/emptyFunctions.md");
})();
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ts-package",
"name": "benchpress",
"version": "0.0.1",
"source": "src/index.ts",
"main": "dist/index.cjs.js",
Expand All @@ -23,7 +23,8 @@
"clean": "rm -rf dist",
"prepublishOnly": "pnpm build",
"release": "release-it",
"start": "node --expose-gc -r ts-node/register src/index.ts"
"example:emptyFunctions": "node --expose-gc -r ts-node/register examples/emptyFunctions.ts",
"example:arrayLoops": "node --expose-gc -r ts-node/register examples/arrayLoops.ts"
},
"license": "MIT",
"devDependencies": {
Expand Down
4 changes: 0 additions & 4 deletions result.md

This file was deleted.

12 changes: 7 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Offset, Offsets, Options, Store, Stores } from "./types";

export const OFFSET_KEYS = ["min", "max", "median"];
export const OFFSET_KEYS = ["min", "max", "median"] as const;

export const OFFSET_MAX = OFFSET_KEYS.length;

Expand All @@ -23,11 +23,13 @@ export const OPTIONS: Options = {
compareSize: 5,
rangePercent: 5,
},
general: {
substractSelf: true,
allowGc: true,
offsetPercent: 5,
offset: {
allow: true,
rangePercent: 5
},
gc: {
allow: true
}
};

export const OFFSET: Offset = {
Expand Down
18 changes: 0 additions & 18 deletions src/getStats.ts

This file was deleted.

18 changes: 2 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,2 @@
import { outputToMarkdown } from "./output/outputToMarkdown";
import { preset } from "./preset";

const defaultSuite = preset();
const callibration = defaultSuite({
test: () => {
`${Math.random()}`;
},
emptySync: () => {
/* */
},
});

(async () => {
await outputToMarkdown(callibration, "./result.md");
})();
export * from "./types";
export * from "./preset";
27 changes: 0 additions & 27 deletions src/measure.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/modes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./modeMarkdown";
11 changes: 7 additions & 4 deletions src/output/outputToMarkdown.ts → src/modes/modeMarkdown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { writeFile } from "fs/promises";
import { Fn, Results } from "../types";

export async function outputToMarkdown(
export async function modeMarkdown(
suite: Fn<[], AsyncGenerator<Results>>,
path: string
) {
Expand All @@ -11,12 +11,13 @@ export async function outputToMarkdown(
let mostKbs = 0;

console.log("Benchmark started");
console.log();

for await (const result of suite()) {
console.log(`-${result.name}`);

if(result.name.length > longestName) {
longestName = result.name.length;
longestName = result.name.length;
}

if(result.ram.median > mostKbs) {
Expand All @@ -26,7 +27,9 @@ export async function outputToMarkdown(
results.push(result);
}

console.log();
console.log("Benchmark ended");
console.log();

const sorted = results.sort(
({ cpu: { median: a } }, { cpu: { median: b } }) => a - b,
Expand All @@ -35,7 +38,7 @@ export async function outputToMarkdown(
let markdown = "";

markdown += `| name | op/s | kbs |\n`;
markdown += `|---|---|---|\n`;
markdown += `|:---|:---|:---|\n`;

for(let i = 0; i < results.length; ++i) {
const result = sorted[i];
Expand All @@ -49,6 +52,6 @@ export async function outputToMarkdown(
}

await writeFile(path, markdown);

console.log(`Results saved to ${path}`);
}
16 changes: 8 additions & 8 deletions src/getOffset.ts → src/offset/getOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import {
OFFSETS,
OFFSET_KEYS,
OFFSET_MAX,
} from "./constants";
import { run } from "./run";
import { OffsetData } from "./types";
} from "../constants";
import { stats } from "../stats";
import { TypeMode } from "../types";

export async function getOffset({ type, mode }: OffsetData) {
export async function getOffset({ type, mode }: TypeMode) {
const fn = type === "async" ? FN_ASYNC : FN_SYNC;
const result = { ...OFFSET };

while (true as any) {
const offset = await run(fn, mode, OFFSETS);
const offset = await stats(fn, mode, OFFSETS);

let counter = 0;

for (let i = 0; i < OFFSET_MAX; ++i) {
const key = OFFSET_KEYS[i];

if (result[key]) {
const substracted = offset[key] - result[key];
const subtracted = offset[key] - result[key];

if (substracted <= 0) {
result[key] += offset[key] + substracted;
if (subtracted <= 0) {
result[key] += offset[key] + subtracted;
counter += 1;
} else {
result[key] += offset[key];
Expand Down
4 changes: 2 additions & 2 deletions src/getAllOffsets.ts → src/offset/getOffsets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getOffset } from "./getOffset";
import { Offsets } from "./types";
import { Offsets } from "../types";

export async function getAllOffsets(): Promise<Offsets> {
export async function getOffsets(): Promise<Offsets> {
return {
async: {
cpu: await getOffset({ type: "async", mode: "cpu" }),
Expand Down
2 changes: 2 additions & 0 deletions src/offset/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./getOffset";
export * from "./getOffsets";
26 changes: 14 additions & 12 deletions src/preset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GLOBAL } from "./constants";
import { createStores } from "./createStores";
import { getAllOffsets } from "./getAllOffsets";
import { getOptions } from "./getOptions";
import { runBenchmark } from "./runBenchmark";
import { createStores } from "./store";
import { getOffsets } from "./offset";
import { getOptions } from "./utils";
import { DeepPartial, Options, Benchmarks } from "./types";
import { stats } from "./stats";

export function preset(partialOptions?: DeepPartial<Options>) {
const options = getOptions(partialOptions);
Expand All @@ -16,17 +16,19 @@ export function preset(partialOptions?: DeepPartial<Options>) {
GLOBAL.stores = stores;
GLOBAL.options = options;

const offsets = await getAllOffsets();
const offsets = await getOffsets();

for (const benchmarkName in benchmarks) {
for (const name in benchmarks) {
// We GC here so memory from one benchmark doesn't leak to the next one
GLOBAL.options.general.allowGc && global.gc?.();
GLOBAL.options.gc.allow && global.gc?.();

yield await runBenchmark(
benchmarkName,
benchmarks[benchmarkName],
offsets,
);
const benchmark = benchmarks[name];

yield {
name,
cpu: await stats(benchmark, "cpu", offsets),
ram: await stats(benchmark, "ram", offsets),
};
}
};
};
Expand Down
10 changes: 0 additions & 10 deletions src/printBenchmark.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/printSuite.ts

This file was deleted.

Loading

0 comments on commit 5c77b79

Please sign in to comment.