serhiikorniushov/micro-bench 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

  1. Include Autoloader: require_once __DIR__ . '/vendor/autoload.php'; loads the Composer-generated autoloader, making the MicroBench class available.
  2. Create Instance: $bench = new MicroBench(); creates a new MicroBench object.
  3. Start Benchmark: $bench->start(); starts the timer and records initial memory usage. It throws a RuntimeException if called when a benchmark is already running or has finished.
  4. Code to Benchmark: This is the section where you place the code you want to test.
  5. Finish Benchmark: $bench->finish(); stops the timer and records final memory usage. It throws a RuntimeException if called before start() or if called a second time.
  6. Get Report: $report = $bench->getReport(); retrieves a detailed report as an associative array.
  7. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 serhiikorniushov/micro-bench 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 25
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-10