|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Analyse a PHPUnit JUnit log: slowest tests, slowest classes, and whether the |
| 11 | + * suite degrades over execution order. |
| 12 | + * |
| 13 | + * Usage: php tests/junit-analyzer.php [junit.xml] [topN] |
| 14 | + * |
| 15 | + * The bucket table splits the run into equal chunks of execution order. A rising |
| 16 | + * median means the suite itself degrades (accumulated DB rows, leaked memory); |
| 17 | + * a flat median with rising sum/max means a few slow tests happen to run late. |
| 18 | + */ |
| 19 | + |
| 20 | +const BUCKETS = 10; |
| 21 | + |
| 22 | +$file = $argv[1] ?? 'junit.xml'; |
| 23 | +$topCount = (int)($argv[2] ?? 30); |
| 24 | + |
| 25 | +if (!is_readable($file)) { |
| 26 | + fwrite(STDERR, "cannot read $file\n"); |
| 27 | + exit(1); |
| 28 | +} |
| 29 | + |
| 30 | +libxml_use_internal_errors(true); |
| 31 | + |
| 32 | +$reader = new XMLReader(); |
| 33 | +if (!$reader->open($file)) { |
| 34 | + fwrite(STDERR, "cannot open $file\n"); |
| 35 | + exit(1); |
| 36 | +} |
| 37 | + |
| 38 | +/** @var list<array{class: string, name: string, duration: float}> $tests in execution order */ |
| 39 | +$tests = []; |
| 40 | +while ($reader->read()) { |
| 41 | + if ($reader->nodeType !== XMLReader::ELEMENT || $reader->name !== 'testcase') { |
| 42 | + continue; |
| 43 | + } |
| 44 | + $tests[] = [ |
| 45 | + 'class' => $reader->getAttribute('class') ?: '(none)', |
| 46 | + 'name' => (string)$reader->getAttribute('name'), |
| 47 | + 'duration' => (float)$reader->getAttribute('time'), |
| 48 | + ]; |
| 49 | +} |
| 50 | +$reader->close(); |
| 51 | + |
| 52 | +$testCount = count($tests); |
| 53 | +if ($testCount === 0) { |
| 54 | + $error = libxml_get_errors()[0] ?? null; |
| 55 | + fwrite(STDERR, "no testcase elements found in $file" |
| 56 | + . ($error !== null ? ': ' . trim($error->message) : '') . "\n"); |
| 57 | + exit(1); |
| 58 | +} |
| 59 | + |
| 60 | +$totalDuration = array_sum(array_column($tests, 'duration')); |
| 61 | +printf("%d tests, %.1fs total (%.1f min)\n\n", $testCount, $totalDuration, $totalDuration / 60); |
| 62 | + |
| 63 | +if ($totalDuration <= 0) { |
| 64 | + fwrite(STDERR, "no timing data to rank\n"); |
| 65 | + exit(0); |
| 66 | +} |
| 67 | + |
| 68 | +$chunks = array_chunk($tests, (int)ceil($testCount / BUCKETS)); |
| 69 | +$buckets = count($chunks); |
| 70 | + |
| 71 | +printf("Execution order, %d buckets (are later tests slower?)\n", $buckets); |
| 72 | +echo " bucket tests sum(s) mean(ms) median(ms) max(s) cum%\n"; |
| 73 | + |
| 74 | +$durationSoFar = 0.0; |
| 75 | +foreach ($chunks as $bucket => $chunk) { |
| 76 | + $durations = array_column($chunk, 'duration'); |
| 77 | + sort($durations); |
| 78 | + $inBucket = count($durations); |
| 79 | + $bucketDuration = array_sum($durations); |
| 80 | + $durationSoFar += $bucketDuration; |
| 81 | + |
| 82 | + printf( |
| 83 | + " %3d-%3d%% %7d %9.1f %10.2f %12.2f %9.2f %5.1f%%\n", |
| 84 | + $bucket * 100 / $buckets, |
| 85 | + ($bucket + 1) * 100 / $buckets, |
| 86 | + $inBucket, |
| 87 | + $bucketDuration, |
| 88 | + $bucketDuration / $inBucket * 1000, |
| 89 | + $durations[intdiv($inBucket, 2)] * 1000, |
| 90 | + max($durations), |
| 91 | + $durationSoFar / $totalDuration * 100, |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +$slowestFirst = $tests; |
| 96 | +usort($slowestFirst, static fn (array $a, array $b): int => $b['duration'] <=> $a['duration']); |
| 97 | + |
| 98 | +printf("\nTop %d slowest tests\n", $topCount); |
| 99 | +foreach (array_slice($slowestFirst, 0, $topCount) as $test) { |
| 100 | + printf(" %8.2fs %s::%s\n", $test['duration'], $test['class'], $test['name']); |
| 101 | +} |
| 102 | + |
| 103 | +/** @var array<string, array{duration: float, tests: int}> $classes */ |
| 104 | +$classes = []; |
| 105 | +foreach ($tests as $test) { |
| 106 | + $classes[$test['class']] ??= ['duration' => 0.0, 'tests' => 0]; |
| 107 | + $classes[$test['class']]['duration'] += $test['duration']; |
| 108 | + $classes[$test['class']]['tests']++; |
| 109 | +} |
| 110 | +uasort($classes, static fn (array $a, array $b): int => $b['duration'] <=> $a['duration']); |
| 111 | + |
| 112 | +printf("\nTop %d slowest classes (sum of its tests)\n", $topCount); |
| 113 | +printf(" %9s %7s %10s %s\n", 'sum', 'tests', 'mean(ms)', 'class'); |
| 114 | +foreach (array_slice($classes, 0, $topCount, true) as $class => $stats) { |
| 115 | + printf( |
| 116 | + " %8.2fs %7d %10.2f %s\n", |
| 117 | + $stats['duration'], |
| 118 | + $stats['tests'], |
| 119 | + $stats['duration'] / $stats['tests'] * 1000, |
| 120 | + $class, |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +// How top-heavy is the run? A handful of tests dominating reads very differently |
| 125 | +// from the cost being spread evenly. |
| 126 | +$durationSoFar = 0.0; |
| 127 | +$testsInHalfTheRuntime = 0; |
| 128 | +foreach ($slowestFirst as $test) { |
| 129 | + $durationSoFar += $test['duration']; |
| 130 | + $testsInHalfTheRuntime++; |
| 131 | + if ($durationSoFar >= $totalDuration / 2) { |
| 132 | + break; |
| 133 | + } |
| 134 | +} |
| 135 | +printf( |
| 136 | + "\nThe slowest %d tests (%.1f%% of tests) account for 50%% of the runtime.\n", |
| 137 | + $testsInHalfTheRuntime, |
| 138 | + $testsInHalfTheRuntime / $testCount * 100, |
| 139 | +); |
0 commit comments