Generic Profiling story to wrap any component (#5341)
This PR introduces a Profiling feature for our story book tests. It also implements a new CI job : front-sb-test-performance, that only runs stories suffixed with `.perf.stories.tsx` ## How it works It allows to wrap any component into an array of React Profiler components that will run tests many times to have the most replicable average render time possible. It is simply used by calling the new `getProfilingStory` util. Internally it creates a defined number of tests, separated by an arbitrary waiting time to allow the CPU to give more stable results. It will do 3 warm-up and 3 finishing runs of tests because the first and last renders are always a bit erratic, so we want to measure only the runs in-between. On the UI side it gives a table of results : <img width="515" alt="image" src="https://github.com/twentyhq/twenty/assets/26528466/273d2d91-26da-437a-890e-778cb6c1f993"> On the programmatic side, it stores the result in a div that can then be parsed by the play fonction of storybook, to expect a defined threshold. ```tsx play: async ({ canvasElement }) => { await findByTestId( canvasElement, 'profiling-session-finished', {}, { timeout: 60000 }, ); const profilingReport = getProfilingReportFromDocument(canvasElement); if (!isDefined(profilingReport)) { return; } const p95result = profilingReport?.total.p95; expect( p95result, `Component render time is more than p95 threshold (${p95ThresholdInMs}ms)`, ).toBeLessThan(p95ThresholdInMs); }, ```
This commit is contained in:
@ -0,0 +1,88 @@
|
||||
import { ProfilingDataPoint } from '~/testing/profiling/types/ProfilingDataPoint';
|
||||
import { ProfilingReport } from '~/testing/profiling/types/ProfilingReportByRun';
|
||||
|
||||
export const computeProfilingReport = (dataPoints: ProfilingDataPoint[]) => {
|
||||
const profilingReport = { total: {}, runs: {} } as ProfilingReport;
|
||||
|
||||
for (const dataPoint of dataPoints) {
|
||||
profilingReport.runs[dataPoint.runName] = {
|
||||
...profilingReport.runs[dataPoint.runName],
|
||||
sumById: {
|
||||
...profilingReport.runs[dataPoint.runName]?.sumById,
|
||||
[dataPoint.id]:
|
||||
(profilingReport.runs[dataPoint.runName]?.sumById?.[dataPoint.id] ??
|
||||
0) + dataPoint.durationInMs,
|
||||
},
|
||||
sum:
|
||||
(profilingReport.runs[dataPoint.runName]?.sum ?? 0) +
|
||||
dataPoint.durationInMs,
|
||||
};
|
||||
}
|
||||
|
||||
for (const runName of Object.keys(profilingReport.runs)) {
|
||||
const ids = Object.keys(profilingReport.runs[runName].sumById);
|
||||
const valuesUnsorted = Object.values(profilingReport.runs[runName].sumById);
|
||||
|
||||
const valuesSortedAsc = [...valuesUnsorted].sort((a, b) => a - b);
|
||||
|
||||
const numberOfIds = ids.length;
|
||||
|
||||
profilingReport.runs[runName].average =
|
||||
profilingReport.runs[runName].sum / numberOfIds;
|
||||
|
||||
profilingReport.runs[runName].min = Math.min(
|
||||
...Object.values(profilingReport.runs[runName].sumById),
|
||||
);
|
||||
|
||||
profilingReport.runs[runName].max = Math.max(
|
||||
...Object.values(profilingReport.runs[runName].sumById),
|
||||
);
|
||||
|
||||
const p50Index = Math.floor(numberOfIds * 0.5);
|
||||
const p80Index = Math.floor(numberOfIds * 0.8);
|
||||
const p90Index = Math.floor(numberOfIds * 0.9);
|
||||
const p95Index = Math.floor(numberOfIds * 0.95);
|
||||
const p99Index = Math.floor(numberOfIds * 0.99);
|
||||
|
||||
profilingReport.runs[runName].p50 = valuesSortedAsc[p50Index];
|
||||
profilingReport.runs[runName].p80 = valuesSortedAsc[p80Index];
|
||||
profilingReport.runs[runName].p90 = valuesSortedAsc[p90Index];
|
||||
profilingReport.runs[runName].p95 = valuesSortedAsc[p95Index];
|
||||
profilingReport.runs[runName].p99 = valuesSortedAsc[p99Index];
|
||||
}
|
||||
|
||||
const runNamesForTotal = Object.keys(profilingReport.runs).filter((runName) =>
|
||||
runName.startsWith('real-run'),
|
||||
);
|
||||
|
||||
const runsForTotal = runNamesForTotal.map(
|
||||
(runName) => profilingReport.runs[runName],
|
||||
);
|
||||
|
||||
profilingReport.total = {
|
||||
sum: Object.values(runsForTotal).reduce((acc, run) => acc + run.sum, 0),
|
||||
average:
|
||||
Object.values(runsForTotal).reduce((acc, run) => acc + run.average, 0) /
|
||||
Object.keys(runsForTotal).length,
|
||||
min: Math.min(...Object.values(runsForTotal).map((run) => run.min)),
|
||||
max: Math.max(...Object.values(runsForTotal).map((run) => run.max)),
|
||||
p50:
|
||||
Object.values(runsForTotal).reduce((acc, run) => acc + run.p50, 0) /
|
||||
Object.keys(runsForTotal).length,
|
||||
p80:
|
||||
Object.values(runsForTotal).reduce((acc, run) => acc + run.p80, 0) /
|
||||
Object.keys(runsForTotal).length,
|
||||
p90:
|
||||
Object.values(runsForTotal).reduce((acc, run) => acc + run.p90, 0) /
|
||||
Object.keys(runsForTotal).length,
|
||||
p95:
|
||||
Object.values(runsForTotal).reduce((acc, run) => acc + run.p95, 0) /
|
||||
Object.keys(runsForTotal).length,
|
||||
p99:
|
||||
Object.values(runsForTotal).reduce((acc, run) => acc + run.p99, 0) /
|
||||
Object.keys(runsForTotal).length,
|
||||
dataPointCount: dataPoints.length,
|
||||
};
|
||||
|
||||
return profilingReport;
|
||||
};
|
||||
@ -0,0 +1,64 @@
|
||||
import { ProfilingDataPoint } from '~/testing/profiling/types/ProfilingDataPoint';
|
||||
import { ProfilingReportByComponent } from '~/testing/profiling/types/ProfilingReportByComponent';
|
||||
|
||||
export const computeProfilingReportByComponent = (
|
||||
profilingReport: Record<string, ProfilingDataPoint[]>,
|
||||
) => {
|
||||
const reportByComponent = {} as ProfilingReportByComponent;
|
||||
|
||||
const dataPoints = Object.entries(profilingReport)
|
||||
.map(([, dataPoints]) => dataPoints)
|
||||
.flat(1);
|
||||
|
||||
for (const dataPoint of dataPoints) {
|
||||
reportByComponent[dataPoint.componentName] = {
|
||||
...reportByComponent?.[dataPoint.componentName],
|
||||
sumById: {
|
||||
...reportByComponent?.[dataPoint.componentName]?.sumById,
|
||||
[dataPoint.id]:
|
||||
(reportByComponent[dataPoint.componentName]?.sumById?.[
|
||||
dataPoint.id
|
||||
] ?? 0) + dataPoint.durationInMs,
|
||||
},
|
||||
sum:
|
||||
(reportByComponent[dataPoint.componentName]?.sum ?? 0) +
|
||||
dataPoint.durationInMs,
|
||||
};
|
||||
}
|
||||
|
||||
for (const componentName of Object.keys(reportByComponent)) {
|
||||
const ids = Object.keys(reportByComponent[componentName].sumById);
|
||||
const valuesUnsorted = Object.values(
|
||||
reportByComponent[componentName].sumById,
|
||||
);
|
||||
|
||||
const valuesSortedAsc = [...valuesUnsorted].sort((a, b) => a - b);
|
||||
|
||||
const numberOfIds = ids.length;
|
||||
|
||||
reportByComponent[componentName].average =
|
||||
reportByComponent[componentName].sum / numberOfIds;
|
||||
|
||||
reportByComponent[componentName].min = Math.min(
|
||||
...Object.values(reportByComponent[componentName].sumById),
|
||||
);
|
||||
|
||||
reportByComponent[componentName].max = Math.max(
|
||||
...Object.values(reportByComponent[componentName].sumById),
|
||||
);
|
||||
|
||||
const p50Index = Math.floor(numberOfIds * 0.5);
|
||||
const p80Index = Math.floor(numberOfIds * 0.8);
|
||||
const p90Index = Math.floor(numberOfIds * 0.9);
|
||||
const p95Index = Math.floor(numberOfIds * 0.95);
|
||||
const p99Index = Math.floor(numberOfIds * 0.99);
|
||||
|
||||
reportByComponent[componentName].p50 = valuesSortedAsc[p50Index];
|
||||
reportByComponent[componentName].p80 = valuesSortedAsc[p80Index];
|
||||
reportByComponent[componentName].p90 = valuesSortedAsc[p90Index];
|
||||
reportByComponent[componentName].p95 = valuesSortedAsc[p95Index];
|
||||
reportByComponent[componentName].p99 = valuesSortedAsc[p99Index];
|
||||
}
|
||||
|
||||
return reportByComponent;
|
||||
};
|
||||
@ -0,0 +1,5 @@
|
||||
export const getProfilingQueueIdentifier = (
|
||||
profilingId: string,
|
||||
testIndex: number,
|
||||
runName: string,
|
||||
) => `${profilingId}-run[${runName}]-test[${testIndex}]`;
|
||||
@ -0,0 +1,32 @@
|
||||
import { isNonEmptyString } from '@sniptt/guards';
|
||||
|
||||
import { PROFILING_REPORTER_DIV_ID } from '~/testing/profiling/constants/ProfilingReporterDivId';
|
||||
import { ProfilingReport } from '~/testing/profiling/types/ProfilingReportByRun';
|
||||
import { parseProfilingReportString } from '~/testing/profiling/utils/parseProfilingReportString';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
export const getProfilingReportFromDocument = (
|
||||
documentElement: Element,
|
||||
): ProfilingReport | null => {
|
||||
const profilingReportElement = documentElement.querySelector(
|
||||
`#${PROFILING_REPORTER_DIV_ID}`,
|
||||
);
|
||||
|
||||
if (!isDefined(profilingReportElement)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const profilingReportString = profilingReportElement.getAttribute(
|
||||
'data-profiling-report',
|
||||
);
|
||||
|
||||
if (!isNonEmptyString(profilingReportString)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsedProfilingReport = parseProfilingReportString(
|
||||
profilingReportString,
|
||||
);
|
||||
|
||||
return parsedProfilingReport;
|
||||
};
|
||||
@ -0,0 +1,57 @@
|
||||
import { StoryObj } from '@storybook/react';
|
||||
import { expect, findByTestId } from '@storybook/test';
|
||||
|
||||
import { ProfilerDecorator } from '~/testing/decorators/ProfilerDecorator';
|
||||
import { getProfilingReportFromDocument } from '~/testing/profiling/utils/getProfilingReportFromDocument';
|
||||
import { isDefined } from '~/utils/isDefined';
|
||||
|
||||
export const getProfilingStory = ({
|
||||
componentName,
|
||||
p95ThresholdInMs,
|
||||
averageThresholdInMs,
|
||||
numberOfRuns,
|
||||
numberOfTestsPerRun,
|
||||
}: {
|
||||
componentName: string;
|
||||
p95ThresholdInMs?: number;
|
||||
averageThresholdInMs: number;
|
||||
numberOfRuns: number;
|
||||
numberOfTestsPerRun: number;
|
||||
}): StoryObj<any> => ({
|
||||
decorators: [ProfilerDecorator],
|
||||
parameters: {
|
||||
numberOfRuns,
|
||||
numberOfTests: numberOfTestsPerRun,
|
||||
componentName,
|
||||
},
|
||||
play: async ({ canvasElement }) => {
|
||||
await findByTestId(
|
||||
canvasElement,
|
||||
'profiling-session-finished',
|
||||
{},
|
||||
{ timeout: 2 * 60000 },
|
||||
);
|
||||
|
||||
const profilingReport = getProfilingReportFromDocument(canvasElement);
|
||||
|
||||
if (!isDefined(profilingReport)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const averageResult = profilingReport?.total.average;
|
||||
|
||||
expect(
|
||||
averageResult,
|
||||
`Component render time is more than average threshold (${averageThresholdInMs}ms)`,
|
||||
).toBeLessThan(averageThresholdInMs);
|
||||
|
||||
if (isDefined(p95ThresholdInMs)) {
|
||||
const p95result = profilingReport?.total.p95;
|
||||
|
||||
expect(
|
||||
p95result,
|
||||
`Component render time is more than p95 threshold (${p95ThresholdInMs}ms)`,
|
||||
).toBeLessThan(p95ThresholdInMs);
|
||||
}
|
||||
},
|
||||
});
|
||||
@ -0,0 +1,13 @@
|
||||
import { getProfilingQueueIdentifier } from '~/testing/profiling/utils/getProfilingQueueIdentifier';
|
||||
|
||||
export const getTestArray = (
|
||||
profilingId: string,
|
||||
numberOfTestsPerRun: number,
|
||||
runName: string,
|
||||
) => {
|
||||
const testArray = Array.from({ length: numberOfTestsPerRun }, (_, i) =>
|
||||
getProfilingQueueIdentifier(profilingId, i, runName),
|
||||
);
|
||||
|
||||
return testArray;
|
||||
};
|
||||
@ -0,0 +1,7 @@
|
||||
import { ProfilingReport } from '~/testing/profiling/types/ProfilingReportByRun';
|
||||
|
||||
export const parseProfilingReportString = (
|
||||
profilingReportStringifiedJson: string,
|
||||
) => {
|
||||
return JSON.parse(profilingReportStringifiedJson) as ProfilingReport;
|
||||
};
|
||||
Reference in New Issue
Block a user