-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Build/Test Tools: Publish PHPUnit timing metrics to CodeVitals #13083
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
Open
lancewillett
wants to merge
3
commits into
WordPress:trunk
Choose a base branch
from
lancewillett:feat/65887-publish-phpunit-metrics
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tests/phpunit/includes/class-wp-phpunit-timing-metrics.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Extracts aggregate timing metrics from a PHPUnit JUnit report. | ||
| */ | ||
| final class WP_PHPUnit_Timing_Metrics { | ||
|
|
||
| /** | ||
| * Extracts timing metrics from a JUnit XML file. | ||
| * | ||
| * @param string $file Path to the JUnit XML file. | ||
| * @return array<string, float|int> Timing metrics keyed for CodeVitals. | ||
| * @throws RuntimeException If the file cannot be read or contains invalid timing data. | ||
| */ | ||
| public static function from_file( $file ) { | ||
| if ( ! is_readable( $file ) ) { | ||
| throw new RuntimeException( 'The JUnit timing report could not be read.' ); | ||
| } | ||
|
|
||
| $reader = new XMLReader(); | ||
| if ( ! $reader->open( $file, null, LIBXML_NONET | LIBXML_COMPACT ) ) { | ||
| throw new RuntimeException( 'The JUnit timing report could not be opened.' ); | ||
| } | ||
|
|
||
| $suite_time = null; | ||
| $test_times = array(); | ||
|
|
||
| while ( $reader->read() ) { | ||
| if ( XMLReader::ELEMENT !== $reader->nodeType ) { | ||
| continue; | ||
| } | ||
|
|
||
| if ( null === $suite_time && 'testsuite' === $reader->name ) { | ||
| $time = $reader->getAttribute( 'time' ); | ||
| if ( is_numeric( $time ) ) { | ||
| $suite_time = (float) $time; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if ( 'testcase' !== $reader->name ) { | ||
| continue; | ||
| } | ||
|
|
||
| $time = $reader->getAttribute( 'time' ); | ||
| if ( ! is_numeric( $time ) ) { | ||
| $reader->close(); | ||
| throw new RuntimeException( 'A JUnit testcase is missing numeric timing data.' ); | ||
| } | ||
|
|
||
| $test_times[] = (float) $time; | ||
| } | ||
|
|
||
| $reader->close(); | ||
|
|
||
| if ( ! $test_times ) { | ||
| throw new RuntimeException( 'The JUnit timing report contains no testcases.' ); | ||
| } | ||
|
|
||
| if ( null === $suite_time ) { | ||
| $suite_time = array_sum( $test_times ); | ||
| } | ||
|
|
||
| sort( $test_times, SORT_NUMERIC ); | ||
|
|
||
| return array( | ||
| 'phpunit-suite-time' => round( $suite_time, 6 ), | ||
| 'phpunit-p95-test-time' => round( self::percentile( $test_times, 0.95 ) * 1000, 3 ), | ||
| 'phpunit-p99-test-time' => round( self::percentile( $test_times, 0.99 ) * 1000, 3 ), | ||
| 'phpunit-max-test-time' => round( max( $test_times ) * 1000, 3 ), | ||
| 'phpunit-tests-over-500ms' => count( array_filter( $test_times, static fn ( $time ) => $time > 0.5 ) ), | ||
| 'phpunit-tests-over-1s' => count( array_filter( $test_times, static fn ( $time ) => $time > 1 ) ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Calculates a nearest-rank percentile from a sorted list. | ||
| * | ||
| * @param float[] $values Sorted values. | ||
| * @param float $percentile Percentile between zero and one. | ||
| * @return float Percentile value. | ||
| */ | ||
| private static function percentile( $values, $percentile ) { | ||
| $index = (int) ceil( $percentile * count( $values ) ) - 1; | ||
|
|
||
| return $values[ max( 0, $index ) ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| /** | ||
| * Prepares aggregate PHPUnit timing metrics for publication to CodeVitals. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage UnitTests | ||
| */ | ||
|
|
||
| require_once __DIR__ . '/includes/class-wp-phpunit-timing-metrics.php'; | ||
|
|
||
| if ( 5 !== $argc ) { | ||
| fwrite( STDERR, "Usage: prepare-timing-results.php <junit-file> <branch> <hash> <timestamp>\n" ); | ||
| exit( 1 ); | ||
| } | ||
|
|
||
| try { | ||
| $timestamp = new DateTimeImmutable( $argv[4] ); | ||
| $payload = array( | ||
| 'branch' => $argv[2], | ||
| 'hash' => $argv[3], | ||
| 'baseHash' => $argv[3], | ||
| 'timestamp' => $timestamp->format( DATE_ATOM ), | ||
| 'metrics' => WP_PHPUnit_Timing_Metrics::from_file( $argv[1] ), | ||
| ); | ||
|
|
||
| echo json_encode( $payload, JSON_THROW_ON_ERROR ) . "\n"; | ||
| } catch ( Throwable $error ) { | ||
| fwrite( STDERR, $error->getMessage() . "\n" ); | ||
| exit( 1 ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| require_once dirname( __DIR__, 2 ) . '/includes/class-wp-phpunit-timing-metrics.php'; | ||
|
|
||
| /** | ||
| * @group core-vitals-tooling | ||
| * | ||
| * @ticket 65887 | ||
| */ | ||
| class Tests_Includes_JUnit_Timing_Metrics extends WP_UnitTestCase { | ||
|
|
||
| /** | ||
| * Temporary files created by a test. | ||
| * | ||
| * @var string[] | ||
| */ | ||
| private $temporary_files = array(); | ||
|
|
||
| public function tear_down() { | ||
| foreach ( $this->temporary_files as $file ) { | ||
| unlink( $file ); | ||
| } | ||
|
|
||
| parent::tear_down(); | ||
| } | ||
|
|
||
| public function test_extracts_aggregate_timing_metrics() { | ||
| $times = array_map( | ||
| static fn ( $millisecond ) => $millisecond / 1000, | ||
| range( 1, 100 ) | ||
| ); | ||
| $file = $this->create_junit_file( $times, 5.05 ); | ||
|
|
||
| $this->assertSame( | ||
| array( | ||
| 'phpunit-suite-time' => 5.05, | ||
| 'phpunit-p95-test-time' => 95.0, | ||
| 'phpunit-p99-test-time' => 99.0, | ||
| 'phpunit-max-test-time' => 100.0, | ||
| 'phpunit-tests-over-500ms' => 0, | ||
| 'phpunit-tests-over-1s' => 0, | ||
| ), | ||
| WP_PHPUnit_Timing_Metrics::from_file( $file ) | ||
| ); | ||
| } | ||
|
|
||
| public function test_counts_only_tests_above_slow_test_thresholds() { | ||
| $file = $this->create_junit_file( array( 0.5, 0.500001, 1.0, 1.000001 ), 3.000002 ); | ||
|
|
||
| $metrics = WP_PHPUnit_Timing_Metrics::from_file( $file ); | ||
|
|
||
| $this->assertSame( 3, $metrics['phpunit-tests-over-500ms'] ); | ||
| $this->assertSame( 1, $metrics['phpunit-tests-over-1s'] ); | ||
| } | ||
|
|
||
| public function test_uses_testcase_time_when_suite_time_is_missing() { | ||
| $file = $this->create_junit_file( array( 0.1, 0.2, 0.3 ) ); | ||
|
|
||
| $metrics = WP_PHPUnit_Timing_Metrics::from_file( $file ); | ||
|
|
||
| $this->assertSame( 0.6, $metrics['phpunit-suite-time'] ); | ||
| } | ||
|
|
||
| public function test_rejects_report_without_testcases() { | ||
| $file = $this->create_junit_file( array(), 0.0 ); | ||
|
|
||
| $this->expectException( RuntimeException::class ); | ||
| $this->expectExceptionMessage( 'The JUnit timing report contains no testcases.' ); | ||
|
|
||
| WP_PHPUnit_Timing_Metrics::from_file( $file ); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a JUnit XML file for a test. | ||
| * | ||
| * @param float[] $times Testcase times in seconds. | ||
| * @param float|int $suite_time Optional testsuite time in seconds. | ||
| * @return string Path to the temporary file. | ||
| */ | ||
| private function create_junit_file( $times, $suite_time = null ) { | ||
| $file = tempnam( sys_get_temp_dir(), 'junit-timing-' ); | ||
| $this->temporary_files[] = $file; | ||
| $suite_time_attribute = null === $suite_time ? '' : sprintf( ' time="%s"', $suite_time ); | ||
| $testcases = ''; | ||
|
|
||
| foreach ( $times as $index => $time ) { | ||
| $testcases .= sprintf( '<testcase name="test_%1$d" time="%2$s"/>', $index, $time ); | ||
| } | ||
|
|
||
| file_put_contents( | ||
| $file, | ||
| sprintf( '<?xml version="1.0"?><testsuites><testsuite tests="%1$d"%2$s>%3$s</testsuite></testsuites>', count( $times ), $suite_time_attribute, $testcases ) | ||
| ); | ||
|
|
||
| return $file; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.