-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add benchmarking to CI #11068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add benchmarking to CI #11068
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0d31d89
Add benchmarking to CI
iluuu1994 c16eb27
Fix issues
iluuu1994 375190a
Adjust benchmark keys
iluuu1994 74dee73
Add JIT benchmarks
iluuu1994 b0e0a86
Use git commit hashes for merge-base
iluuu1994 7da2d6a
Remove (possibly?) unused apt packages
iluuu1994 ed93016
Renames
iluuu1994 ffcf6ec
Pull before commiting benchmarking files
iluuu1994 761ec91
Increase warmup
iluuu1994 6465986
Fix oci warning
iluuu1994 e8c07d3
improve git handling
iluuu1994 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/repos |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?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; | ||
|
||
$data = []; | ||
$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, 'git@github.com: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([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([$dir . '/public/index.php'], cwd: $dir, jit: $jit, warmup: 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', | ||
'--admin_email=benchmark@php.net', | ||
], $dir); | ||
|
||
// Warmup | ||
runPhpCommand([$dir . '/index.php'], $dir); | ||
return runValgrindPhpCgiCommand([$dir . '/index.php'], cwd: $dir, jit: $jit, warmup: 50); | ||
} | ||
|
||
function runPhpCommand(array $args, ?string $cwd = null): ProcessResult { | ||
return runCommand([PHP_BINARY, ...$args], $cwd); | ||
} | ||
|
||
function runValgrindPhpCgiCommand( | ||
array $args, | ||
?string $cwd = null, | ||
bool $jit = false, | ||
int $warmup = 0, | ||
): array { | ||
global $phpCgi; | ||
$process = runCommand([ | ||
'valgrind', | ||
'--tool=callgrind', | ||
'--dump-instr=yes', | ||
'--callgrind-out-file=/dev/null', | ||
'--', | ||
$phpCgi, | ||
'-T' . ($warmup ? $warmup . ',' : '') . '1', | ||
'-d max_execution_time=0', | ||
'-d opcache.enable=1', | ||
'-d opcache.jit_buffer_size=' . ($jit ? '128M' : '0'), | ||
...$args, | ||
]); | ||
$instructions = extractInstructionsFromValgrindOutput($process->stderr); | ||
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'] ?? null; | ||
} | ||
|
||
main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
version: "3.8" | ||
iluuu1994 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
services: | ||
wordpress_db: | ||
image: mysql:8.0 | ||
ports: | ||
- "3306:3306" | ||
environment: | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_DATABASE: wordpress | ||
MYSQL_USER: wordpress | ||
MYSQL_PASSWORD: wordpress |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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'); | ||
$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) { | ||
$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); | ||
} | ||
|
||
$headCommitHash = $argv[1] ?? null; | ||
$baseCommitHash = $argv[2] ?? null; | ||
$output = main($headCommitHash, $baseCommitHash); | ||
fwrite(STDOUT, $output); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
class ProcessResult { | ||
public $stdout; | ||
public $stderr; | ||
} | ||
|
||
function runCommand(array $args, ?string $cwd = null): ProcessResult { | ||
$cmd = implode(' ', array_map('escapeshellarg', $args)); | ||
$pipes = null; | ||
$result = new ProcessResult(); | ||
$descriptorSpec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; | ||
fwrite(STDOUT, "> $cmd\n"); | ||
$processHandle = proc_open($cmd, $descriptorSpec, $pipes, $cwd ?? getcwd(), null); | ||
|
||
$stdin = $pipes[0]; | ||
$stdout = $pipes[1]; | ||
$stderr = $pipes[2]; | ||
|
||
fclose($stdin); | ||
|
||
stream_set_blocking($stdout, false); | ||
stream_set_blocking($stderr, false); | ||
|
||
$stdoutEof = false; | ||
$stderrEof = false; | ||
|
||
do { | ||
$read = [$stdout, $stderr]; | ||
$write = null; | ||
$except = null; | ||
|
||
stream_select($read, $write, $except, 1, 0); | ||
|
||
foreach ($read as $stream) { | ||
$chunk = fgets($stream); | ||
if ($stream === $stdout) { | ||
$result->stdout .= $chunk; | ||
} elseif ($stream === $stderr) { | ||
$result->stderr .= $chunk; | ||
} | ||
} | ||
|
||
$stdoutEof = $stdoutEof || feof($stdout); | ||
$stderrEof = $stderrEof || feof($stderr); | ||
} while(!$stdoutEof || !$stderrEof); | ||
|
||
fclose($stdout); | ||
fclose($stderr); | ||
|
||
$statusCode = proc_close($processHandle); | ||
if ($statusCode !== 0) { | ||
fwrite(STDOUT, $result->stdout); | ||
fwrite(STDERR, $result->stderr); | ||
fwrite(STDERR, 'Exited with status code ' . $statusCode . "\n"); | ||
exit($statusCode); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
function cloneRepo(string $path, string $url) { | ||
if (is_dir($path)) { | ||
return; | ||
} | ||
$dir = dirname($path); | ||
$repo = basename($path); | ||
if (!is_dir($dir)) { | ||
mkdir($dir, 0755, true); | ||
} | ||
runCommand(['git', 'clone', '-q', '--end-of-options', $url, $repo], dirname($path)); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.