xref: /php-src/benchmark/generate_diff.php (revision ee6f9e29)
1<?php
2
3require_once __DIR__ . '/shared.php';
4
5function main(?string $headCommitHash, ?string $baseCommitHash) {
6    if ($headCommitHash === null || $baseCommitHash === null) {
7        fwrite(STDERR, "Usage: php generate_diff.php HEAD_COMMIT_HASH BASE_COMMIT_HASH\n");
8        exit(1);
9    }
10
11    $repo = __DIR__ . '/repos/data';
12    cloneRepo($repo, 'git@github.com:php/benchmarking-data.git');
13    $headSummaryFile = $repo . '/' . substr($headCommitHash, 0, 2) . '/' . $headCommitHash . '/summary.json';
14    $baseSummaryFile = $repo . '/' . substr($baseCommitHash, 0, 2) . '/' . $baseCommitHash . '/summary.json';
15    if (!file_exists($headSummaryFile)) {
16        return "Head commit '$headCommitHash' not found\n";
17    }
18    if (!file_exists($baseSummaryFile)) {
19        return "Base commit '$baseCommitHash' not found\n";
20    }
21    $headSummary  = json_decode(file_get_contents($headSummaryFile), true);
22    $baseSummary  = json_decode(file_get_contents($baseSummaryFile), true);
23
24    $headCommitHashShort = substr($headCommitHash, 0, 7);
25    $baseCommitHashShort = substr($baseCommitHash, 0, 7);
26    $output = "| Benchmark | Base ($baseCommitHashShort) | Head ($headCommitHashShort) | Diff |\n";
27    $output .= "|---|---|---|---|\n";
28    foreach ($headSummary as $name => $headBenchmark) {
29        if ($name === 'branch') {
30            continue;
31        }
32        $baseInstructions = $baseSummary[$name]['instructions'] ?? null;
33        $headInstructions = $headSummary[$name]['instructions'];
34        $output .= "| $name | "
35            . formatInstructions($baseInstructions) . " | "
36            . formatInstructions($headInstructions) . " | "
37            . formatDiff($baseInstructions, $headInstructions) . " |\n";
38    }
39    return $output;
40}
41
42function formatInstructions(?int $instructions): string {
43    if ($instructions === null) {
44        return '-';
45    }
46    if ($instructions > 1e6) {
47        return sprintf('%.0fM', $instructions / 1e6);
48    } elseif ($instructions > 1e3) {
49        return sprintf('%.0fK', $instructions / 1e3);
50    } else {
51        return (string) $instructions;
52    }
53}
54
55function formatDiff(?int $baseInstructions, int $headInstructions): string {
56    if ($baseInstructions === null) {
57        return '-';
58    }
59    $instructionDiff = $headInstructions - $baseInstructions;
60    return sprintf('%.2f%%', $instructionDiff / $baseInstructions * 100);
61}
62
63$headCommitHash = $argv[1] ?? null;
64$baseCommitHash = $argv[2] ?? null;
65$output = main($headCommitHash, $baseCommitHash);
66fwrite(STDOUT, $output);
67