|
| 1 | +<?php |
| 2 | + |
| 3 | +// SPDX-FileCopyrightText: 2026 LibreSign |
| 4 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +$root = dirname(__DIR__); |
| 9 | +$baselinePath = $root . '/.github/.performance/baseline.json'; |
| 10 | +$outputPath = $root . '/build/benchmark-output.txt'; |
| 11 | + |
| 12 | +$command = $argv[1] ?? null; |
| 13 | +if ($command === null || !in_array($command, ['update', 'check', 'check-stale'], true)) { |
| 14 | + fwrite(STDERR, "Usage: php benchmarks/baseline.php <update|check|check-stale>\n"); |
| 15 | + exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +if (!file_exists($outputPath)) { |
| 19 | + fwrite(STDERR, "Benchmark output file not found: {$outputPath}\n"); |
| 20 | + exit(1); |
| 21 | +} |
| 22 | + |
| 23 | +$output = file_get_contents($outputPath); |
| 24 | +if ($output === false) { |
| 25 | + fwrite(STDERR, "Unable to read benchmark output file.\n"); |
| 26 | + exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +$patterns = [ |
| 30 | + 'LibreSign\\XObjectTemplate\\Benchmarks\\CompilerBench::benchSimpleHtml' => '/benchSimpleHtml.*?Mo([0-9]+(?:\\.[0-9]+)?)(μs|us|ms)/', |
| 31 | + 'LibreSign\\XObjectTemplate\\Benchmarks\\CompilerBench::benchComplexHtml' => '/benchComplexHtml.*?Mo([0-9]+(?:\\.[0-9]+)?)(μs|us|ms)/', |
| 32 | +]; |
| 33 | + |
| 34 | +$current = []; |
| 35 | +foreach ($patterns as $name => $pattern) { |
| 36 | + if (!preg_match($pattern, $output, $matches)) { |
| 37 | + fwrite(STDERR, "Metric not found in benchmark output for {$name}.\n"); |
| 38 | + exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + $value = (float) $matches[1]; |
| 42 | + $unit = $matches[2]; |
| 43 | + $currentMs = $unit === 'ms' ? $value : ($value / 1000.0); |
| 44 | + |
| 45 | + $current[$name] = $currentMs; |
| 46 | +} |
| 47 | + |
| 48 | +if ($command === 'update') { |
| 49 | + $baseline = [ |
| 50 | + 'version' => '1.0.0', |
| 51 | + 'created_at' => gmdate('Y-m-d'), |
| 52 | + 'allowed_regression_pct' => 25.0, |
| 53 | + 'stale_threshold_pct' => 35.0, |
| 54 | + 'benchmarks' => [ |
| 55 | + 'LibreSign\\XObjectTemplate\\Benchmarks\\CompilerBench::benchSimpleHtml' => [ |
| 56 | + 'mean' => round($current['LibreSign\\XObjectTemplate\\Benchmarks\\CompilerBench::benchSimpleHtml'], 6), |
| 57 | + 'memory_real' => 768, |
| 58 | + ], |
| 59 | + 'LibreSign\\XObjectTemplate\\Benchmarks\\CompilerBench::benchComplexHtml' => [ |
| 60 | + 'mean' => round($current['LibreSign\\XObjectTemplate\\Benchmarks\\CompilerBench::benchComplexHtml'], 6), |
| 61 | + 'memory_real' => 1024, |
| 62 | + ], |
| 63 | + ], |
| 64 | + ]; |
| 65 | + |
| 66 | + $encoded = json_encode($baseline, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 67 | + if ($encoded === false) { |
| 68 | + fwrite(STDERR, "Unable to encode baseline JSON.\n"); |
| 69 | + exit(1); |
| 70 | + } |
| 71 | + |
| 72 | + $dir = dirname($baselinePath); |
| 73 | + if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) { |
| 74 | + fwrite(STDERR, "Unable to create baseline directory.\n"); |
| 75 | + exit(1); |
| 76 | + } |
| 77 | + |
| 78 | + if (file_put_contents($baselinePath, $encoded . "\n") === false) { |
| 79 | + fwrite(STDERR, "Unable to write baseline file.\n"); |
| 80 | + exit(1); |
| 81 | + } |
| 82 | + |
| 83 | + fwrite(STDOUT, "Baseline updated: {$baselinePath}\n"); |
| 84 | + exit(0); |
| 85 | +} |
| 86 | + |
| 87 | +if (!file_exists($baselinePath)) { |
| 88 | + fwrite(STDERR, "Baseline file not found: {$baselinePath}\n"); |
| 89 | + exit(1); |
| 90 | +} |
| 91 | + |
| 92 | +$baselineJson = file_get_contents($baselinePath); |
| 93 | +if ($baselineJson === false) { |
| 94 | + fwrite(STDERR, "Unable to read baseline file.\n"); |
| 95 | + exit(1); |
| 96 | +} |
| 97 | + |
| 98 | +$baseline = json_decode($baselineJson, true); |
| 99 | +if (!is_array($baseline)) { |
| 100 | + fwrite(STDERR, "Invalid baseline JSON format.\n"); |
| 101 | + exit(1); |
| 102 | +} |
| 103 | + |
| 104 | +$baselineBenchmarks = $baseline['benchmarks'] ?? []; |
| 105 | +if (!is_array($baselineBenchmarks)) { |
| 106 | + fwrite(STDERR, "Invalid baseline benchmark section.\n"); |
| 107 | + exit(1); |
| 108 | +} |
| 109 | + |
| 110 | +$allowedRegressionPct = (float) ($baseline['allowed_regression_pct'] ?? 25.0); |
| 111 | +$staleThresholdPct = (float) ($baseline['stale_threshold_pct'] ?? 35.0); |
| 112 | + |
| 113 | +$failures = []; |
| 114 | +foreach ($current as $name => $currentMs) { |
| 115 | + if (!isset($baselineBenchmarks[$name]['mean'])) { |
| 116 | + $failures[] = "{$name}: missing baseline entry"; |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + $baselineMs = (float) $baselineBenchmarks[$name]['mean']; |
| 121 | + if ($baselineMs <= 0) { |
| 122 | + $failures[] = "{$name}: invalid baseline mean {$baselineMs}"; |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + $deltaPct = (($currentMs - $baselineMs) / $baselineMs) * 100.0; |
| 127 | + $limitPct = $command === 'check' ? $allowedRegressionPct : $staleThresholdPct; |
| 128 | + |
| 129 | + fwrite( |
| 130 | + STDOUT, |
| 131 | + sprintf( |
| 132 | + "%s: current=%.6fms baseline=%.6fms delta=%+.2f%% limit=%s%.2f%%\n", |
| 133 | + $name, |
| 134 | + $currentMs, |
| 135 | + $baselineMs, |
| 136 | + $deltaPct, |
| 137 | + $command === 'check' ? '+' : '±', |
| 138 | + $limitPct, |
| 139 | + ), |
| 140 | + ); |
| 141 | + |
| 142 | + if ($command === 'check' && $deltaPct > $allowedRegressionPct) { |
| 143 | + $failures[] = sprintf( |
| 144 | + '%s: regression detected (%+.2f%% > +%.2f%%)', |
| 145 | + $name, |
| 146 | + $deltaPct, |
| 147 | + $allowedRegressionPct, |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + if ($command === 'check-stale' && abs($deltaPct) > $staleThresholdPct) { |
| 152 | + $failures[] = sprintf( |
| 153 | + '%s: baseline appears stale (%+.2f%% exceeds ±%.2f%%). Run composer benchmark:baseline:update', |
| 154 | + $name, |
| 155 | + $deltaPct, |
| 156 | + $staleThresholdPct, |
| 157 | + ); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +if ($failures !== []) { |
| 162 | + fwrite(STDERR, "\nBaseline validation failed:\n"); |
| 163 | + foreach ($failures as $failure) { |
| 164 | + fwrite(STDERR, " - {$failure}\n"); |
| 165 | + } |
| 166 | + exit(1); |
| 167 | +} |
| 168 | + |
| 169 | +fwrite(STDOUT, "\nBaseline validation passed.\n"); |
0 commit comments