forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark.php
138 lines (117 loc) · 4.25 KB
/
benchmark.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
require_once __DIR__ . '/shared.php';
$storeResult = ($argv[1] ?? 'false') === 'true';
$phpCgi = $argv[2] ?? dirname(PHP_BINARY) . '/php-cgi';
if (!file_exists($phpCgi)) {
fwrite(STDERR, "php-cgi not found\n");
exit(1);
}
function main() {
global $storeResult;
$profilesDir = __DIR__ . '/profiles';
if (!is_dir($profilesDir)) {
mkdir($profilesDir, 0755, true);
}
$data = [];
if (false !== $branch = getenv('GITHUB_REF_NAME')) {
$data['branch'] = $branch;
}
$data['Zend/bench.php'] = runBench(false);
$data['Zend/bench.php JIT'] = runBench(true);
$data['Symfony Demo 2.2.3'] = runSymfonyDemo(false);
$data['Symfony Demo 2.2.3 JIT'] = runSymfonyDemo(true);
$data['Wordpress 6.2'] = runWordpress(false);
$data['Wordpress 6.2 JIT'] = runWordpress(true);
$result = json_encode($data, JSON_PRETTY_PRINT) . "\n";
fwrite(STDOUT, $result);
if ($storeResult) {
storeResult($result);
}
}
function storeResult(string $result) {
$repo = __DIR__ . '/repos/data';
cloneRepo($repo, '[email protected]:php/benchmarking-data.git');
$commitHash = getPhpSrcCommitHash();
$dir = $repo . '/' . substr($commitHash, 0, 2) . '/' . $commitHash;
$summaryFile = $dir . '/summary.json';
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
file_put_contents($summaryFile, $result);
}
function getPhpSrcCommitHash(): string {
$result = runCommand(['git', 'log', '--pretty=format:%H', '-n', '1'], dirname(__DIR__));
return $result->stdout;
}
function runBench(bool $jit): array {
return runValgrindPhpCgiCommand('bench', [dirname(__DIR__) . '/Zend/bench.php'], jit: $jit);
}
function runSymfonyDemo(bool $jit): array {
$dir = __DIR__ . '/repos/symfony-demo-2.2.3';
cloneRepo($dir, 'https://github.com/php/benchmarking-symfony-demo-2.2.3.git');
runPhpCommand([$dir . '/bin/console', 'cache:clear']);
runPhpCommand([$dir . '/bin/console', 'cache:warmup']);
return runValgrindPhpCgiCommand('symfony-demo', [$dir . '/public/index.php'], cwd: $dir, jit: $jit, warmup: 50, repeat: 50);
}
function runWordpress(bool $jit): array {
$dir = __DIR__ . '/repos/wordpress-6.2';
cloneRepo($dir, 'https://github.com/php/benchmarking-wordpress-6.2.git');
/* FIXME: It might be better to use a stable version of PHP for this command because we can't
* easily alter the phar file */
runPhpCommand([
'-d error_reporting=0',
'wp-cli.phar',
'core',
'install',
'--url=wordpress.local',
'--title="Wordpress"',
'--admin_user=wordpress',
'--admin_password=wordpress',
], $dir);
// Warmup
runPhpCommand([$dir . '/index.php'], $dir);
return runValgrindPhpCgiCommand('wordpress', [$dir . '/index.php'], cwd: $dir, jit: $jit, warmup: 50, repeat: 50);
}
function runPhpCommand(array $args, ?string $cwd = null): ProcessResult {
return runCommand([PHP_BINARY, ...$args], $cwd);
}
function runValgrindPhpCgiCommand(
string $name,
array $args,
?string $cwd = null,
bool $jit = false,
int $warmup = 0,
int $repeat = 1,
): array {
global $phpCgi;
$profileOut = __DIR__ . "/profiles/callgrind.out.$name";
if ($jit) {
$profileOut .= '.jit';
}
$process = runCommand([
'valgrind',
'--tool=callgrind',
'--dump-instr=yes',
"--callgrind-out-file=$profileOut",
'--',
$phpCgi,
'-T' . ($warmup ? $warmup . ',' : '') . $repeat,
'-d max_execution_time=0',
'-d opcache.enable=1',
'-d opcache.jit=' . ($jit ? 'tracing' : 'disable'),
'-d opcache.jit_buffer_size=128M',
'-d opcache.validate_timestamps=0',
...$args,
]);
$instructions = extractInstructionsFromValgrindOutput($process->stderr);
if ($repeat > 1) {
$instructions = gmp_strval(gmp_div_q($instructions, $repeat));
}
return ['instructions' => $instructions];
}
function extractInstructionsFromValgrindOutput(string $output): string {
preg_match("(==[0-9]+== Events : Ir\n==[0-9]+== Collected : (?<instructions>[0-9]+))", $output, $matches);
return $matches['instructions'] ?? throw new \Exception('Unexpected valgrind output');
}
main();