Skip to content

Commit ea95d00

Browse files
committed
Build/Test Tools: Publish PHPUnit timing metrics to CodeVitals.
The canonical PHPUnit job now publishes six aggregate timing metrics to the public CodeVitals dashboard after every push to trunk: total suite time, the p95, p99 and maximum per-test times, and counts of tests over 500ms and over 1s. The publisher reads the JUnit report the suite already produces, streams it, and sends only aggregates. No test names or per-test records leave CI. The metric definitions are verified on submission, so a missing definition fails the step rather than silently dropping data. Developed in: WordPress#13083 Props desrosj, adrianmoldovanwp, johnbillion, youknowriad. See #65887. git-svn-id: https://develop.svn.wordpress.org/trunk@63410 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4676feb commit ea95d00

5 files changed

Lines changed: 262 additions & 0 deletions

File tree

‎.github/workflows/phpunit-tests.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ jobs:
8181
permissions:
8282
contents: read
8383
secrets:
84+
CODEVITALS_PROJECT_TOKEN: ${{ secrets.CODEVITALS_PROJECT_TOKEN }}
8485
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
8586
WPT_REPORT_API_KEY: ${{ secrets.WPT_REPORT_API_KEY }}
8687
if: |

‎.github/workflows/reusable-phpunit-tests-v3.yml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ on:
8383
type: string
8484
default: ''
8585
secrets:
86+
CODEVITALS_PROJECT_TOKEN:
87+
description: 'The authorization token for publishing results to CodeVitals.'
88+
required: false
8689
CODECOV_TOKEN:
8790
description: 'The Codecov token required for uploading reports.'
8891
required: false
@@ -123,6 +126,7 @@ jobs:
123126
# - Logs debug information about what's installed within the WordPress Docker containers.
124127
# - Install WordPress within the Docker container.
125128
# - Run the PHPUnit tests.
129+
# - Publish PHPUnit timing metrics to CodeVitals.
126130
# - Upload the code coverage report to Codecov.io.
127131
# - Ensures version-controlled files are not modified or deleted.
128132
# - Checks out the WordPress Test reporter repository.
@@ -269,6 +273,40 @@ jobs:
269273
TEST_GROUPS: ${{ inputs.phpunit-test-groups }}
270274
MULTISITE_FLAG: ${{ inputs.multisite && 'multisite' || 'single' }}
271275

276+
- name: Publish PHPUnit timing metrics
277+
continue-on-error: true
278+
if: |
279+
github.event_name == 'push' &&
280+
github.ref == 'refs/heads/trunk' &&
281+
inputs.php == '8.5' &&
282+
inputs.report
283+
env:
284+
CODEVITALS_PROJECT_TOKEN: ${{ secrets.CODEVITALS_PROJECT_TOKEN }}
285+
shell: bash
286+
run: |
287+
if [ -z "$CODEVITALS_PROJECT_TOKEN" ]; then
288+
echo "PHPUnit timing metrics could not be published. 'CODEVITALS_PROJECT_TOKEN' is not set"
289+
exit 1
290+
fi
291+
COMMITTED_AT="$(git show -s "$GITHUB_SHA" --format='%cI')"
292+
RESPONSE="$(
293+
php tests/phpunit/prepare-timing-results.php \
294+
tests/phpunit/build/logs/junit.xml \
295+
trunk \
296+
"$GITHUB_SHA" \
297+
"$COMMITTED_AT" \
298+
| curl --fail-with-body --silent --show-error \
299+
--request POST \
300+
--header 'Content-Type: application/json' \
301+
--data-binary @- \
302+
"https://codevitals.run/api/log?token=${CODEVITALS_PROJECT_TOKEN}"
303+
)"
304+
if ! php -r "\$d = json_decode(file_get_contents('php://stdin'), true); exit(is_array(\$d) && (\$d['status'] ?? null) === 'ok' && (int) (\$d['count'] ?? 0) === 6 ? 0 : 1);" <<< "$RESPONSE"; then
305+
echo 'CodeVitals did not accept all six PHPUnit timing metrics.'
306+
exit 1
307+
fi
308+
echo 'Published six PHPUnit timing metrics to CodeVitals.'
309+
272310
- name: Run AJAX tests
273311
if: ${{ ! inputs.phpunit-test-groups && ! inputs.coverage-report }}
274312
continue-on-error: ${{ inputs.allow-errors }}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
/**
4+
* Extracts aggregate timing metrics from a PHPUnit JUnit report.
5+
*/
6+
final class WP_PHPUnit_Timing_Metrics {
7+
8+
/**
9+
* Extracts timing metrics from a JUnit XML file.
10+
*
11+
* @param string $file Path to the JUnit XML file.
12+
* @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+
*/
15+
public static function from_file( $file ) {
16+
if ( ! is_readable( $file ) ) {
17+
throw new RuntimeException( 'The JUnit timing report could not be read.' );
18+
}
19+
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.' );
23+
}
24+
25+
$suite_time = null;
26+
$test_times = array();
27+
28+
while ( $reader->read() ) {
29+
if ( XMLReader::ELEMENT !== $reader->nodeType ) {
30+
continue;
31+
}
32+
33+
if ( null === $suite_time && 'testsuite' === $reader->name ) {
34+
$time = $reader->getAttribute( 'time' );
35+
if ( is_numeric( $time ) ) {
36+
$suite_time = (float) $time;
37+
}
38+
continue;
39+
}
40+
41+
if ( 'testcase' !== $reader->name ) {
42+
continue;
43+
}
44+
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;
52+
}
53+
54+
$reader->close();
55+
56+
if ( ! $test_times ) {
57+
throw new RuntimeException( 'The JUnit timing report contains no testcases.' );
58+
}
59+
60+
if ( null === $suite_time ) {
61+
$suite_time = array_sum( $test_times );
62+
}
63+
64+
sort( $test_times, SORT_NUMERIC );
65+
66+
return array(
67+
'phpunit-suite-time' => round( $suite_time, 6 ),
68+
'phpunit-p95-test-time' => round( self::percentile( $test_times, 0.95 ) * 1000, 3 ),
69+
'phpunit-p99-test-time' => round( self::percentile( $test_times, 0.99 ) * 1000, 3 ),
70+
'phpunit-max-test-time' => round( max( $test_times ) * 1000, 3 ),
71+
'phpunit-tests-over-500ms' => count( array_filter( $test_times, static fn ( $time ) => $time > 0.5 ) ),
72+
'phpunit-tests-over-1s' => count( array_filter( $test_times, static fn ( $time ) => $time > 1 ) ),
73+
);
74+
}
75+
76+
/**
77+
* Calculates a nearest-rank percentile from a sorted list.
78+
*
79+
* @param float[] $values Sorted values.
80+
* @param float $percentile Percentile between zero and one.
81+
* @return float Percentile value.
82+
*/
83+
private static function percentile( $values, $percentile ) {
84+
$index = (int) ceil( $percentile * count( $values ) ) - 1;
85+
86+
return $values[ max( 0, $index ) ];
87+
}
88+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/**
5+
* Prepares aggregate PHPUnit timing metrics for publication to CodeVitals.
6+
*
7+
* @package WordPress
8+
* @subpackage UnitTests
9+
*/
10+
11+
require_once __DIR__ . '/includes/class-wp-phpunit-timing-metrics.php';
12+
13+
if ( 5 !== $argc ) {
14+
fwrite( STDERR, "Usage: prepare-timing-results.php <junit-file> <branch> <hash> <timestamp>\n" );
15+
exit( 1 );
16+
}
17+
18+
try {
19+
$timestamp = new DateTimeImmutable( $argv[4] );
20+
$payload = array(
21+
'branch' => $argv[2],
22+
'hash' => $argv[3],
23+
'baseHash' => $argv[3],
24+
'baseMetrics' => new stdClass(),
25+
'timestamp' => $timestamp->format( DATE_ATOM ),
26+
'metrics' => WP_PHPUnit_Timing_Metrics::from_file( $argv[1] ),
27+
);
28+
29+
echo json_encode( $payload, JSON_THROW_ON_ERROR ) . "\n";
30+
} catch ( Throwable $error ) {
31+
fwrite( STDERR, $error->getMessage() . "\n" );
32+
exit( 1 );
33+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
require_once dirname( __DIR__, 2 ) . '/includes/class-wp-phpunit-timing-metrics.php';
4+
5+
/**
6+
* @group core-vitals-tooling
7+
*
8+
* @ticket 65887
9+
*/
10+
class Tests_Includes_JUnit_Timing_Metrics extends WP_UnitTestCase {
11+
12+
/**
13+
* Temporary files created by a test.
14+
*
15+
* @var string[]
16+
*/
17+
private $temporary_files = array();
18+
19+
public function tear_down() {
20+
foreach ( $this->temporary_files as $file ) {
21+
unlink( $file );
22+
}
23+
24+
parent::tear_down();
25+
}
26+
27+
public function test_extracts_aggregate_timing_metrics() {
28+
$times = array_map(
29+
static fn ( $millisecond ) => $millisecond / 1000,
30+
range( 1, 100 )
31+
);
32+
$file = $this->create_junit_file( $times, 5.05 );
33+
34+
$this->assertSame(
35+
array(
36+
'phpunit-suite-time' => 5.05,
37+
'phpunit-p95-test-time' => 95.0,
38+
'phpunit-p99-test-time' => 99.0,
39+
'phpunit-max-test-time' => 100.0,
40+
'phpunit-tests-over-500ms' => 0,
41+
'phpunit-tests-over-1s' => 0,
42+
),
43+
WP_PHPUnit_Timing_Metrics::from_file( $file )
44+
);
45+
}
46+
47+
public function test_counts_only_tests_above_slow_test_thresholds() {
48+
$file = $this->create_junit_file( array( 0.5, 0.500001, 1.0, 1.000001 ), 3.000002 );
49+
50+
$metrics = WP_PHPUnit_Timing_Metrics::from_file( $file );
51+
52+
$this->assertSame( 3, $metrics['phpunit-tests-over-500ms'] );
53+
$this->assertSame( 1, $metrics['phpunit-tests-over-1s'] );
54+
}
55+
56+
public function test_uses_testcase_time_when_suite_time_is_missing() {
57+
$file = $this->create_junit_file( array( 0.1, 0.2, 0.3 ) );
58+
59+
$metrics = WP_PHPUnit_Timing_Metrics::from_file( $file );
60+
61+
$this->assertSame( 0.6, $metrics['phpunit-suite-time'] );
62+
}
63+
64+
public function test_rejects_report_without_testcases() {
65+
$file = $this->create_junit_file( array(), 0.0 );
66+
67+
$this->expectException( RuntimeException::class );
68+
$this->expectExceptionMessage( 'The JUnit timing report contains no testcases.' );
69+
70+
WP_PHPUnit_Timing_Metrics::from_file( $file );
71+
}
72+
73+
/**
74+
* Creates a JUnit XML file for a test.
75+
*
76+
* @param float[] $times Testcase times in seconds.
77+
* @param float|int $suite_time Optional testsuite time in seconds.
78+
* @return string Path to the temporary file.
79+
*/
80+
private function create_junit_file( $times, $suite_time = null ) {
81+
$file = tempnam( sys_get_temp_dir(), 'junit-timing-' );
82+
83+
if ( false === $file ) {
84+
$this->fail( 'Failed to create a temporary JUnit file.' );
85+
}
86+
87+
$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+
);
99+
100+
return $file;
101+
}
102+
}

0 commit comments

Comments
 (0)