Skip to content

Commit 2de06de

Browse files
authored
feat(perf): add new eFPS benchmark suite (#7407)
* feat(perf): add new eFPS benchmark suite * fix(perf): add `.depcheckrc.json` with `ignores`
1 parent 930d508 commit 2de06de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+6156
-39
lines changed

perf/efps/.depcheckrc.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignores": ["@swc-node/register", "@types/react", "@types/react-dom"]
3+
}

perf/efps/.env.template

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
VITE_PERF_EFPS_PROJECT_ID=qk0wb6qx
2+
VITE_PERF_EFPS_DATASET=test
3+
PERF_EFPS_SANITY_TOKEN=

perf/efps/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/builds
2+
/results
3+
/.exported
4+
.env

perf/efps/README.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Editor "Frames per Second" — eFPS benchmarks
2+
3+
This folder contains a performance test suite for benchmarking the Sanity Studio editor and ensuring smooth performance. The suite is designed to run various tests and measure the editor's performance using the eFPS (editor Frames Per Second) metric.
4+
5+
## Overview
6+
7+
The performance test suite is part of the Sanity Studio monorepo and is used to benchmark the editor's performance. It runs a series of tests on different document types and field configurations to measure the responsiveness and smoothness of the editing experience.
8+
9+
## eFPS Metric
10+
11+
The eFPS (editor Frames Per Second) metric is used to quantify the performance of the Sanity Studio editor. Here's how it works:
12+
13+
1. The test suite measures the time it takes for the editor to respond to user input (e.g., typing in a field).
14+
2. This response time is then converted into a "frames per second" analogy to provide an intuitive understanding of performance.
15+
3. The eFPS is calculated as: `eFPS = 1000 / responseTime`
16+
17+
We use the "frames per second" analogy because it helps us have a better intuition for what constitutes good or bad performance. Just like in video games or animations:
18+
19+
- Higher eFPS values indicate smoother, more responsive performance.
20+
- Lower eFPS values suggest lag or sluggishness in the editor.
21+
22+
For example:
23+
24+
- An eFPS of 60 or higher is generally considered very smooth.
25+
- An eFPS between 30-60 is acceptable but may show some lag.
26+
- An eFPS below 30 indicates noticeable performance issues.
27+
28+
## Percentiles
29+
30+
The test suite reports eFPS values at different percentiles (p50, p75, and p90) for each run. Here's why we use percentiles and what they tell us:
31+
32+
- **p50 (50th percentile or median)**: This represents the typical performance. Half of the interactions were faster than this, and half were slower.
33+
- **p75 (75th percentile)**: 75% of interactions were faster than this value. It gives us an idea of performance during slightly worse conditions.
34+
- **p90 (90th percentile)**: 90% of interactions were faster than this value. This helps us understand performance during more challenging scenarios or edge cases.
35+
36+
Using percentiles allows us to:
37+
38+
1. Get a more comprehensive view of performance across various conditions.
39+
2. Identify inconsistencies or outliers in performance.
40+
3. Ensure that we're not just optimizing for average cases but also for worst-case scenarios.
41+
42+
## Test Structure
43+
44+
Each test in the suite has its own build. This approach offers several advantages:
45+
46+
1. **Isolation**: Each test has its own schema and configuration, preventing interference between tests.
47+
2. **Ease of Adding Tests**: New tests can be added without affecting existing ones, making the suite more modular and maintainable.
48+
3. **Accurate Profiling**: Individual builds allow for more precise source maps, which leads to better profiling output and easier performance debugging.
49+
50+
## Adding a New Test
51+
52+
To add a new test to the suite:
53+
54+
1. Create a new folder in the `tests` directory with your test name.
55+
2. Create the following files in your test folder:
56+
- `sanity.config.ts`: Define the Sanity configuration for your test.
57+
- `sanity.types.ts`: Define TypeScript types for your schema (if needed).
58+
- `<testname>.ts`: Implement your test using the `defineEfpsTest` function.
59+
3. If your test requires assets, add them to an `assets` subfolder.
60+
4. Update the `tests` array in `index.ts` to include your new test.
61+
62+
Example structure for a new test:
63+
64+
```
65+
tests/
66+
newtest/
67+
assets/
68+
sanity.config.ts
69+
sanity.types.ts
70+
newtest.ts
71+
```
72+
73+
## CPU Profiles
74+
75+
The test suite generates CPU profiles for each test run. These profiles are remapped to the original source code, making them easier to analyze. To inspect a CPU profile:
76+
77+
1. Open Google Chrome DevTools.
78+
2. Go to the "Performance" tab.
79+
3. Click on "Load profile" and select the `.cpuprofile` file from the `results` directory.
80+
81+
The mapped CPU profiles allow you to:
82+
83+
- Identify performance bottlenecks in the original source code.
84+
- Analyze the time spent in different functions and components.
85+
- Optimize the areas of code that have the most significant impact on performance.

perf/efps/entry.tsx

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {createRoot} from 'react-dom/client'
2+
import {Studio} from 'sanity'
3+
import {structureTool} from 'sanity/structure'
4+
5+
import config from '#config'
6+
7+
const configWithStructure = {
8+
...config,
9+
plugins: [...(config.plugins || []), structureTool()],
10+
}
11+
12+
const container = document.getElementById('container')
13+
if (!container) throw new Error('Could not find `#container`')
14+
15+
const root = createRoot(container)
16+
17+
root.render(<Studio config={configWithStructure} />)
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function calculatePercentile(numbers: number[], percentile: number): number {
2+
// Sort the array in ascending order
3+
const sorted = numbers.slice().sort((a, b) => a - b)
4+
5+
// Calculate the index
6+
const index = percentile * (sorted.length - 1)
7+
8+
// If the index is an integer, return the value at that index
9+
if (Number.isInteger(index)) {
10+
return sorted[index]
11+
}
12+
13+
// Otherwise, interpolate between the two nearest values
14+
const lowerIndex = Math.floor(index)
15+
const upperIndex = Math.ceil(index)
16+
const lowerValue = sorted[lowerIndex]
17+
const upperValue = sorted[upperIndex]
18+
19+
const fraction = index - lowerIndex
20+
return lowerValue + (upperValue - lowerValue) * fraction
21+
}

perf/efps/helpers/exec.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {spawn} from 'node:child_process'
2+
import process from 'node:process'
3+
4+
import chalk from 'chalk'
5+
import {type Ora} from 'ora'
6+
7+
interface ExecOptions {
8+
spinner: Ora
9+
command: string
10+
text: [string, string]
11+
cwd?: string
12+
}
13+
14+
export async function exec({
15+
spinner,
16+
command,
17+
text: [inprogressText, successText],
18+
cwd,
19+
}: ExecOptions): Promise<void> {
20+
spinner.start(inprogressText)
21+
22+
const maxColumnLength = 80
23+
const maxLines = 12
24+
const outputLines: string[] = []
25+
26+
function updateSpinnerText() {
27+
spinner.text = `${inprogressText}\n${outputLines
28+
.map((line) => {
29+
return chalk.dim(
30+
`${chalk.cyan('│')} ${
31+
line.length > maxColumnLength ? `${line.slice(0, maxColumnLength)}…` : line
32+
}`,
33+
)
34+
})
35+
.join('\n')}`
36+
}
37+
38+
await new Promise<void>((resolve, reject) => {
39+
const childProcess = spawn(command, {
40+
shell: true,
41+
stdio: process.env.CI ? 'inherit' : ['inherit', 'pipe', 'pipe'],
42+
cwd,
43+
})
44+
45+
function handleOutput(data: Buffer) {
46+
const newLines = data.toString().split('\n')
47+
for (const line of newLines) {
48+
if (line.trim() !== '') {
49+
outputLines.push(line.trim())
50+
if (outputLines.length > maxLines) {
51+
outputLines.shift()
52+
}
53+
updateSpinnerText()
54+
}
55+
}
56+
}
57+
58+
childProcess.stdout?.on('data', handleOutput)
59+
childProcess.stderr?.on('data', handleOutput)
60+
61+
childProcess.on('close', (code) => {
62+
if (code === 0) resolve()
63+
else reject(new Error(`Command exited with code ${code}`))
64+
})
65+
66+
childProcess.on('error', (error) => {
67+
reject(error)
68+
})
69+
})
70+
71+
spinner.succeed(successText)
72+
}
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {type Locator} from 'playwright'
2+
3+
import {type EfpsResult} from '../types'
4+
import {calculatePercentile} from './calculatePercentile'
5+
6+
export async function measureFpsForInput(input: Locator): Promise<EfpsResult> {
7+
await input.waitFor({state: 'visible'})
8+
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
9+
10+
await input.click()
11+
await new Promise((resolve) => setTimeout(resolve, 500))
12+
13+
const rendersPromise = input.evaluate(async (el: HTMLInputElement | HTMLTextAreaElement) => {
14+
const updates: {value: string; timestamp: number}[] = []
15+
16+
const mutationObserver = new MutationObserver(() => {
17+
updates.push({value: el.value, timestamp: Date.now()})
18+
})
19+
20+
if (el instanceof HTMLTextAreaElement) {
21+
mutationObserver.observe(el, {childList: true, characterData: true, subtree: true})
22+
} else {
23+
mutationObserver.observe(el, {attributes: true, attributeFilter: ['value']})
24+
}
25+
26+
await new Promise<void>((resolve) => {
27+
const handler = () => {
28+
el.removeEventListener('blur', handler)
29+
resolve()
30+
}
31+
32+
el.addEventListener('blur', handler)
33+
})
34+
35+
return updates
36+
})
37+
await new Promise((resolve) => setTimeout(resolve, 500))
38+
39+
const inputEvents: {character: string; timestamp: number}[] = []
40+
41+
const startingMarker = '__START__|'
42+
const endingMarker = '__END__'
43+
44+
await input.pressSequentially(endingMarker)
45+
await new Promise((resolve) => setTimeout(resolve, 500))
46+
for (let i = 0; i < endingMarker.length; i++) {
47+
await input.press('ArrowLeft')
48+
}
49+
await input.pressSequentially(startingMarker)
50+
await new Promise((resolve) => setTimeout(resolve, 500))
51+
52+
for (const character of characters) {
53+
inputEvents.push({character, timestamp: Date.now()})
54+
await input.press(character)
55+
await new Promise((resolve) => setTimeout(resolve, 0))
56+
}
57+
58+
await input.blur()
59+
60+
const renderEvents = await rendersPromise
61+
62+
await new Promise((resolve) => setTimeout(resolve, 500))
63+
64+
const latencies = inputEvents.map((inputEvent) => {
65+
const matchingEvent = renderEvents.find(({value}) => {
66+
if (!value.includes(startingMarker) || !value.includes(endingMarker)) return false
67+
68+
const [, afterStartingMarker] = value.split(startingMarker)
69+
const [beforeEndingMarker] = afterStartingMarker.split(endingMarker)
70+
return beforeEndingMarker.includes(inputEvent.character)
71+
})
72+
if (!matchingEvent) throw new Error(`No matching event for ${inputEvent.character}`)
73+
74+
return matchingEvent.timestamp - inputEvent.timestamp
75+
})
76+
77+
const p50 = 1000 / calculatePercentile(latencies, 0.5)
78+
const p75 = 1000 / calculatePercentile(latencies, 0.75)
79+
const p90 = 1000 / calculatePercentile(latencies, 0.9)
80+
81+
return {p50, p75, p90, latencies}
82+
}

perf/efps/helpers/measureFpsForPte.ts

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {type Locator} from 'playwright'
2+
3+
import {type EfpsResult} from '../types'
4+
import {calculatePercentile} from './calculatePercentile'
5+
6+
export async function measureFpsForPte(pteField: Locator): Promise<EfpsResult> {
7+
const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
8+
9+
await pteField.waitFor({state: 'visible'})
10+
await new Promise((resolve) => setTimeout(resolve, 500))
11+
12+
await pteField.click()
13+
14+
const contentEditable = pteField.locator('[contenteditable="true"]')
15+
await contentEditable.waitFor({state: 'visible'})
16+
17+
const rendersPromise = contentEditable.evaluate(async (el: HTMLElement) => {
18+
const updates: {
19+
value: string
20+
timestamp: number
21+
// with very large PTE fields, it may take time to serialize the result
22+
// so we capture this time and remove it from the final metric
23+
textContentProcessingTime: number
24+
}[] = []
25+
26+
const mutationObserver = new MutationObserver(() => {
27+
const start = performance.now()
28+
const textContent = el.textContent || ''
29+
const end = performance.now()
30+
31+
updates.push({
32+
value: textContent,
33+
timestamp: Date.now(),
34+
textContentProcessingTime: end - start,
35+
})
36+
})
37+
38+
mutationObserver.observe(el, {subtree: true, characterData: true})
39+
40+
await new Promise<void>((resolve) => {
41+
const handler = () => {
42+
el.removeEventListener('blur', handler)
43+
resolve()
44+
}
45+
46+
el.addEventListener('blur', handler)
47+
})
48+
49+
return updates
50+
})
51+
await new Promise((resolve) => setTimeout(resolve, 500))
52+
53+
const inputEvents: {character: string; timestamp: number}[] = []
54+
55+
const startingMarker = '__START__|'
56+
const endingMarker = '__END__'
57+
58+
await contentEditable.pressSequentially(endingMarker)
59+
await new Promise((resolve) => setTimeout(resolve, 500))
60+
for (let i = 0; i < endingMarker.length; i++) {
61+
await contentEditable.press('ArrowLeft')
62+
}
63+
await contentEditable.pressSequentially(startingMarker)
64+
await new Promise((resolve) => setTimeout(resolve, 500))
65+
66+
for (const character of characters) {
67+
inputEvents.push({character, timestamp: Date.now()})
68+
await contentEditable.press(character)
69+
await new Promise((resolve) => setTimeout(resolve, 0))
70+
}
71+
72+
await contentEditable.blur()
73+
74+
const renderEvents = await rendersPromise
75+
76+
const latencies = inputEvents.map((inputEvent) => {
77+
const matchingEvent = renderEvents.find(({value}) => {
78+
if (!value.includes(startingMarker) || !value.includes(endingMarker)) return false
79+
80+
const [, afterStartingMarker] = value.split(startingMarker)
81+
const [beforeEndingMarker] = afterStartingMarker.split(endingMarker)
82+
return beforeEndingMarker.includes(inputEvent.character)
83+
})
84+
if (!matchingEvent) throw new Error(`No matching event for ${inputEvent.character}`)
85+
86+
return matchingEvent.timestamp - inputEvent.timestamp - matchingEvent.textContentProcessingTime
87+
})
88+
89+
const p50 = 1000 / calculatePercentile(latencies, 0.5)
90+
const p75 = 1000 / calculatePercentile(latencies, 0.75)
91+
const p90 = 1000 / calculatePercentile(latencies, 0.9)
92+
93+
return {p50, p75, p90, latencies}
94+
}

0 commit comments

Comments
 (0)