Skip to content

Commit 1aaa957

Browse files
committed
Outlier detection
1 parent c028d54 commit 1aaa957

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

docs/src/app/(private)/experiments/perf/utils/benchmark.tsx

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Controls = React.memo(function Controls(props: {
1414
const { setShowBenchmark, benchmarkRootRef } = props;
1515
const settleTimeout = useTimeout();
1616
const [isRunning, setIsRunning] = React.useState(false);
17+
const [shouldRemoveOutliers, setShouldRemoveOutliers] = React.useState(true);
1718

1819
const measureDomSettled = useStableCallback(() => {
1920
const start = performance.now();
@@ -85,26 +86,14 @@ const Controls = React.memo(function Controls(props: {
8586
results.push(Math.round(domSettleDuration * 10) / 10);
8687
}
8788

88-
const average = results.reduce((a, b) => a + b, 0) / results.length;
89-
const stdDev = (() => {
90-
const squareDiffs = results.map((value) => {
91-
const diff = value - average;
92-
return diff * diff;
93-
});
94-
const avgSquareDiff = squareDiffs.reduce((a, b) => a + b, 0) / squareDiffs.length;
95-
return +Math.sqrt(avgSquareDiff).toFixed(2);
96-
})();
97-
98-
console.log('DOM settled durations (ms):', results);
99-
console.log('Average:', Math.round(average * 10) / 10);
100-
console.log('Std Dev:', stdDev);
89+
logResults(shouldRemoveOutliers ? removeOutliers(results) : results);
10190
} finally {
10291
setIsRunning(false);
10392
}
10493
});
10594

10695
return (
107-
<div className="flex gap-2">
96+
<div className="flex gap-2 items-baseline">
10897
<button
10998
type="button"
11099
onClick={() => setShowBenchmark((prev: boolean) => !prev)}
@@ -137,6 +126,15 @@ const Controls = React.memo(function Controls(props: {
137126
>
138127
Run 50
139128
</button>
129+
<label style={{ marginLeft: 8 }}>
130+
<input
131+
type="checkbox"
132+
style={{ marginRight: 4 }}
133+
checked={shouldRemoveOutliers}
134+
onChange={(ev) => setShouldRemoveOutliers(ev.target.checked)}
135+
/>
136+
Remove outliers
137+
</label>
140138
</div>
141139
);
142140
});
@@ -152,3 +150,36 @@ export default function PerformanceBenchmark(props: React.PropsWithChildren<{}>)
152150
</div>
153151
);
154152
}
153+
154+
function logResults(results: number[]) {
155+
console.log(results);
156+
console.log(
157+
'Average:',
158+
Math.round((results.reduce((a, b) => a + b, 0) / results.length) * 10) / 10,
159+
);
160+
console.log(
161+
'Std Dev:',
162+
(() => {
163+
const avg = results.reduce((a, b) => a + b, 0) / results.length;
164+
const squareDiffs = results.map((value) => {
165+
const diff = value - avg;
166+
return diff * diff;
167+
});
168+
const avgSquareDiff = squareDiffs.reduce((a, b) => a + b, 0) / squareDiffs.length;
169+
return +Math.sqrt(avgSquareDiff).toFixed(2);
170+
})(),
171+
);
172+
}
173+
174+
function removeOutliers(data: number[]) {
175+
const sortedData = data.slice().sort((a, b) => a - b);
176+
const q1Index = Math.floor(sortedData.length / 4);
177+
const q3Index = Math.floor((sortedData.length * 3) / 4);
178+
const q1 = sortedData[q1Index];
179+
const q3 = sortedData[q3Index];
180+
const iqr = q3 - q1;
181+
const lowerBound = q1 - 1.5 * iqr;
182+
const upperBound = q3 + 1.5 * iqr;
183+
184+
return data.filter((value) => value >= lowerBound && value <= upperBound);
185+
}

0 commit comments

Comments
 (0)