Skip to content

Commit

Permalink
Make options and stores global
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Mar 3, 2023
1 parent 7e4bda8 commit 1521e0c
Show file tree
Hide file tree
Showing 20 changed files with 258 additions and 206 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
# TS Package

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

## TODOs

- [ ] 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

- It should probably have a static and interactive mode
- Static mode should be the main focus
- I should design TUI for the interactive mode

## 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??)
- The cold part should have different color from the hot results

## 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

- Outputs markdown into a file
- Data should be in a table with all important data
- If possible should generate SVG graphs and include them into the markdown file
4 changes: 0 additions & 4 deletions jest.config.js

This file was deleted.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"files": ["dist/*.js", "dist/*.map", "dist/*.ts"],
"files": [
"dist/*.js",
"dist/*.map",
"dist/*.ts"
],
"scripts": {
"fix:format": "prettier --write \"**/*.ts\"",
"fix:lint": "eslint --fix --ext .ts .",
Expand All @@ -19,7 +23,7 @@
"clean": "rm -rf dist",
"prepublishOnly": "pnpm build",
"release": "release-it",
"start": "node --expose-gc -r ts-node/register src/index.ts"
"start": "node --expose-gc -r ts-node/register src/index.ts"
},
"license": "MIT",
"devDependencies": {
Expand Down
78 changes: 52 additions & 26 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import { Offsets, Options } from "./types";
import { Offset, Offsets, Options, Store, Stores } from "./types";

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

export const OFFSET_MAX = OFFSET_KEYS.length;

export const FN_ASYNC = async () => {
/* */
};

export const FN_SYNC = () => {
/* */
};

export const OPTIONS: Options = {
cpu: {
chunkSize: 100,
compareSize: 10,
Expand All @@ -14,33 +26,47 @@ export const OPTIONS = {
general: {
substractSelf: true,
allowGc: true,
offsetPercent: 5,
offsetPercent: 5,
},
} satisfies Options;
};

export const OFFSET: Offset = {
min: 0,
max: 0,
median: 0,
};

export const OFFSETS = {
export const OFFSETS: Offsets = {
async: {
cpu: {
min: 0,
max: 0,
median: 0,
},
ram: {
min: 0,
max: 0,
median: 0,
},
cpu: OFFSET,
ram: OFFSET,
},
sync: {
cpu: {
min: 0,
max: 0,
median: 0,
},
ram: {
min: 0,
max: 0,
median: 0,
},
cpu: OFFSET,
ram: OFFSET,
},
};

export const STORE: Store = {
array: new Uint32Array(),
index: 0,
};

export const STORES = {
cpu: {
chunk: STORE,
main: STORE,
},
} satisfies Offsets;
ram: {
chunk: STORE,
main: STORE,
},
};

export const GLOBAL: {
options: Options;
stores: Stores;
} = {
options: OPTIONS,
stores: STORES,
};
8 changes: 8 additions & 0 deletions src/createStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Mode, Options } from "./types";

export function createStore(mode: Mode, options: Options) {
return {
array: new Uint32Array(new ArrayBuffer(options[mode].chunkSize * 4)),
index: 0,
};
}
26 changes: 7 additions & 19 deletions src/createStores.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
import { Options, Stores } from "./types";
import { createStore } from "./createStore";
import { Options } from "./types";

// memoize store by options and reuse it if possible
export function createStores(options: Options): Stores {
export function createStores(options: Options) {
return {
cpu: {
chunk: {
array: new Uint32Array(new ArrayBuffer(options.cpu.chunkSize * 4)),
index: 0,
},
main: {
array: new Uint32Array(new ArrayBuffer(options.cpu.chunkSize * 4)),
index: 0,
},
chunk: createStore("cpu", options),
main: createStore("cpu", options),
},
ram: {
chunk: {
array: new Uint32Array(new ArrayBuffer(options.ram.chunkSize * 4)),
index: 0,
},
main: {
array: new Uint32Array(new ArrayBuffer(options.ram.chunkSize * 4)),
index: 0,
},
chunk: createStore("ram", options),
main: createStore("ram", options),
},
};
}
15 changes: 6 additions & 9 deletions src/getAllOffsets.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { getOffset } from "./getOffset";
import { Offsets, Options, Stores } from "./types";
import { Offsets } from "./types";

export async function getAllOffsets(
stores: Stores,
options: Options,
): Promise<Offsets> {
export async function getAllOffsets(): Promise<Offsets> {
return {
async: {
cpu: await getOffset({ type: "async", mode: "cpu" }, stores, options),
ram: await getOffset({ type: "async", mode: "ram" }, stores, options),
cpu: await getOffset({ type: "async", mode: "cpu" }),
ram: await getOffset({ type: "async", mode: "ram" }),
},
sync: {
cpu: await getOffset({ type: "sync", mode: "cpu" }, stores, options),
ram: await getOffset({ type: "sync", mode: "ram" }, stores, options),
cpu: await getOffset({ type: "sync", mode: "cpu" }),
ram: await getOffset({ type: "sync", mode: "ram" }),
},
};
}
93 changes: 41 additions & 52 deletions src/getOffset.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,44 @@
import { OFFSETS } from "./constants";
import {
FN_ASYNC,
FN_SYNC,
OFFSET,
OFFSETS,
OFFSET_KEYS,
OFFSET_MAX,
} from "./constants";
import { run } from "./run";
import { OffsetData, Options, Stores } from "./types";

const KEYS = ["min", "max", "median"];
const MAX = KEYS.length;

export async function getOffset(
{ type, mode }: OffsetData,
stores: Stores,
options: Options,
) {
const fn = type === "async"
? async () => {
/* */
import { OffsetData } from "./types";

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

while (true as any) {
const offset = await run(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];

if (substracted <= 0) {
result[key] += offset[key] + substracted;
counter += 1;
} else {
result[key] += offset[key];
}
: () => {
/* */
};

const result = {
min: 0,
max: 0,
median: 0
};

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

let counter = 0;

for(let i = 0; i < MAX; ++i) {
const key = KEYS[i];

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

if(substracted <= 0) {
result[key] += offset[key] + substracted;
counter += 1;
} else {
result[key] += offset[key];
}
} else {
result[key] += offset[key];
}
}

if(counter === MAX) {
break;
}
}

return result;
} else {
result[key] += offset[key];
}
}

if (counter === OFFSET_MAX) {
break;
}
}

return result;
}
20 changes: 0 additions & 20 deletions src/getRamStats.ts

This file was deleted.

10 changes: 4 additions & 6 deletions src/getCpuStats.ts → src/getStats.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { GLOBAL } from "./constants";
import { getMedian } from "./getMedian";
import { getMinMax } from "./getMinMax";
import { positive } from "./positive";
import { OffsetData, Offsets, Store } from "./types";
import { OffsetData, Offsets } from "./types";

export function getCpuStats(
{ array, index }: Store,
{ mode, type }: OffsetData,
offsets: Offsets,
) {
export function getStats({ mode, type }: OffsetData, offsets: Offsets) {
const { array, index } = GLOBAL.stores[mode].main;
const median = getMedian(array, index);
const { min, max } = getMinMax(array, index);
const ctx = offsets[type][mode];
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { preset } from "./preset";
import { printSuite } from "./printSuite";

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

(async () => {
for await (const result of callibration()) {
console.log(result);
}
await printSuite(callibration);
})();
Loading

0 comments on commit 1521e0c

Please sign in to comment.