Make WordPress Core

Changeset 63412


Ignore:
Timestamp:
08/31/2026 05:29:41 PM (3 months ago)
Author:
lancewillett
Message:

Build/Test Tools: Flag slow PHPUnit tests in GitHub Actions.

The canonical PHP 8.5 reporting job now emits warning annotations and a run-summary table for tests that exceed one second. The advisory step runs after successful or failed test runs and skips cancelled jobs.

The annotation pass reuses the streaming JUnit parser introduced in [63410] and passes its six aggregate metrics to CodeVitals through a temporary file. This keeps the workflow to one traversal of the report.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13212

Props adrianmoldovanwp.
See #65887.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/.github/workflows/reusable-phpunit-tests-v3.yml

    r63410 r63412  
    127127  # - Install WordPress within the Docker container.
    128128  # - Run the PHPUnit tests.
     129  # - Flags slow PHPUnit tests with GitHub Actions annotations and a run summary.
    129130  # - Publish PHPUnit timing metrics to CodeVitals.
    130131  # - Upload the code coverage report to Codecov.io.
    … …  
    274275          MULTISITE_FLAG: ${{ inputs.multisite && 'multisite' || 'single' }}
    275276
     277      - name: Flag slow PHPUnit tests
     278        # Runs when the test step succeeds or fails, but skips cancelled jobs.
     279        # This step never fails the job itself (continue-on-error), because the
     280        # slow-test signal is advisory only.
     281        if: >-
     282          ${{
     283            ! cancelled() && inputs.report && inputs.php == '8.5' &&
     284            ( github.event_name == 'pull_request' ||
     285              ( github.event_name == 'push' && github.ref == 'refs/heads/trunk' ) )
     286          }}
     287        continue-on-error: true
     288        run: |
     289          if [ -f tests/phpunit/build/logs/junit.xml ]; then
     290            php tests/phpunit/prepare-slow-test-annotations.php \
     291              tests/phpunit/build/logs/junit.xml \
     292              1.0 \
     293              20 \
     294              "$RUNNER_TEMP/phpunit-timing-metrics.json"
     295          else
     296            echo 'PHPUnit JUnit report not found; skipping slow-test annotations.'
     297          fi
     298
    276299      - name: Publish PHPUnit timing metrics
    277300        continue-on-error: true
    … …  
    296319              "$GITHUB_SHA" \
    297320              "$COMMITTED_AT" \
     321              "$RUNNER_TEMP/phpunit-timing-metrics.json" \
    298322              | curl --fail-with-body --silent --show-error \
    299323                --request POST \
  • trunk/tests/phpunit/includes/class-wp-phpunit-timing-metrics.php

    r63410 r63412  
    99         * Extracts timing metrics from a JUnit XML file.
    1010         *
    11          * @param string $file Path to the JUnit XML file.
     11         * @param string        $file              Path to the JUnit XML file.
     12         * @param callable|null $testcase_callback Optional callback invoked for each timed testcase.
    1213         * @return array<string, float|int> Timing metrics keyed for CodeVitals.
    13          * @throws RuntimeException If the file cannot be read or contains invalid timing data.
     14         * @throws RuntimeException If the file cannot be read or contains invalid XML.
    1415         */
    15         public static function from_file( $file ) {
     16        public static function from_file( $file, $testcase_callback = null ) {
    1617                if ( ! is_readable( $file ) ) {
    1718                        throw new RuntimeException( 'The JUnit timing report could not be read.' );
    1819                }
    1920
    20                 $reader = new XMLReader();
    21                 if ( ! $reader->open( $file, null, LIBXML_NONET | LIBXML_COMPACT ) ) {
    22                         throw new RuntimeException( 'The JUnit timing report could not be opened.' );
     21                if ( null !== $testcase_callback && ! is_callable( $testcase_callback ) ) {
     22                        throw new InvalidArgumentException( 'The testcase callback must be callable.' );
    2323                }
    2424
    25                 $suite_time = null;
    26                 $test_times = array();
     25                $reader                = new XMLReader();
     26                $previous_libxml_state = libxml_use_internal_errors( true );
     27                $reader_is_open        = false;
     28                $suite_time            = null;
     29                $test_times            = array();
     30                $xml_errors            = array();
    2731
    28                 while ( $reader->read() ) {
    29                         if ( XMLReader::ELEMENT !== $reader->nodeType ) {
    30                                 continue;
     32                libxml_clear_errors();
     33
     34                try {
     35                        if ( ! $reader->open( $file, null, LIBXML_NONET | LIBXML_COMPACT ) ) {
     36                                throw new RuntimeException( 'The JUnit timing report could not be opened.' );
    3137                        }
    3238
    33                         if ( null === $suite_time && 'testsuite' === $reader->name ) {
     39                        $reader_is_open = true;
     40
     41                        while ( $reader->read() ) {
     42                                if ( XMLReader::ELEMENT !== $reader->nodeType ) {
     43                                        continue;
     44                                }
     45
     46                                if ( null === $suite_time && 'testsuite' === $reader->name ) {
     47                                        $time = $reader->getAttribute( 'time' );
     48                                        if ( is_numeric( $time ) ) {
     49                                                $suite_time = (float) $time;
     50                                        }
     51                                        continue;
     52                                }
     53
     54                                if ( 'testcase' !== $reader->name ) {
     55                                        continue;
     56                                }
     57
    3458                                $time = $reader->getAttribute( 'time' );
    35                                 if ( is_numeric( $time ) ) {
    36                                         $suite_time = (float) $time;
     59
     60                                // A testcase without numeric timing, such as a skipped test, carries
     61                                // no timing signal and is excluded from the aggregate metrics.
     62                                if ( ! is_numeric( $time ) ) {
     63                                        continue;
    3764                                }
    38                                 continue;
     65
     66                                $test_time    = (float) $time;
     67                                $test_times[] = $test_time;
     68
     69                                if ( null !== $testcase_callback ) {
     70                                        $testcase_callback(
     71                                                array(
     72                                                        'name'         => (string) $reader->getAttribute( 'name' ),
     73                                                        'class'        => (string) $reader->getAttribute( 'class' ),
     74                                                        'file'         => (string) $reader->getAttribute( 'file' ),
     75                                                        'line'         => (string) $reader->getAttribute( 'line' ),
     76                                                        'time'         => $test_time,
     77                                                        'time_display' => (string) $time,
     78                                                )
     79                                        );
     80                                }
    3981                        }
    4082
    41                         if ( 'testcase' !== $reader->name ) {
    42                                 continue;
     83                        $xml_errors = libxml_get_errors();
     84                } finally {
     85                        if ( $reader_is_open ) {
     86                                $reader->close();
    4387                        }
    4488
    45                         $time = $reader->getAttribute( 'time' );
    46                         if ( ! is_numeric( $time ) ) {
    47                                 $reader->close();
    48                                 throw new RuntimeException( 'A JUnit testcase is missing numeric timing data.' );
    49                         }
    50 
    51                         $test_times[] = (float) $time;
     89                        libxml_clear_errors();
     90                        libxml_use_internal_errors( $previous_libxml_state );
    5291                }
    5392
    54                 $reader->close();
     93                foreach ( $xml_errors as $xml_error ) {
     94                        if ( LIBXML_ERR_WARNING < $xml_error->level ) {
     95                                throw new RuntimeException( 'The JUnit timing report contains invalid XML.' );
     96                        }
     97                }
    5598
    5699                if ( ! $test_times ) {
  • trunk/tests/phpunit/prepare-timing-results.php

    r63410 r63412  
    1111require_once __DIR__ . '/includes/class-wp-phpunit-timing-metrics.php';
    1212
    13 if ( 5 !== $argc ) {
    14         fwrite( STDERR, "Usage: prepare-timing-results.php <junit-file> <branch> <hash> <timestamp>\n" );
     13if ( 5 !== $argc && 6 !== $argc ) {
     14        fwrite( STDERR, "Usage: prepare-timing-results.php <junit-file> <branch> <hash> <timestamp> [timing-metrics-file]\n" );
    1515        exit( 1 );
    1616}
    1717
    1818try {
     19        if ( 6 === $argc ) {
     20                if ( ! is_readable( $argv[5] ) ) {
     21                        throw new RuntimeException( 'The prepared PHPUnit timing metrics could not be read.' );
     22                }
     23
     24                $timing_metrics_json = file_get_contents( $argv[5] );
     25
     26                if ( false === $timing_metrics_json ) {
     27                        throw new RuntimeException( 'The prepared PHPUnit timing metrics could not be read.' );
     28                }
     29
     30                $timing_metrics = json_decode( $timing_metrics_json, true, 512, JSON_THROW_ON_ERROR );
     31                $metric_keys    = array(
     32                        'phpunit-suite-time',
     33                        'phpunit-p95-test-time',
     34                        'phpunit-p99-test-time',
     35                        'phpunit-max-test-time',
     36                        'phpunit-tests-over-500ms',
     37                        'phpunit-tests-over-1s',
     38                );
     39
     40                if (
     41                        ! is_array( $timing_metrics )
     42                        || array_keys( $timing_metrics ) !== $metric_keys
     43                        || count( $timing_metrics ) !== count( array_filter( $timing_metrics, 'is_numeric' ) )
     44                ) {
     45                        throw new RuntimeException( 'The prepared PHPUnit timing metrics are invalid.' );
     46                }
     47        } else {
     48                $timing_metrics = WP_PHPUnit_Timing_Metrics::from_file( $argv[1] );
     49        }
     50
    1951        $timestamp = new DateTimeImmutable( $argv[4] );
    2052        $payload   = array(
    … …  
    2456                'baseMetrics' => new stdClass(),
    2557                'timestamp'   => $timestamp->format( DATE_ATOM ),
    26                 'metrics'     => WP_PHPUnit_Timing_Metrics::from_file( $argv[1] ),
     58                'metrics'     => $timing_metrics,
    2759        );
    2860
  • trunk/tests/phpunit/tests/includes/junitTimingMetrics.php

    r63410 r63412  
    6262        }
    6363
     64        public function test_passes_timed_testcase_details_to_callback() {
     65                $file      = $this->create_junit_file( array( 0.25, 1.5 ), 1.75 );
     66                $testcases = array();
     67
     68                WP_PHPUnit_Timing_Metrics::from_file(
     69                        $file,
     70                        static function ( $testcase ) use ( &$testcases ) {
     71                                $testcases[] = $testcase;
     72                        }
     73                );
     74
     75                $this->assertSame(
     76                        array(
     77                                array(
     78                                        'name'         => 'test_0',
     79                                        'class'        => 'Tests_Example',
     80                                        'file'         => '/var/www/tests/phpunit/tests/example.php',
     81                                        'line'         => '100',
     82                                        'time'         => 0.25,
     83                                        'time_display' => '0.25',
     84                                ),
     85                                array(
     86                                        'name'         => 'test_1',
     87                                        'class'        => 'Tests_Example',
     88                                        'file'         => '/var/www/tests/phpunit/tests/example.php',
     89                                        'line'         => '101',
     90                                        'time'         => 1.5,
     91                                        'time_display' => '1.5',
     92                                ),
     93                        ),
     94                        $testcases
     95                );
     96        }
     97
     98        public function test_ignores_testcase_without_numeric_timing() {
     99                $file      = $this->create_junit_file( array( 0.5, null, 1.5 ), 2.0 );
     100                $testcases = array();
     101
     102                $metrics = WP_PHPUnit_Timing_Metrics::from_file(
     103                        $file,
     104                        static function ( $testcase ) use ( &$testcases ) {
     105                                $testcases[] = $testcase;
     106                        }
     107                );
     108
     109                $this->assertCount( 2, $testcases );
     110                $this->assertSame( 1500.0, $metrics['phpunit-max-test-time'] );
     111        }
     112
    64113        public function test_rejects_report_without_testcases() {
    65114                $file = $this->create_junit_file( array(), 0.0 );
    … …  
    71120        }
    72121
     122        public function test_rejects_invalid_xml() {
     123                $file = $this->create_temporary_file( '<?xml version="1.0"?><testsuites><testsuite><testcase time="1">' );
     124
     125                $this->expectException( RuntimeException::class );
     126                $this->expectExceptionMessage( 'The JUnit timing report contains invalid XML.' );
     127
     128                WP_PHPUnit_Timing_Metrics::from_file( $file );
     129        }
     130
    73131        /**
    74132         * Creates a JUnit XML file for a test.
    75133         *
    76          * @param float[]   $times      Testcase times in seconds.
    77          * @param float|int $suite_time Optional testsuite time in seconds.
     134         * @param array<float|null> $times      Testcase times in seconds. Null omits the timing attribute.
     135         * @param float|int|null    $suite_time Optional testsuite time in seconds.
    78136         * @return string Path to the temporary file.
    79137         */
    80138        private function create_junit_file( $times, $suite_time = null ) {
     139                $suite_time_attribute = null === $suite_time ? '' : sprintf( ' time="%s"', $suite_time );
     140                $testcases            = '';
     141
     142                foreach ( $times as $index => $time ) {
     143                        $time_attribute = null === $time ? '' : sprintf( ' time="%s"', $time );
     144                        $testcases     .= sprintf(
     145                                '<testcase name="test_%1$d" class="Tests_Example" file="/var/www/tests/phpunit/tests/example.php" line="%2$d"%3$s/>',
     146                                $index,
     147                                100 + $index,
     148                                $time_attribute
     149                        );
     150                }
     151
     152                return $this->create_temporary_file(
     153                        sprintf( '<?xml version="1.0"?><testsuites><testsuite tests="%1$d"%2$s>%3$s</testsuite></testsuites>', count( $times ), $suite_time_attribute, $testcases )
     154                );
     155        }
     156
     157        /**
     158         * Creates a temporary file containing the provided data.
     159         *
     160         * @param string $data File contents.
     161         * @return string Path to the temporary file.
     162         */
     163        private function create_temporary_file( $data ) {
    81164                $file = tempnam( sys_get_temp_dir(), 'junit-timing-' );
    82165
    … …  
    86169
    87170                $this->temporary_files[] = $file;
    88                 $suite_time_attribute    = null === $suite_time ? '' : sprintf( ' time="%s"', $suite_time );
    89                 $testcases               = '';
    90 
    91                 foreach ( $times as $index => $time ) {
    92                         $testcases .= sprintf( '<testcase name="test_%1$d" time="%2$s"/>', $index, $time );
    93                 }
    94 
    95                 file_put_contents(
    96                         $file,
    97                         sprintf( '<?xml version="1.0"?><testsuites><testsuite tests="%1$d"%2$s>%3$s</testsuite></testsuites>', count( $times ), $suite_time_attribute, $testcases )
    98                 );
     171                file_put_contents( $file, $data );
    99172
    100173                return $file;
Note: See TracChangeset for help on using the changeset viewer.