diff --git a/composer.json b/composer.json index 4be6524..96d45db 100644 --- a/composer.json +++ b/composer.json @@ -68,6 +68,15 @@ "test-server": "echo \"Running Test Server\" && php -S localhost:8000 -t tests/server/", "test-server-v2": "echo \"Running Test Server\" && php -S localhost:8000 -t tests/server-v2/", "test-coverage:win": "del clover.xml && phpunit --coverage-html=coverage --coverage-clover=clover.xml && coverage-check clover.xml 100", + "test-performance": [ + "echo \"Running Performance Tests...\"", + "php -S localhost:8077 -t tests/performance/ > /dev/null 2>&1 & echo $! > server.pid", + "sleep 2", + "bash tests/performance/performance_tests.sh", + "kill `cat server.pid`", + "rm server.pid", + "echo \"Performance Tests Completed.\"" + ], "lint": "phpstan --no-progress --memory-limit=256M -cphpstan.neon", "beautify": "phpcbf --standard=phpcs.xml", "phpcs": "phpcs --standard=phpcs.xml -n", diff --git a/tests/performance/index.php b/tests/performance/index.php index 2d4d7cb..a42ddf8 100644 --- a/tests/performance/index.php +++ b/tests/performance/index.php @@ -2,7 +2,7 @@ declare(strict_types=1); -require __DIR__ . '/vendor/autoload.php'; +require __DIR__ . '/../../vendor/autoload.php'; // Route to list all available test routes Flight::route('GET /', function () { diff --git a/tests/performance/performance_tests.sh b/tests/performance/performance_tests.sh index 12978f5..605e242 100644 --- a/tests/performance/performance_tests.sh +++ b/tests/performance/performance_tests.sh @@ -1,7 +1,7 @@ #!/bin/bash # Allow URL to be set via environment variable or first command-line argument, default to localhost for safety -URL="${URL:-${1:-http://localhost:8080/test-static}}" +URL="${URL:-${1:-http://localhost:8077/test-static}}" REQUESTS=1000 CONCURRENCY=10 ITERATIONS=10 @@ -24,15 +24,20 @@ echo "----------------------------------------" for i in $(seq 1 $ITERATIONS); do printf "Run %2d/%d: " $i $ITERATIONS - + # Run ab and extract time per request result=$(ab -n $REQUESTS -c $CONCURRENCY $URL 2>/dev/null) time_per_request=$(echo "$result" | grep "Time per request:" | head -1 | awk '{print $4}') requests_per_sec=$(echo "$result" | grep "Requests per second:" | awk '{print $4}') - + + if [[ -z "$time_per_request" || ! "$time_per_request" =~ ^[0-9.]+$ ]]; then + echo "Warning: Could not parse time per request (ab output may be malformed)" + continue + fi + times+=($time_per_request) total=$(echo "$total + $time_per_request" | bc -l) - + printf "%.3f ms (%.2f req/s)\n" $time_per_request $requests_per_sec done