Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS Migration] Migrate Math.js from e2e to Typescript #36321

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions tests/e2e/compare/math.js → tests/e2e/compare/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
*
* Based on :: https://github.com/v8/v8/blob/master/test/benchmarks/csuite/compare-baseline.py
*
* @param {Number} baselineMean
* @param {Number} baselineStdev
* @param {Number} currentMean
* @param {Number} runs
* @returns {Number}
*/
const computeZ = (baselineMean, baselineStdev, currentMean, runs) => {
const computeZ = (baselineMean: number, baselineStdev: number, currentMean: number, runs: number): number => {
if (baselineStdev === 0) {
return 1000;
}
Expand All @@ -26,10 +21,8 @@ const computeZ = (baselineMean, baselineStdev, currentMean, runs) => {
*
* Based on :: https://github.com/v8/v8/blob/master/test/benchmarks/csuite/compare-baseline.py
*
* @param {Number} z
* @returns {Number}
*/
const computeProbability = (z) => {
const computeProbability = (z: number): number => {
// p 0.005: two sided < 0.01
if (z > 2.575829) {
return 0;
Expand Down
27 changes: 14 additions & 13 deletions tests/e2e/measure/math.js → tests/e2e/measure/math.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import _ from 'underscore';
type Entries = number[];

const filterOutliersViaIQR = (data) => {
type Stats = {
mean: number;
stdev: number;
runs: number;
entries: Entries;
};

const filterOutliersViaIQR = (data: Entries): Entries => {
let q1;
let q3;

Expand All @@ -18,22 +25,17 @@ const filterOutliersViaIQR = (data) => {
const maxValue = q3 + iqr * 1.5;
const minValue = q1 - iqr * 1.5;

return _.filter(values, (x) => x >= minValue && x <= maxValue);
return values.filter((x) => x >= minValue && x <= maxValue);
};

const mean = (arr) => _.reduce(arr, (a, b) => a + b, 0) / arr.length;
const mean = (arr: Entries): number => arr.reduce((a, b) => a + b, 0) / arr.length;

const std = (arr) => {
const std = (arr: Entries): number => {
const avg = mean(arr);
return Math.sqrt(
_.reduce(
_.map(arr, (i) => (i - avg) ** 2),
(a, b) => a + b,
) / arr.length,
);
return Math.sqrt(arr.map((i) => (i - avg) ** 2).reduce((a, b) => a + b) / arr.length);
};

const getStats = (entries) => {
const getStats = (entries: Entries): Stats => {
const cleanedEntries = filterOutliersViaIQR(entries);
const meanDuration = mean(cleanedEntries);
const stdevDuration = std(cleanedEntries);
Expand All @@ -46,5 +48,4 @@ const getStats = (entries) => {
};
};

// eslint-disable-next-line import/prefer-default-export
export default getStats;
Loading