Repository: facebook/react
File: compiler/packages/react-mcp-server/src/tools/runtimePerf.ts (TypeScript)

Code before:
    },
    error: null,
  };

  for (let ii = 0; ii < iterations; ii++) {
    await page.setContent(html, {waitUntil: 'networkidle0'});
    await page.waitForFunction(
      'window.__RESULT__ !== undefined && (window.__RESULT__.renderTime !== null || window.__RESULT__.error !== null)',
    );

    // ui chaos monkey
    const selectors = await page.evaluate(() => {
      window.__INTERACTABLE_SELECTORS__ = [];
      const elements = Array.from(document.querySelectorAll('a')).concat(
        Array.from(document.querySelectorAll('button')),
      );
      for (const el of elements) {
        window.__INTERACTABLE_SELECTORS__.push(el.tagName.toLowerCase());
      }
      return window.__INTERACTABLE_SELECTORS__;
    });

    for (const selector of selectors) {
      await page.click(selector);
      await delay(500);
    }

    // Visit a new page for 1s to background the current page so that WebVitals can finish being calculated
    const tempPage = await browser.newPage();
    await tempPage.evaluate(() => {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve(true);
        }, 1000);
      });
    });
    await tempPage.close();

    const evaluationResult: EvaluationResults = await page.evaluate(() => {
      return (window as any).__RESULT__;
    });

    if (evaluationResult.renderTime !== null) {
      performanceResults.renderTime.push(evaluationResult.renderTime);
    }

    const webVitalMetrics = ['cls', 'lcp', 'inp', 'fid', 'ttfb'] as const;
    for (const metric of webVitalMetrics) {
      if (evaluationResult.webVitals[metric] !== null) {
        performanceResults.webVitals[metric].push(
          evaluationResult.webVitals[metric],

Review comment (performance):
depending on the number of elements this could be pretty slow. I don't think you need to wait 500ms after every click before clicking again right?

```suggestion
    await Promise.all(selectors.map((selector) => page.click(selector)));
    await delay(500);
```

Code after:
    },
    error: null,
  };

  for (let ii = 0; ii < iterations; ii++) {
    await page.setContent(html, {waitUntil: 'networkidle0'});
    await page.waitForFunction(
      'window.__RESULT__ !== undefined && (window.__RESULT__.renderTime !== null || window.__RESULT__.error !== null)',
    );

    // ui chaos monkey
    const selectors = await page.evaluate(() => {
      window.__INTERACTABLE_SELECTORS__ = [];
      const elements = Array.from(document.querySelectorAll('a')).concat(
        Array.from(document.querySelectorAll('button')),
      );
      for (const el of elements) {
        window.__INTERACTABLE_SELECTORS__.push(el.tagName.toLowerCase());
      }
      return window.__INTERACTABLE_SELECTORS__;
    });

    await Promise.all(
      selectors.map(async (selector: string) => {
        try {
          await page.click(selector);
        } catch (e) {
          console.log(`warning: Could not click ${selector}: ${e.message}`);
        }
      }),
    );
    await delay(500);

    // Visit a new page for 1s to background the current page so that WebVitals can finish being calculated
    const tempPage = await browser.newPage();
    await tempPage.evaluate(() => {
      return new Promise(resolve => {
        setTimeout(() => {
          resolve(true);
        }, 1000);
      });
    });
    await tempPage.close();

    const evaluationResult: EvaluationResults = await page.evaluate(() => {
      return (window as any).__RESULT__;
    });

    if (evaluationResult.renderTime !== null) {
      performanceResults.renderTime.push(evaluationResult.renderTime);
    }