File: generate_diff.php

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (89 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

require_once __DIR__ . '/shared.php';

function main(?string $headCommitHash, ?string $baseCommitHash) {
    if ($headCommitHash === null || $baseCommitHash === null) {
        fwrite(STDERR, "Usage: php generate_diff.php HEAD_COMMIT_HASH BASE_COMMIT_HASH\n");
        exit(1);
    }

    $repo = __DIR__ . '/repos/data';
    cloneRepo($repo, 'git@github.com:php/benchmarking-data.git');
    $baseCommitHash = find_benchmarked_commit_hash($repo, $baseCommitHash);
    $headSummaryFile = $repo . '/' . substr($headCommitHash, 0, 2) . '/' . $headCommitHash . '/summary.json';
    $baseSummaryFile = $repo . '/' . substr($baseCommitHash, 0, 2) . '/' . $baseCommitHash . '/summary.json';
    if (!file_exists($headSummaryFile)) {
        return "Head commit '$headCommitHash' not found\n";
    }
    if (!file_exists($baseSummaryFile)) {
        return "Base commit '$baseCommitHash' not found\n";
    }
    $headSummary  = json_decode(file_get_contents($headSummaryFile), true);
    $baseSummary  = json_decode(file_get_contents($baseSummaryFile), true);

    $headCommitHashShort = substr($headCommitHash, 0, 7);
    $baseCommitHashShort = substr($baseCommitHash, 0, 7);
    $output = "| Benchmark | Base ($baseCommitHashShort) | Head ($headCommitHashShort) | Diff |\n";
    $output .= "|---|---|---|---|\n";
    foreach ($headSummary as $name => $headBenchmark) {
        if ($name === 'branch') {
            continue;
        }
        $baseInstructions = $baseSummary[$name]['instructions'] ?? null;
        $headInstructions = $headSummary[$name]['instructions'];
        $output .= "| $name | "
            . formatInstructions($baseInstructions) . " | "
            . formatInstructions($headInstructions) . " | "
            . formatDiff($baseInstructions, $headInstructions) . " |\n";
    }
    return $output;
}

function formatInstructions(?int $instructions): string {
    if ($instructions === null) {
        return '-';
    }
    if ($instructions > 1e6) {
        return sprintf('%.0fM', $instructions / 1e6);
    } elseif ($instructions > 1e3) {
        return sprintf('%.0fK', $instructions / 1e3);
    } else {
        return (string) $instructions;
    }
}

function formatDiff(?int $baseInstructions, int $headInstructions): string {
    if ($baseInstructions === null) {
        return '-';
    }
    $instructionDiff = $headInstructions - $baseInstructions;
    return sprintf('%.2f%%', $instructionDiff / $baseInstructions * 100);
}

function find_benchmarked_commit_hash(string $repo, string $commitHash): ?string {
    $repeat = 10;

    while (true) {
        if ($repeat-- <= 0) {
            fwrite(STDERR, "Count not find benchmarked commit hash\n");
            exit(1);
        }
        $summaryFile = $repo . '/' . substr($commitHash, 0, 2) . '/' . $commitHash . '/summary.json';
        if (file_exists($summaryFile)) {
            break;
        }
        $commitHash = trim(runCommand(
            ['git', 'rev-parse', $commitHash . '^'],
            dirname(__DIR__),
            printCommand: false,
        )->stdout);
    }

    return $commitHash;
}

$headCommitHash = $argv[1] ?? null;
$baseCommitHash = $argv[2] ?? null;
$output = main($headCommitHash, $baseCommitHash);
fwrite(STDOUT, $output);