initphp/performance-meter 问题修复 & 功能扩展

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

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

initphp/performance-meter

Composer 安装命令:

composer require initphp/performance-meter

包简介

Zero-dependency, single-file PHP profiler for measuring elapsed time and memory usage between named checkpoints.

README 文档

README

A zero-dependency, single-class PHP profiler for measuring elapsed time and memory usage between named checkpoints.

CI Latest Stable Version Total Downloads License PHP Version Require

Positioning

PerformanceMeter is intentionally minimal — a single final class with a handful of static methods that works without any other dependency. It exists to fill a specific niche:

Quick, single-file timing checks where pulling in a full profiling library would be overkill.

It is not a replacement for full-featured profilers; it is the cheapest possible thing that lets you answer "how long did this block take and how much memory did it use?".

When to use this

  • One-off benchmarking scripts and microbenchmarks
  • CLI tools and cron jobs where you want a quick elapsed-time print at the end
  • Tutorial / educational code where introducing a heavier dependency would obscure the lesson
  • Library examples and reproduction scripts in bug reports
  • Hot-path probes during local development, when adding a composer require round-trip is friction

When NOT to use this

Need Use instead
Application-level profiling with nested sections, periods, categories symfony/stopwatch
Production profiling, flame graphs, call tree analysis Xdebug profiler, Blackfire, Tideways, SPX
Web request profiling with timeline UI Symfony's WebProfilerBundle, Laravel Telescope, Clockwork
Memory leak hunting with object retention graphs Xdebug + xdebug_debug_zval, or memprof
OpenTelemetry / APM integration open-telemetry/sdk and an APM vendor SDK

If you are reaching for any of the above, this package is not the right tool — and that is by design.

Requirements

  • PHP 8.1 or higher
  • No runtime dependencies

Installation

composer require initphp/performance-meter

You can also include src/PerformanceMeter.php (and src/Exception/PointerNotFoundException.php) manually if you cannot use Composer — the package has no transitive dependencies.

Quick start

require_once 'vendor/autoload.php';

use InitPHP\PerformanceMeter\PerformanceMeter;

PerformanceMeter::setPointer('main');

for ($i = 0; $i <= 1000; $i++) {
    usleep(10);
}

PerformanceMeter::setPointer('mainEnd');

echo PerformanceMeter::elapsedTime('main', 'mainEnd', 3) . ' seconds elapsed' . PHP_EOL;
echo PerformanceMeter::memoryUsage('main', 'mainEnd', 2) . ' memory used' . PHP_EOL;

// Example output:
// 0.015 seconds elapsed
// 0.77KB memory used

Open-ended measurement

When you only pass a starting checkpoint, the second argument defaults to "now":

PerformanceMeter::setPointer('boot');

// ... do work ...

echo PerformanceMeter::elapsedTime('boot') . ' seconds since boot' . PHP_EOL;

mark() alias

mark($name) is a one-to-one alias of setPointer($name) for readers who prefer stopwatch-style vocabulary:

PerformanceMeter::mark('before');
heavy_work();
PerformanceMeter::mark('after');

echo PerformanceMeter::elapsedTime('before', 'after');

More usage patterns — peak memory, comparing two implementations, resetting between runs — live in docs/cookbook.md.

API at a glance

Method Purpose
setPointer(string $name): void Record a checkpoint with the current time + memory. Case-insensitive.
mark(string $name): void Alias of setPointer().
elapsedTime(string $start, ?string $end = null, int $decimal = 4): float Seconds between two checkpoints. $end = null ⇒ "now".
memoryUsage(string $start, ?string $end = null, int $decimal = 2, bool $realUsage = false): string Memory delta, formatted as "x.xxKB" or "x.xxMB".
peakMemoryUsage(int $decimal = 2, bool $realUsage = false): string Peak memory used so far by the process.
has(string $name): bool Whether a checkpoint with that name has been recorded.
getPointers(): array Snapshot copy of every recorded checkpoint.
reset(): void Clear all checkpoints.

elapsedTime() and memoryUsage() throw InitPHP\PerformanceMeter\Exception\PointerNotFoundException when $start (or a non-null $end) does not match a recorded checkpoint.

Full reference with parameter notes, error conditions and runnable examples: docs/api-reference.md.

Documentation

Migrating from v1.x to v2.0

v2.0 is a clean break that fixes real bugs and tightens the API. Most callers only need to upgrade PHP.

Area v1 behaviour v2 behaviour Action
PHP requirement >=7.4 ^8.1 Upgrade your runtime.
Missing $startPoint Silently returned ~0 ("now" – "now") Throws PointerNotFoundException Wrap in try/catch or call PerformanceMeter::has() first.
Missing non-null $endPoint Silently fell back to "now" Throws PointerNotFoundException Same — fix the typo or check with has().
memoryUsage() with a freed-memory delta ≥ 1 MB Reported in KB (broken) Reports correctly in MB with sign No code change; output now matches expectations.
decimal < 0 Accepted, produced odd output Throws InvalidArgumentException Pass decimal >= 0.
Subclassing PerformanceMeter Allowed (pointless — all-static) Blocked (final) Compose, do not inherit.
protected static $pointers Visible to subclasses private Use getPointers() / has() / reset().
New: reset(), has(), peakMemoryUsage(), getPointers() Added Opt-in.

A migration cookbook entry with side-by-side diffs lives in docs/cookbook.md.

Contributing

This package follows the org-wide InitPHP contribution guide — PSR-12, declare(strict_types=1);, PHPStan at the configured level, PHPUnit-tested behaviour changes, Conventional Commits.

Locally:

composer install
composer test         # PHPUnit
composer phpstan      # static analysis
composer cs-check     # coding standards (use cs-fix to apply)
composer qa           # all of the above

CI runs on PHP 8.1 → 8.4 against both highest and lowest installable dependencies.

Security

Please report security issues privately — see the org-wide SECURITY.md. Do not open public issues for vulnerabilities.

Credits

License

Released under the MIT License. Copyright © 2022-2026 InitPHP.

initphp/performance-meter 适用场景与选型建议

initphp/performance-meter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 48 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「performance」 「profiler」 「memory」 「benchmark」 「timing」 「stopwatch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 initphp/performance-meter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 initphp/performance-meter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-15