Skip to content

Commit

Permalink
Merge pull request #36321 from ruben-rebelo/ts-migration/e2e-math
Browse files Browse the repository at this point in the history
[TS Migration] Migrate Math.js from e2e to Typescript
  • Loading branch information
MonilBhavsar authored Feb 22, 2024
2 parents 42f1c68 + 15b6f41 commit c425c34
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
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;

0 comments on commit c425c34

Please sign in to comment.