Benchmark
Bash and PHP scripts to benchmark HTTP requests and command line calls.
How long does the thing take, and how many of them a second. Two shell scripts and a console wrapper around them — curl and xargs do the work, so there is nothing to install beyond what is already on the machine.
Requirements
- PHP 8.1 or newer, for the console commands
bash,curl,awkandxargs— the scripts alone need no PHP at all
The console side is quillstack/cli. It used to be Symfony's — nine packages and 1.7 MB to read three arguments, in a stack whose only other outside dependencies are PSR interfaces.
Installation
composer require --dev quillstack/benchmarkUsage
HTTP requests
./vendor/bin/benchmark benchmark:http:get https://example.org 100 10100 requests, 10 concurrently
URL https://example.org
--------------------------------------------------------------------
Took 2.104000 s, 47.528517 requests per second, 0.196742 avg req timeA hundred requests, ten at a time.
Command line calls
The command has to print one number — the seconds it took — and nothing else:
<?php
$started = microtime(true);
// … the thing being measured …
echo number_format(microtime(true) - $started, 6), "\n";./vendor/bin/benchmark benchmark:console "php measured.php" 100 10100 calls, 10 concurrently
Command `php measured.php`
-------------------------------------------------------------------
Took 0.512000 s, 195.312500 calls per second, 0.001264 avg call timeWithout PHP
The scripts run on their own:
./http_get.sh https://example.org 100 10
./command_line.sh "php measured.php" 100 10An empty measurement is worse than none
The clock is read differently on every system, and reading it wrongly used to produce this:
Took s, requests per second, 0.067014 avg req timeA number nobody could see was missing. get_milliseconds now tries GNU date, then perl, then python3, and refuses outright where none of them can answer. A count which is not a positive whole number is refused too, rather than reaching a shell as part of a command.
Technical documentation
| File | What it is |
|---|---|
http_get.sh | asks a URL many times, some at once, and reports |
command_line.sh | runs a command many times and reports |
lib/common.sh | the clock, and the check that a count is one |
src/Benchmark.php | finds the scripts and runs them with everything escaped |
src/Commands/HttpGetBenchmarkCommand.php | benchmark:http:get |
src/Commands/ConsoleBenchmarkCommand.php | benchmark:console |
Both take the same three arguments: what to measure, how many times, and how many at once.
Everything reaching a shell goes through escapeshellarg(), so a URL holding a quote is a URL rather than a second command.
Static analysis
composer stanLicense
MIT. See LICENSE.