jamesgober/benchmark
Composer 安装命令:
composer require jamesgober/benchmark
包简介
A PHP benchmarking library for measuring code performance and memory usage with precision, extensibility, and advanced reporting features.
关键字:
README 文档
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:
composer "require jamesgober/benchmark"
Usage
Basic Benchmark
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
$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
$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:
$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:
composer test
Run static analysis with PHPStan:
composer phpstan
Run both tests and static analysis:
composer check
Reporting Bugs and Feature Requests
For bugs, feature requests, or security issues, please visit our Issue Tracker.
If you discover a vulnerability, refer to our Security Policy.
Contributing
Contributions are welcome! Please follow our Contribution Guidelines:
- Fork the repository.
- Create a feature branch.
- Commit your changes with descriptive messages.
- 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.
COPYRIGHT © 2025 JAMES GOBER.
jamesgober/benchmark 适用场景与选型建议
jamesgober/benchmark 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 121 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「performance」 「php」 「profiling」 「timer」 「benchmark」 「memory-usage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jamesgober/benchmark 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jamesgober/benchmark 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jamesgober/benchmark 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Quick profiling of your code for Laravel
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Create link to static resources with cache-breaking segment based on md5 of the file
Excimetry PHP package. Bridge between ext-eximer and open telemetry
统计信息
- 总下载量: 121
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 34
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2025-01-27