Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve measureRenders precision #534

Merged
merged 3 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-buses-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@callstack/reassure-measure': minor
---

improve `measureRenders` precision on React Native
10 changes: 7 additions & 3 deletions packages/measure/src/measure-renders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import * as logger from '@callstack/reassure-logger';
import { config } from './config';
import { RunResult, processRunResults } from './measure-helpers';
import { showFlagsOutputIfNeeded, writeTestStats } from './output';
import { applyRenderPolyfills, revertRenderPolyfills } from './polyfills';
import { ElementJsonTree, detectRedundantUpdates } from './redundant-renders';
import { resolveTestingLibrary, getTestingLibrary } from './testing-library';
import type { MeasureRendersResults } from './types';
import { ElementJsonTree, detectRedundantUpdates } from './redundant-renders';

logger.configure({
verbose: process.env.REASSURE_VERBOSE === 'true' || process.env.REASSURE_VERBOSE === '1',
Expand Down Expand Up @@ -59,14 +60,15 @@ async function measureRendersInternal(
const testingLibrary = getTestingLibrary();

showFlagsOutputIfNeeded();
applyRenderPolyfills();

const runResults: RunResult[] = [];
let hasTooLateRender = false;

const renderJsonTrees: ElementJsonTree[] = [];
let initialRenderCount = 0;

for (let i = 0; i < runs + warmupRuns; i += 1) {
for (let iteration = 0; iteration < runs + warmupRuns; iteration += 1) {
let duration = 0;
let count = 0;
let isFinished = false;
Expand All @@ -75,7 +77,7 @@ async function measureRendersInternal(

const captureRenderDetails = () => {
// We capture render details only on the first run
if (i !== 0) {
if (iteration !== 0) {
return;
}

Expand Down Expand Up @@ -124,6 +126,8 @@ async function measureRendersInternal(
);
}

revertRenderPolyfills();

return {
...processRunResults(runResults, warmupRuns),
issues: {
Expand Down
34 changes: 34 additions & 0 deletions packages/measure/src/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { performance as perf } from 'perf_hooks';
import { getTestingLibrary } from './testing-library';

export function applyRenderPolyfills() {
const testingLibrary = getTestingLibrary();
if (testingLibrary === 'react-native') {
polyfillPerformanceNow();
}
}

export function revertRenderPolyfills() {
const testingLibrary = getTestingLibrary();
if (testingLibrary === 'react-native') {
restorePerformanceNow();
}
}

/**
* React Native Jest preset mocks the global.performance object, with `now()` method being `Date.now()`.
* Ref: https://github.com/facebook/react-native/blob/3dfe22bd27429a43b4648c597b71f7965f31ca65/packages/react-native/jest/setup.js#L41
*
* Then React uses `performance.now()` in `Scheduler` to measure component render time.
* https://github.com/facebook/react/blob/45804af18d589fd2c181f3b020f07661c46b73ea/packages/scheduler/src/forks/Scheduler.js#L59
*/
let originalPerformanceNow: () => number;

function polyfillPerformanceNow() {
originalPerformanceNow = global.performance?.now;
global.performance.now = () => perf.now();
}

function restorePerformanceNow() {
globalThis.performance.now = originalPerformanceNow;
}