Skip to content

Commit 1b8f49b

Browse files
committed
refactor(web-ts-results): minor improvements
1 parent 9849ba0 commit 1b8f49b

File tree

2 files changed

+184
-155
lines changed

2 files changed

+184
-155
lines changed

webdriver-ts-results/src/Common.ts

Lines changed: 147 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ export enum Severity {
3636

3737
const DEFAULT_RESULTS_KEY = "DEFAULT";
3838

39-
interface IIssue {
39+
interface Issue {
4040
issue: number;
4141
severity: Severity;
4242
text: string;
4343
link: string;
4444
}
4545

46-
export const knownIssues: IIssue[] = [
46+
export const knownIssues: Issue[] = [
4747
{
4848
issue: 634,
4949
severity: Severity.Error,
@@ -88,7 +88,7 @@ export const knownIssues: IIssue[] = [
8888
},
8989
];
9090

91-
export function findIssue(issueNumber: number): IIssue | undefined {
91+
export function findIssue(issueNumber: number): Issue | undefined {
9292
return knownIssues.find((i) => i.issue === issueNumber);
9393
}
9494
export enum BenchmarkType {
@@ -299,38 +299,53 @@ export class ResultTableData {
299299
public selectedCategories: Set<number>,
300300
public cpuDurationMode: string,
301301
) {
302-
this.selectedFameworks = new Set<Framework>();
302+
const allowedIssues = new Set(knownIssues.map((issue) => issue.issue));
303+
const defaultFrameworks = [
304+
"vanillajs-keyed",
305+
"vanillajs-1-keyed",
306+
"vanillajs-non-keyed",
307+
"vanillajs-1-non-keyed",
308+
];
303309

304-
const allowedIssues = new Set<number>();
305-
knownIssues.forEach((i) => allowedIssues.add(i.issue));
306310
console.log("ResultTableData", allowedIssues, selectedCategories);
307311

308-
selectedFrameworksInDropdown.forEach((f) => {
312+
this.selectedFameworks = new Set<Framework>();
313+
314+
selectedFrameworksInDropdown.forEach((framework) => {
309315
if (
310-
f.issues.every((i) => allowedIssues.has(i)) ||
311-
f.name === "vanillajs-keyed" ||
312-
f.name === "vanillajs-1-keyed" ||
313-
f.name === "vanillajs-non-keyed" ||
314-
f.name === "vanillajs-1-non-keyed"
316+
this.isFrameworkAllowed(framework, allowedIssues, defaultFrameworks)
315317
) {
316-
this.selectedFameworks.add(f);
318+
this.selectedFameworks.add(framework);
317319
}
318320
});
319-
this.frameworks = this.allFrameworks.filter(
320-
(framework) =>
321-
framework.type === type && this.selectedFameworks.has(framework),
322-
);
321+
this.frameworks = this.filterFrameworksByType(this.selectedFameworks, type);
323322
this.frameworksForFactors = this.allFrameworks.filter(
324323
(framework) =>
325324
framework.type === type &&
326-
(framework.issues.every((i) => allowedIssues.has(i)) ||
327-
framework.name === "vanillajs-keyed" ||
328-
framework.name === "vanillajs-1-keyed" ||
329-
framework.name === "vanillajs-non-keyed" ||
330-
framework.name === "vanillajs-1-non-keyed"),
325+
this.isFrameworkAllowed(framework, allowedIssues, defaultFrameworks),
331326
);
327+
332328
this.update(sortKey);
333329
}
330+
private isFrameworkAllowed(
331+
framework: Framework,
332+
allowedIssues: Set<number>,
333+
defaultFrameworks: Array<string>,
334+
) {
335+
return (
336+
framework.issues.every((i) => allowedIssues.has(i)) ||
337+
defaultFrameworks.includes(framework.name)
338+
);
339+
}
340+
private filterFrameworksByType(
341+
selectedFrameworks: Set<Framework>,
342+
type: FrameworkType,
343+
) {
344+
return this.allFrameworks.filter(
345+
(framework) =>
346+
framework.type === type && selectedFrameworks.has(framework),
347+
);
348+
}
334349
private update(sortKey: string) {
335350
console.time("update");
336351

@@ -361,9 +376,16 @@ export class ResultTableData {
361376

362377
return { benchmarks, results, geomMean, comparison };
363378
};
364-
[BenchmarkType.CPU, BenchmarkType.MEM, BenchmarkType.STARTUP].forEach(
365-
(type) => this.resultsMap.set(type, createResult(type)),
366-
);
379+
380+
const benchmarkTypes = [
381+
BenchmarkType.CPU,
382+
BenchmarkType.MEM,
383+
BenchmarkType.STARTUP,
384+
];
385+
for (const type of benchmarkTypes) {
386+
this.resultsMap.set(type, createResult(type));
387+
}
388+
367389
this.sortBy(sortKey);
368390
console.timeEnd("update");
369391
}
@@ -417,43 +439,31 @@ export class ResultTableData {
417439
sortValue: sortValue,
418440
};
419441
});
420-
zipped.sort((a, b) => {
421-
if (a.sortValue! < b.sortValue!) return -1;
422-
else if (a.sortValue === b.sortValue) return 0;
423-
return 1;
424-
});
425-
const remappedIdx = zipped.map((z) => z.origIndex);
442+
443+
const remappedIdx = zipped
444+
.sort((a, b) => Number(a.sortValue) - Number(b.sortValue))
445+
.map((z) => z.origIndex);
446+
426447
this.frameworks = this.remap(remappedIdx, this.frameworks);
427-
[BenchmarkType.CPU, BenchmarkType.MEM, BenchmarkType.STARTUP].forEach(
428-
(type) => {
429-
this.getResult(type).results = this.getResult(type).results.map((row) =>
430-
this.remap(remappedIdx, row),
431-
);
432-
},
433-
);
434-
[BenchmarkType.CPU, BenchmarkType.MEM, BenchmarkType.STARTUP].forEach(
435-
(type) => {
436-
this.getResult(type).geomMean = this.remap(
437-
remappedIdx,
438-
this.getResult(type).geomMean,
439-
);
440-
},
441-
);
442-
[BenchmarkType.CPU, BenchmarkType.MEM, BenchmarkType.STARTUP].forEach(
443-
(type) => {
444-
this.getResult(type).comparison = this.remap(
445-
remappedIdx,
446-
this.getResult(type).comparison,
447-
);
448-
},
449-
);
448+
449+
const benchmarkTypes = [
450+
BenchmarkType.CPU,
451+
BenchmarkType.MEM,
452+
BenchmarkType.STARTUP,
453+
];
454+
455+
for (const type of benchmarkTypes) {
456+
const result = this.getResult(type);
457+
458+
result.results = result.results.map((row) =>
459+
this.remap(remappedIdx, row),
460+
);
461+
result.geomMean = this.remap(remappedIdx, result.geomMean);
462+
result.comparison = this.remap(remappedIdx, result.comparison);
463+
}
450464
}
451465
remap<T>(remappedIdx: Array<number>, array: Array<T>): Array<T> {
452-
const copy = new Array<T>(array.length);
453-
remappedIdx.forEach((idx, i) => {
454-
copy[i] = array[idx];
455-
});
456-
return copy;
466+
return remappedIdx.map((idx) => array[idx]);
457467
}
458468

459469
computeGeometricMean(
@@ -462,13 +472,13 @@ export class ResultTableData {
462472
resultsCPUForFramework: Array<TableResultValueEntry | null>,
463473
): TableResultGeommeanEntry {
464474
let count = 0.0;
465-
const gMean = resultsCPUForFramework.reduce((gMean, r) => {
466-
if (r !== null) {
467-
count++;
468-
gMean *= r.factor as number;
469-
}
470-
return gMean;
471-
}, 1.0);
475+
let gMean = 1.0;
476+
for (const r of resultsCPUForFramework) {
477+
if (r === null) continue;
478+
gMean *= r.factor;
479+
count++;
480+
}
481+
472482
const value = Math.pow(gMean, 1 / count);
473483
return this.compareWith
474484
? new TableResultGeommeanEntry(
@@ -488,36 +498,10 @@ export class ResultTableData {
488498
}
489499
computeComparison(
490500
framework: Framework,
491-
benchmarksCPU: Array<Benchmark>,
501+
benchmarksCPU: Array<Benchmark>, // Remove cause unused
492502
resultsCPUForFramework: Array<TableResultValueEntry | null>,
493503
): TableResultComparisonEntry {
494-
if (this.compareWith) {
495-
let statisticResult: StatisticResult | undefined = undefined;
496-
resultsCPUForFramework.forEach((r) => {
497-
if (r?.statisticResult !== StatisticResult.Undecided) {
498-
if (statisticResult === undefined) {
499-
statisticResult = r?.statisticResult;
500-
} else {
501-
if (statisticResult !== r?.statisticResult)
502-
statisticResult = StatisticResult.Undecided;
503-
}
504-
}
505-
});
506-
let label = "";
507-
statisticResult = statisticResult! ?? StatisticResult.Undecided;
508-
if (statisticResult === StatisticResult.Faster) {
509-
label = "faster!";
510-
} else if (statisticResult === StatisticResult.Slower) {
511-
label = "slower!";
512-
}
513-
return new TableResultComparisonEntry(
514-
framework.name,
515-
framework,
516-
label,
517-
colorsForStatisticResult(statisticResult)[0],
518-
colorsForStatisticResult(statisticResult)[1],
519-
);
520-
} else {
504+
if (!this.compareWith) {
521505
return new TableResultComparisonEntry(
522506
framework.name,
523507
framework,
@@ -526,6 +510,33 @@ export class ResultTableData {
526510
"#000",
527511
);
528512
}
513+
514+
let statisticResult: StatisticResult | undefined = undefined;
515+
516+
for (const r of resultsCPUForFramework) {
517+
if (r?.statisticResult !== StatisticResult.Undecided) {
518+
if (!statisticResult) {
519+
statisticResult = r?.statisticResult;
520+
} else if (statisticResult !== r?.statisticResult) {
521+
statisticResult = StatisticResult.Undecided;
522+
}
523+
}
524+
}
525+
526+
let label = "";
527+
statisticResult ??= StatisticResult.Undecided;
528+
if (statisticResult === StatisticResult.Faster) {
529+
label = "faster!";
530+
} else if (statisticResult === StatisticResult.Slower) {
531+
label = "slower!";
532+
}
533+
return new TableResultComparisonEntry(
534+
framework.name,
535+
framework,
536+
label,
537+
colorsForStatisticResult(statisticResult)[0],
538+
colorsForStatisticResult(statisticResult)[1],
539+
);
529540
}
530541

531542
computeFactors(benchmark: Benchmark): Array<TableResultValueEntry | null> {
@@ -586,47 +597,49 @@ export class ResultTableData {
586597
"#000",
587598
StatisticResult.Undecided,
588599
);
589-
} else {
590-
const compareWithResults = this.results(benchmark, this.compareWith)!;
591-
const compareWithResultsValues = compareWithResults.results[resultsKey];
592-
// let meanStr = 'x'; //mean.toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: true});
593-
594-
// X1,..,Xn: this Framework, Y1, ..., Ym: selected Framework
595-
// https://de.wikipedia.org/wiki/Zweistichproben-t-Test
596-
let statisticalResult = undefined;
597-
let statisticalCol = undefined;
598-
const compareWithMean = compareWithResultsValues.mean;
599-
const stdDev = compareWithResultsValues.standardDeviation || 0;
600-
const compareWithResultsStdDev =
601-
compareWithResultsValues.standardDeviation || 0;
602-
603-
const x1 = resultValues.mean;
604-
const x2 = compareWithMean;
605-
const s1_2 = stdDev * stdDev;
606-
const s2_2 = compareWithResultsStdDev * compareWithResultsStdDev;
607-
const n1 = 10;
608-
const n2 = 10;
609-
const ny =
610-
Math.pow(s1_2 / n1 + s2_2 / n2, 2) /
611-
((s1_2 * s1_2) / (n1 * n1 * (n1 - 1)) +
612-
(s2_2 * s2_2) / (n2 * n2 * (n2 - 1)));
613-
const t = (x1 - x2) / Math.sqrt(s1_2 / n1 + s2_2 / n2);
614-
const p = (1.0 - jStat.studentt.cdf(Math.abs(t), ny)) * 2;
615-
statisticalCol = statisticComputeColor(t, p);
616-
statisticalResult = (p * 100).toFixed(3) + "%";
617-
return new TableResultValueEntry(
618-
f.name,
619-
value,
620-
formattedValue,
621-
conficenceIntervalStr,
622-
factor,
623-
factor.toFixed(2),
624-
statisticalCol[0],
625-
statisticalCol[1],
626-
statisticalCol[2],
627-
statisticalResult,
628-
);
629600
}
601+
602+
const compareWithResults = this.results(benchmark, this.compareWith)!;
603+
const compareWithResultsValues = compareWithResults.results[resultsKey];
604+
// let meanStr = 'x'; //mean.toLocaleString('en-US', {minimumFractionDigits: 1, maximumFractionDigits: 1, useGrouping: true});
605+
606+
// X1,..,Xn: this Framework, Y1, ..., Ym: selected Framework
607+
// https://de.wikipedia.org/wiki/Zweistichproben-t-Test
608+
let statisticalResult = undefined;
609+
let statisticalCol = undefined;
610+
const compareWithMean = compareWithResultsValues.mean;
611+
const stdDev = compareWithResultsValues.standardDeviation || 0;
612+
const compareWithResultsStdDev =
613+
compareWithResultsValues.standardDeviation || 0;
614+
615+
const x1 = resultValues.mean;
616+
const x2 = compareWithMean;
617+
const s1_2 = stdDev * stdDev;
618+
const s2_2 = compareWithResultsStdDev * compareWithResultsStdDev;
619+
const n1 = 10;
620+
const n2 = 10;
621+
const ny =
622+
Math.pow(s1_2 / n1 + s2_2 / n2, 2) /
623+
((s1_2 * s1_2) / (n1 * n1 * (n1 - 1)) +
624+
(s2_2 * s2_2) / (n2 * n2 * (n2 - 1)));
625+
const t = (x1 - x2) / Math.sqrt(s1_2 / n1 + s2_2 / n2);
626+
const p = (1.0 - jStat.studentt.cdf(Math.abs(t), ny)) * 2;
627+
628+
statisticalCol = statisticComputeColor(t, p);
629+
statisticalResult = (p * 100).toFixed(3) + "%";
630+
631+
return new TableResultValueEntry(
632+
f.name,
633+
value,
634+
formattedValue,
635+
conficenceIntervalStr,
636+
factor,
637+
factor.toFixed(2),
638+
statisticalCol[0],
639+
statisticalCol[1],
640+
statisticalCol[2],
641+
statisticalResult,
642+
);
630643
});
631644
}
632645
}

0 commit comments

Comments
 (0)