Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,77 @@ jobs:
run: .github/scripts/windows/build.bat
- name: Test
run: .github/scripts/windows/test.bat
BENCHMARKING:
name: BENCHMARKING
runs-on: ubuntu-22.04
steps:
- name: git checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: apt
run: |
set -x
sudo apt-get update
sudo apt-get install \
bison \
re2c \
locales \
openssl \
language-pack-de \
libsqlite3-dev \
libonig-dev \
libc-client-dev \
valgrind
- name: ccache
uses: hendrikmuhs/ccache-action@v1.2
with:
key: "${{github.job}}-${{hashFiles('main/php_version.h')}}"
append-timestamp: false
- name: ./configure
run: |
set -x
./buildconf --force
./configure \
--disable-debug \
--with-valgrind \
--enable-opcache \
--enable-option-checking=fatal \
--prefix=/usr \
--with-config-file-scan-dir=/etc/php.d \
--with-mysqli=mysqlnd \
--with-pdo-sqlite \
--enable-mbstring \
--enable-sockets \
--with-openssl \
--enable-werror
- name: make
run: make -j$(/usr/bin/nproc) >/dev/null
- name: make install
uses: ./.github/actions/install-linux
- name: Prepare SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.BENCHMARK_DATA_DEPLOY_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
git config --global user.name "Benchmark"
git config --global user.email "benchmark@php.net"
- name: Setup
run: |
sudo service mysql start
mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS wordpress"
mysql -uroot -proot -e "CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'wordpress'; FLUSH PRIVILEGES;"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON *.* TO 'wordpress'@'localhost' WITH GRANT OPTION;"
mkdir -p /etc/php.d
echo zend_extension=opcache.so >> /etc/php.d/opcache.ini
echo opcache.enable=1 >> /etc/php.d/opcache.ini
echo opcache.enable_cli=1 >> /etc/php.d/opcache.ini
- name: Benchmark
run: php benchmark/benchmark.php true
- name: Show diff
if: github.event_name == 'pull_request'
run: >-
php benchmark/generate_diff.php
${{ github.event.pull_request.head.sha }}
$(git merge-base ${{ github.base_ref }} ${{ github.head_ref }})
> $GITHUB_STEP_SUMMARY
1 change: 1 addition & 0 deletions benchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/repos
112 changes: 112 additions & 0 deletions benchmark/benchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

require_once __DIR__ . '/shared.php';

$commitResult = ($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 $commitResult;

$data = [];
$data['bench.php'] = runBench();
$data['symfony_demo'] = runSymfonyDemo();
$data['wordpress_6.2'] = runWordpress();
$result = json_encode($data, JSON_PRETTY_PRINT) . "\n";

if ($commitResult) {
commitResult($result);
} else {
fwrite(STDOUT, $result);
}
}

function commitResult(string $result) {
$repo = __DIR__ . '/repos/data';
cloneRepo($repo, 'git@github.com:iluuu1994/php-benchmark-data.git');
runCommand(['git', 'pull', '--end-of-options', 'origin'], $repo);

$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);

runCommand(['git', 'add', '--end-of-options', $summaryFile], $repo);
runCommand(['git', 'commit', '--allow-empty', '-m', 'Add result for php/php-src@' . $commitHash, '--author', 'Benchmark <benchmark@php.net>'], $repo);
runCommand(['git', 'push'], $repo);
}

function getPhpSrcCommitHash(): string {
$result = runCommand(['git', 'log', '--pretty=format:%H', '-n', '1'], dirname(__DIR__));
return $result->stdout;
}

function runBench(): array {
$process = runValgrindPhpCgiCommand([dirname(__DIR__) . '/Zend/bench.php']);
return ['instructions' => extractInstructionsFromValgrindOutput($process->stderr)];
}

function runSymfonyDemo(): array {
$dir = __DIR__ . '/repos/symfony-demo-2.2.3';
cloneRepo($dir, 'git@github.com:iluuu1994/symfony-demo-2.2.3.git');
runPhpCommand([$dir . '/bin/console', 'cache:clear']);
runPhpCommand([$dir . '/bin/console', 'cache:warmup']);
$process = runValgrindPhpCgiCommand(['-T1,1', $dir . '/public/index.php']);
return ['instructions' => extractInstructionsFromValgrindOutput($process->stderr)];
}

function runWordpress(): array {
$dir = __DIR__ . '/repos/wordpress-6.2';
cloneRepo($dir, 'git@github.com:iluuu1994/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);
$process = runValgrindPhpCgiCommand(['-T1,1', $dir . '/index.php'], $dir);
return ['instructions' => extractInstructionsFromValgrindOutput($process->stderr)];
}

function runPhpCommand(array $args, ?string $cwd = null): ProcessResult {
return runCommand([PHP_BINARY, ...$args], $cwd);
}

function runValgrindPhpCgiCommand(array $args, ?string $cwd = null): ProcessResult {
global $phpCgi;
return runCommand([
'valgrind',
'--tool=callgrind',
'--dump-instr=yes',
'--callgrind-out-file=/dev/null',
'--',
$phpCgi,
'-d max_execution_time=0',
...$args,
]);
}

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();
11 changes: 11 additions & 0 deletions benchmark/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: "3.8"
services:
wordpress_db:
image: mysql:8.0
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
63 changes: 63 additions & 0 deletions benchmark/generate_diff.php
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:iluuu1994/php-benchmark-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);
72 changes: 72 additions & 0 deletions benchmark/shared.php
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));
}