-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d72c2e
commit 60a1017
Showing
10 changed files
with
453 additions
and
309 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {type EfpsResult} from '../types' | ||
|
||
function calculatePercentile(numbers: number[], percentile: number): number { | ||
// Sort the array in ascending order | ||
const sorted = numbers.slice().sort((a, b) => a - b) | ||
|
||
// Calculate the index | ||
const index = percentile * (sorted.length - 1) | ||
|
||
// If the index is an integer, return the value at that index | ||
if (Number.isInteger(index)) { | ||
return sorted[index] | ||
} | ||
|
||
// Otherwise, interpolate between the two nearest values | ||
const lowerIndex = Math.floor(index) | ||
const upperIndex = Math.ceil(index) | ||
const lowerValue = sorted[lowerIndex] | ||
const upperValue = sorted[upperIndex] | ||
|
||
const fraction = index - lowerIndex | ||
return lowerValue + (upperValue - lowerValue) * fraction | ||
} | ||
|
||
function calculateError(numbers: number[]) { | ||
const mean = numbers.reduce((sum, num) => sum + num, 0) / numbers.length | ||
|
||
// calculate the sum of squared differences from the mean | ||
const squaredDiffs = numbers.map((num) => Math.pow(num - mean, 2)) | ||
const sumSquaredDiffs = squaredDiffs.reduce((sum, diff) => sum + diff, 0) | ||
|
||
const variance = sumSquaredDiffs / (numbers.length - 1) | ||
const standardDeviation = Math.sqrt(variance) | ||
|
||
// We assume normal distribution and multiply the standard deviations by 1.96 | ||
// which aims to represent 95% of the population | ||
return 1.96 * standardDeviation | ||
} | ||
|
||
export function aggregateLatencies(values: number[]): EfpsResult['latency'] { | ||
return { | ||
median: calculatePercentile(values, 0.5), | ||
error: calculateError(values), | ||
p75: calculatePercentile(values, 0.75), | ||
p90: calculatePercentile(values, 0.9), | ||
p99: calculatePercentile(values, 0.99), | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {type Page} from 'playwright' | ||
|
||
const BLOCKING_TASK_THRESHOLD = 50 | ||
|
||
export function measureBlockingTime(page: Page): () => Promise<number> { | ||
const idleGapLengthsPromise = page.evaluate(async () => { | ||
const idleGapLengths: number[] = [] | ||
const done = false | ||
let last = performance.now() | ||
|
||
const handler = () => { | ||
const current = performance.now() | ||
idleGapLengths.push(current - last) | ||
last = current | ||
|
||
if (done) return | ||
requestIdleCallback(handler) | ||
} | ||
|
||
requestIdleCallback(handler) | ||
|
||
await new Promise((resolve) => { | ||
document.addEventListener('__blockingTimeFinish', resolve, {once: true}) | ||
}) | ||
|
||
return idleGapLengths | ||
}) | ||
|
||
async function getBlockingTime() { | ||
await page.evaluate(() => { | ||
document.dispatchEvent(new CustomEvent('__blockingTimeFinish')) | ||
}) | ||
|
||
const idleGapLengths = await idleGapLengthsPromise | ||
|
||
const blockingTime = idleGapLengths | ||
// only consider the gap lengths that are blocking | ||
.filter((idleGapLength) => idleGapLength > BLOCKING_TASK_THRESHOLD) | ||
// subtract the allowed time so we're only left with blocking time | ||
.map((idleGapLength) => idleGapLength - BLOCKING_TASK_THRESHOLD) | ||
.reduce((sum, next) => sum + next, 0) | ||
|
||
return blockingTime | ||
} | ||
|
||
return getBlockingTime | ||
} |
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
Oops, something went wrong.