Command Benchmark
A benchmarking component for Hyperf commands, forked from christophrumpel/artisan-benchmark.
Installation
composer require friendsofhyperf/command-benchmarkIntroduction
The Command Benchmark component provides performance benchmarking capabilities for Hyperf commands. It automatically collects and displays performance metrics for command execution, including:
- Execution Time: The time required for the command to run
- Memory Usage: Memory consumed during command execution
- Database Query Count: Number of SQL queries executed during command execution
Usage
This component automatically adds the --enable-benchmark option to all Hyperf commands through AOP (Aspect-Oriented Programming).
Enabling Benchmarking
Simply add the --enable-benchmark option when running any command to enable benchmarking:
php bin/hyperf.php your:command --enable-benchmarkOutput Example
After command execution completes, benchmark results will be displayed at the end of the output:
⚡ TIME: 2.5s MEM: 15.23MB SQL: 42Metric explanations:
- TIME: Execution time (milliseconds, seconds, or minutes)
- MEM: Memory usage (MB)
- SQL: Number of SQL queries executed
How It Works
This component uses Hyperf's AOP functionality to intercept command construction and execution:
Construction Phase:
- Records start time and memory usage
- Registers database query event listeners
- Adds the
--enable-benchmarkoption to the command
Execution Phase:
- If the
--enable-benchmarkoption is enabled - Calculates execution time, memory usage, and query count
- Formats and displays benchmark results
- If the
Result Display:
- Displays metrics using colored output
- Automatically formats time (milliseconds, seconds, minutes)
- Shows results at the end of command output
Configuration
This component requires no additional configuration and is ready to use after installation. It automatically registers with the Hyperf container.
Technical Details
AOP Aspect
The component intercepts the following methods of the Hyperf\Command\Command class through the CommandAspect aspect class:
__construct: Initializes performance metric collectionexecute: Displays benchmark results after execution completes
Performance Metrics
- Execution Time: Measured using
microtime(true) - Memory Usage: Measured using
memory_get_usage() - Query Count: Counted by listening to
QueryExecutedevents
Time Formatting
Time is automatically formatted with appropriate units based on execution duration:
- Less than 1 second: Displayed in milliseconds (e.g.,
250ms) - 1 second to 60 seconds: Displayed in seconds (e.g.,
2.5s) - More than 60 seconds: Displayed in minutes and seconds (e.g.,
2m 30s)
Examples
Testing Data Import Command
php bin/hyperf.php import:users --enable-benchmarkOutput:
Importing users...
100 users imported successfully.
⚡ TIME: 5.23s MEM: 28.45MB SQL: 150Testing Cache Clear Command
php bin/hyperf.php cache:clear --enable-benchmarkOutput:
Cache cleared successfully.
⚡ TIME: 120ms MEM: 2.15MB SQL: 0Notes
- Benchmarking introduces slight performance overhead; recommended for development and debugging only
- SQL query statistics include all queries executed through the Hyperf database component
- Memory usage is a relative value, representing the increase in memory usage during command execution