serhiikorniushov/micro-bench
Composer 安装命令:
composer require serhiikorniushov/micro-bench
包简介
PHP Micro Benchmarking tool
关键字:
README 文档
README
MicroBench is a simple PHP tool for micro-benchmarking your code developed by Serhii Korniushov. It measures execution time, memory usage, and memory peak usage. It supports high-resolution time (hrtime) on PHP 7.3+.
Features
- Measures elapsed time (microtime and hrtime).
- Tracks memory usage and peak memory usage.
- Simple API:
start(),finish(),getReport(). - Easy to install with Composer.
- Provides human-readable time formatting.
Installation
Install MicroBench using Composer:
composer require --dev serhiikorniushov/micro-bench
This command will download the MicroBench library and its dependencies into your project's vendor directory.
Usage
Here's a basic example of how to use MicroBench:
<?php require_once __DIR__ . '/vendor/autoload.php'; // Include Composer's autoloader use SerhiiKorniushov\MicroBench\MicroBench; // Start the benchmark $benchmark = (new SerhiiKorniushov\MicroBench\MicroBench)->start(); // Code to benchmark for ($i = 0; $i < 100000; $i++) { // Some operation $a = $i * 2; } // Get elapsed microtime time in human-readable format $benchmark->finish()->getElapsedMicroTimeReadable(); // OR // Finish the benchmark $benchmark->finish(); // Get the report $report = $benchmark->getReport(); // Output the results echo "Elapsed time: " . $benchmark->getElapsedMicroTimeReadable() . "\n"; echo "Elapsed time (seconds): " . $report['elapsed']['microtime'] . " s\n"; echo "Memory usage: " . $report['elapsed']['memory_usage'] . " bytes\n"; echo "Memory peak usage: " . $report['elapsed']['memory_peak_usage'] . " bytes\n"; // Print the full report (optional) // print_r($report); ?>
Explanation:
- Include Autoloader:
require_once __DIR__ . '/vendor/autoload.php';loads the Composer-generated autoloader, making the MicroBench class available. - Create Instance:
$bench = new MicroBench();creates a new MicroBench object. - Start Benchmark:
$bench->start();starts the timer and records initial memory usage. It throws aRuntimeExceptionif called when a benchmark is already running or has finished. - Code to Benchmark: This is the section where you place the code you want to test.
- Finish Benchmark:
$bench->finish();stops the timer and records final memory usage. It throws aRuntimeExceptionif called beforestart()or if called a second time. - Get Report:
$report = $bench->getReport();retrieves a detailed report as an associative array. - Output Results: The example shows how to access specific values from the report, such as elapsed time and memory usage.
getElapsedMicroTimeReadable()provides a nicely formatted time string.
API Reference
start(): Starts the benchmark.finish(): Finishes the benchmark.getReport(): Returns a detailed report array.isStarted(): Checks if the benchmark has been started.isFinished(): Checks if the benchmark has been finished.getElapsedMicroTime(): Returns the elapsed time in seconds (float).getElapsedMicroTimeReadable(): Returns the elapsed time in a human-readable format (e.g., "100 ms", "1 s").formatMicroTimeDiff(float $timeDiff): (Static) Formats a time difference (in seconds) into a human-readable string.getStartMicroTime(): Returns the starting microtime.getStartMemoryUsage(): Returns the starting memory usage.getStartMemoryUsageReal(): Returns the starting real memory usage.getFinishMicroTime(): Returns the finishing microtime.getFinishMemoryUsage(): Returns the finishing memory usage.getFinishMemoryUsageReal(): Returns the finishing real memory usage.getStartMemoryPeakUsage(): Returns the starting memory peak usage.getStartMemoryPeakUsageReal(): Returns the starting real memory peak usage.getFinishMemoryPeakUsage(): Returns the finishing memory peak usage.getFinishMemoryPeakUsageReal(): Returns the finishing real memory peak usage.getStartHrTime(): Returns the starting hrtime (PHP 7.3+).getFinishHrTime(): Returns the finishing hrtime (PHP 7.3+).
serhiikorniushov/micro-bench 适用场景与选型建议
serhiikorniushov/micro-bench 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「library」 「micro」 「bench」 「benchmark」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 serhiikorniushov/micro-bench 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 serhiikorniushov/micro-bench 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 serhiikorniushov/micro-bench 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Anax htmlform module.
Micro Active Record library in PHP, support chain calls, events, and relations.
Onix Micro Framework
Inbox pattern process implementation for your Laravel Applications
PHP classes for database handling upon PDO
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-10