Skip to content

Commit

Permalink
Run offset function as many times as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Mar 2, 2023
1 parent fb90aad commit 7e4bda8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const OPTIONS = {
general: {
substractSelf: true,
allowGc: true,
offsetPercent: 5,
},
} satisfies Options;

Expand Down
84 changes: 39 additions & 45 deletions src/getOffset.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,55 @@
import { OFFSETS } from "./constants";
import { getCpuStats } from "./getCpuStats";
import { getMedian } from "./getMedian";
import { getMinMax } from "./getMinMax";
import { getRamStats } from "./getRamStats";
import { measure } from "./measure";
import { run } from "./run";
import { OffsetData, Options, Stores } from "./types";

// TODO: use "run" function to get rid of duplication
// TODO: run as many times as needed to get to zero
const KEYS = ["min", "max", "median"];
const MAX = KEYS.length;

export async function getOffset(
{ type, mode }: OffsetData,
stores: Stores,
options: Options,
) {
const { chunk, main } = stores[mode];
const { chunkSize, compareSize, rangePercent } = options[mode];
const getStats = mode === "cpu" ? getCpuStats : getRamStats;
const fn =
type === "async"
const fn = type === "async"
? async () => {
/* */
}
: () => {
/* */
};

main.index = -1;
chunk.index = -1;

while (true as any) {
if (chunk.index === chunkSize) {
main.array[++main.index] = getMedian(chunk.array, chunk.index);
chunk.index = -1;

if (main.index >= compareSize) {
const { min, max } = getMinMax(
main.array.slice(main.index - compareSize),
compareSize,
);

if (max - (max / 100) * rangePercent <= min) {
break;
}
}
}

if (main.index === chunkSize) {
main.array[0] = getMedian(main.array, main.index);
main.index = 0;
}

await measure({ fn, mode, store: chunk }, options);
}

return getStats(
main,
{ mode, type: fn instanceof Promise ? "async" : "sync" },
OFFSETS,
);
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;
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { preset } from "./preset";

const defaultSuite = preset();
const callibration = defaultSuite({
emptySync: () => {
emptyAsync: async () => {
/* */
},
emptyAsync: async () => {
emptySync: () => {
/* */
},
});
Expand Down
4 changes: 4 additions & 0 deletions src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export function preset(partialOptions?: DeepPartial<Options>) {
return async function* runSuite() {
const offsets = await getAllOffsets(stores, options);

console.log("Overhead");
console.log(offsets);

for (const benchmarkName in benchmarks) {
options.general.allowGc && global.gc?.();

console.log();
console.log(benchmarkName);

yield await runBenchmark(
benchmarks[benchmarkName],
stores,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Options = {
general: {
substractSelf: boolean;
allowGc: boolean;
offsetPercent: number;
};
};

Expand Down

0 comments on commit 7e4bda8

Please sign in to comment.