generated from yamiteru/ts-package
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run offset function as many times as needed
- Loading branch information
Showing
5 changed files
with
47 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters