Changeset 63412
- Timestamp:
- 08/31/2026 05:29:41 PM (3 months ago)
- Location:
- trunk
- Files:
-
- 1 added
- 4 edited
-
.github/workflows/reusable-phpunit-tests-v3.yml (modified) (3 diffs)
-
tests/phpunit/includes/class-wp-phpunit-timing-metrics.php (modified) (1 diff)
-
tests/phpunit/prepare-slow-test-annotations.php (added)
-
tests/phpunit/prepare-timing-results.php (modified) (2 diffs)
-
tests/phpunit/tests/includes/junitTimingMetrics.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/.github/workflows/reusable-phpunit-tests-v3.yml
r63410 r63412 127 127 # - Install WordPress within the Docker container. 128 128 # - Run the PHPUnit tests. 129 # - Flags slow PHPUnit tests with GitHub Actions annotations and a run summary. 129 130 # - Publish PHPUnit timing metrics to CodeVitals. 130 131 # - Upload the code coverage report to Codecov.io. … … 274 275 MULTISITE_FLAG: ${{ inputs.multisite && 'multisite' || 'single' }} 275 276 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 276 299 - name: Publish PHPUnit timing metrics 277 300 continue-on-error: true … … 296 319 "$GITHUB_SHA" \ 297 320 "$COMMITTED_AT" \ 321 "$RUNNER_TEMP/phpunit-timing-metrics.json" \ 298 322 | curl --fail-with-body --silent --show-error \ 299 323 --request POST \ -
trunk/tests/phpunit/includes/class-wp-phpunit-timing-metrics.php
r63410 r63412 9 9 * Extracts timing metrics from a JUnit XML file. 10 10 * 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. 12 13 * @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. 14 15 */ 15 public static function from_file( $file ) {16 public static function from_file( $file, $testcase_callback = null ) { 16 17 if ( ! is_readable( $file ) ) { 17 18 throw new RuntimeException( 'The JUnit timing report could not be read.' ); 18 19 } 19 20 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.' ); 23 23 } 24 24 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(); 27 31 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.' ); 31 37 } 32 38 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 34 58 $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; 37 64 } 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 } 39 81 } 40 82 41 if ( 'testcase' !== $reader->name ) { 42 continue; 83 $xml_errors = libxml_get_errors(); 84 } finally { 85 if ( $reader_is_open ) { 86 $reader->close(); 43 87 } 44 88 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 ); 52 91 } 53 92 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 } 55 98 56 99 if ( ! $test_times ) { -
trunk/tests/phpunit/prepare-timing-results.php
r63410 r63412 11 11 require_once __DIR__ . '/includes/class-wp-phpunit-timing-metrics.php'; 12 12 13 if ( 5 !== $argc ) {14 fwrite( STDERR, "Usage: prepare-timing-results.php <junit-file> <branch> <hash> <timestamp> \n" );13 if ( 5 !== $argc && 6 !== $argc ) { 14 fwrite( STDERR, "Usage: prepare-timing-results.php <junit-file> <branch> <hash> <timestamp> [timing-metrics-file]\n" ); 15 15 exit( 1 ); 16 16 } 17 17 18 18 try { 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 19 51 $timestamp = new DateTimeImmutable( $argv[4] ); 20 52 $payload = array( … … 24 56 'baseMetrics' => new stdClass(), 25 57 'timestamp' => $timestamp->format( DATE_ATOM ), 26 'metrics' => WP_PHPUnit_Timing_Metrics::from_file( $argv[1] ),58 'metrics' => $timing_metrics, 27 59 ); 28 60 -
trunk/tests/phpunit/tests/includes/junitTimingMetrics.php
r63410 r63412 62 62 } 63 63 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 64 113 public function test_rejects_report_without_testcases() { 65 114 $file = $this->create_junit_file( array(), 0.0 ); … … 71 120 } 72 121 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 73 131 /** 74 132 * Creates a JUnit XML file for a test. 75 133 * 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. 78 136 * @return string Path to the temporary file. 79 137 */ 80 138 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 ) { 81 164 $file = tempnam( sys_get_temp_dir(), 'junit-timing-' ); 82 165 … … 86 169 87 170 $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 ); 99 172 100 173 return $file;
Note:
See TracChangeset
for help on using the changeset viewer.