Skip to content

Commit 142e907

Browse files
committed
Add support for setting test output expectations
This way, it becomes possible to fail the benchmark if a PHP version gave wrong results.
1 parent 5080061 commit 142e907

9 files changed

+983
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ vendor/
66
!/build/infrastructure/config/aws.tfvars.dist
77
/config/**/*.*
88
!/config/**/*.dist
9+
!/config/**/*.expectation
910
/tmp/
1011
!.gitkeep
1112

app/zend/assert.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
error_reporting(E_ALL);
6+
ini_set("display_errors", true);
7+
8+
function assertEquals(string $expected, string $actual): void
9+
{
10+
$expectedArray = explode("\n", $expected);
11+
$expectedArrayCount = count($expectedArray);
12+
$actualArray = explode("\n", $actual);
13+
$actualArrayCount = count($actualArray);
14+
if ($expectedArrayCount !== $actualArrayCount) {
15+
echo "Expected and actual output does not have the same number of lines ($expectedArrayCount vs $actualArrayCount).\n";
16+
echo "Output: \n$actual\n";
17+
exit(1);
18+
}
19+
20+
foreach ($expectedArray as $line => $expectedLine) {
21+
$actualLine = trim($actualArray[$line]);
22+
$quotedExpectedLine = preg_quote(trim($expectedLine), "#");
23+
$quotedExpectedLine = str_replace(
24+
[
25+
"%s", // string pattern
26+
"%d", // integer pattern
27+
"%f", // float pattern
28+
"%v", // version pattern
29+
],
30+
[
31+
'.+',
32+
'\d+',
33+
'\d+\.\d+',
34+
'\d+\.\d+.\d+(?:-dev)*',
35+
],
36+
$quotedExpectedLine
37+
);
38+
39+
$result = preg_match("#^$quotedExpectedLine\$#", $actualLine);
40+
if ($result === false) {
41+
echo "Error in regular expression at line " . ($line + 1) . ": " . preg_last_error_msg() . "\n";
42+
echo "Pattern: $quotedExpectedLine\n";
43+
exit(1);
44+
}
45+
46+
if ($result === 0) {
47+
echo "Invalid output at line " . ($line + 1) . ": $actualLine\n";
48+
echo "Pattern: $quotedExpectedLine\n";
49+
exit(1);
50+
}
51+
}
52+
53+
echo "Output is valid\n";
54+
exit(0);
55+
}
56+
57+
$expectationFilename = $argv[1] ?? null;
58+
$actualFilename = $argv[2] ?? null;
59+
if ($expectationFilename === null || $actualFilename === null) {
60+
echo "Missing arguments\n";
61+
exit(1);
62+
}
63+
64+
$expected = file_get_contents($expectationFilename);
65+
if ($expected === false) {
66+
echo "Empty file or unsuccessful read: $expectationFilename";
67+
exit(1);
68+
}
69+
70+
$actual = file_get_contents($actualFilename);
71+
if ($actual === false) {
72+
echo "Empty file or unsuccessful read: $actualFilename";
73+
exit(1);
74+
}
75+
76+
assertEquals($expected, $actual);

bin/benchmark.sh

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,27 @@ run_cgi () {
259259
fi
260260
}
261261

262+
assert_test_output() {
263+
expectation_file="$1"
264+
actual_file="$2"
265+
266+
if [[ "$INFRA_RUNNER" == "host" ]]; then
267+
$php_source_path/sapi/cli/php "$PROJECT_ROOT/app/zend/assert.php" "$expectation_file" "$actual_file"
268+
else
269+
if [[ "$INFRA_ENVIRONMENT" == "local" ]]; then
270+
run_as=""
271+
repository="$INFRA_DOCKER_REPOSITORY"
272+
elif [[ "$INFRA_ENVIRONMENT" == "aws" ]]; then
273+
run_as="sudo"
274+
repository="$INFRA_DOCKER_REGISTRY/$INFRA_DOCKER_REPOSITORY"
275+
fi
276+
277+
$run_as docker run --rm --log-driver=local \
278+
--volume "$PROJECT_ROOT/app:/code/app:delegated" \
279+
"$repository:$PHP_ID-latest" php /code/app/zend/assert.php "$expectation_file" "$actual_file"
280+
fi
281+
}
282+
262283
format_instruction_count_log_file() {
263284
result="$(grep "== Collected : " "$1")"
264285
echo "$result" > "$1"
@@ -275,7 +296,11 @@ format_memory_log_file() {
275296

276297
run_real_benchmark () {
277298
# Benchmark
278-
run_cgi "verbose" "0" "1" "$1" "$2" "$3"
299+
run_cgi "verbose" "0" "1" "$1" "$2" "$3" | tee -a "$output_file"
300+
if [ ! -z "$test_expectation_file" ]; then
301+
assert_test_output "$test_expectation_file" "$output_file"
302+
fi
303+
279304
if [ "$INFRA_INSTRUCTION_COUNT" == "1" ]; then
280305
run_cgi "instruction_count" "10" "10" "$1" "$2" "$3" 2>&1 | tee -a "$instruction_count_log_file"
281306
fi
@@ -318,7 +343,6 @@ run_micro_benchmark () {
318343
}
319344

320345
run_test () {
321-
322346
case "$TEST_ID" in
323347

324348
laravel_11_1_2)
@@ -353,7 +377,10 @@ run_test () {
353377
}
354378

355379
run_benchmark () {
356-
380+
test_expectation_file="${test_config//.ini/.expectation}"
381+
if [ ! -f "$test_expectation_file" ]; then
382+
test_expectation_file=""
383+
fi
357384
result_dir="$infra_dir/${TEST_NUMBER}_${TEST_ID}"
358385
result_file="$result_dir/result"
359386

@@ -374,6 +401,7 @@ run_benchmark () {
374401

375402
log_dir="$result_dir"
376403
log_file="$log_dir/${PHP_ID}.log"
404+
output_file="$log_dir/${PHP_ID}_output.txt"
377405
instruction_count_log_file="$log_dir/${PHP_ID}.instruction_count.log"
378406
memory_log_file="$log_dir/${PHP_ID}.memory.log"
379407
mkdir -p "$log_dir"

config/test/1_laravel.expectation

Lines changed: 152 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)