https://github.com/jamesgober/benchmark
PHP Benchmark library designed to measure code performance and memory usage with precision and efficiency.
https://github.com/jamesgober/benchmark
benchmark benchmarking execution-time memory-usage php php-benchmark profiler
Last synced: 10 months ago
JSON representation
PHP Benchmark library designed to measure code performance and memory usage with precision and efficiency.
- Host: GitHub
- URL: https://github.com/jamesgober/benchmark
- Owner: jamesgober
- License: apache-2.0
- Created: 2025-01-26T23:57:31.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-18T18:20:53.000Z (11 months ago)
- Last Synced: 2025-04-10T22:18:27.896Z (10 months ago)
- Topics: benchmark, benchmarking, execution-time, memory-usage, php, php-benchmark, profiler
- Language: PHP
- Homepage:
- Size: 57.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
BENCHMARK
PHP PERFORMANCE INSIGHTS
Benchmark is a high-performance PHP library for measuring execution time and memory usage of code. Designed with precision, fault tolerance, and extensibility in mind, it provides advanced features like middleware hooks, configurable options, and detailed reporting to help developers optimize their applications.
## Key Features
- **High-Resolution Timing**: Nanosecond-level precision using `hrtime`.
- **Memory Tracking**: Track memory usage and peak memory consumption.
- **Middleware Support**: Pre- and post-processing hooks for advanced customization.
- **Grouped Benchmarking**: Run and analyze multiple benchmarks simultaneously.
- **Statistical Analysis**: Generate averages, variances, and detailed reports over multiple iterations.
- **Configurable Options**: Toggle features like verbose output and peak memory tracking.
- **Integration Ready**: Export reports in JSON, array, or human-readable formats.
- **Lightweight and Efficient**: Minimal overhead to keep performance at its peak.
## Installation
Install via [composer](https://getcomposer.org/download/):
```sh
composer "require jamesgober/benchmark"
```
## Usage
### Basic Benchmark
```php
use JG\Benchmark\Benchmark;
$benchmark = new Benchmark();
$benchmark->start('example');
// Code to benchmark
usleep(1000); // Simulate a short delay
$benchmark->stop('example');
// Get results
echo $benchmark->getTime('example'); // Outputs elapsed time
echo $benchmark->getMemory('example'); // Outputs memory usage
```
### Grouped Benchmark Report
```php
$benchmark->start('task1');
usleep(2000);
$benchmark->stop('task1');
$benchmark->start('task2');
usleep(1000);
$benchmark->stop('task2');
print_r($benchmark->getReport());
```
#### Example Output
```
Array
(
[task1] => Array
(
[time] => 0.002 ms
[memory] => 1.23 KB
[peak_memory] => 1.45 KB
)
[task2] => Array
(
[time] => 0.001 ms
[memory] => 1.12 KB
[peak_memory] => 1.32 KB
)
)
```
### Middleware Example
```php
$benchmark->addMiddleware('before', function ($name, $data) {
echo "Starting benchmark: $name\n";
});
$benchmark->addMiddleware('after', function ($name, $data) {
echo "Finished benchmark: $name\n";
});
$benchmark->start('middleware_example');
usleep(1500);
$benchmark->stop('middleware_example');
```
---
## Configuration
Customize behavior by passing an array of options to the constructor:
```php
$config = [
'track_peak_memory' => true,
'verbose' => false,
];
$benchmark = new Benchmark($config);
```
| Option | Default | Description |
|---------------------|---------|------------------------------------|
| `track_peak_memory` | `true` | Tracks peak memory usage if true. |
| `verbose` | `false` | Enables verbose output if true. |
---
## Testing
Run PHPUnit tests:
```bash
composer test
```
Run static analysis with PHPStan:
```bash
composer phpstan
```
Run both tests and static analysis:
```bash
composer check
```
---
## Reporting Bugs and Feature Requests
For bugs, feature requests, or security issues, please visit our **[Issue Tracker](https://github.com/jamesgober/Benchmark/issues)**.
If you discover a vulnerability, refer to our [Security Policy](.github/SECURITY.md).
## Contributing
Contributions are welcome! Please follow our [Contribution Guidelines](.github/CONTRIBUTING.md):
1. Fork the repository.
2. Create a feature branch.
3. Commit your changes with descriptive messages.
4. Open a pull request.
Ensure all tests pass and adhere to the project's coding standards.
## License
This library is licensed under the [Apache-2.0](LICENSE) License.
---
COPYRIGHT © 2025 JAMES GOBER.
[Contribution Guidelines]: .github/CONTRIBUTING.md
[CONTRIBUTING]: .github/CONTRIBUTING.md
[CODE OF CONDUCT]: .github/CODE_OF_CONDUCT.md
[REPORT SECURITY ISSUES]: .github/SECURITY.md
[SECURITY POLICY]: .github/SECURITY.md
[SECURITY]: .github/SECURITY.md