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.
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 requireround-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
docs/getting-started.md— install, first measurement, conceptual modeldocs/api-reference.md— every public method, parameter by parameterdocs/cookbook.md— real-world recipes (CLI benchmarks, cron timing, A/B comparisons, peak memory tracking, v1 → v2 migration)
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 initphp/performance-meter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
g4 application profiler package
repository php library
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>.
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
Create link to static resources with cache-breaking segment based on md5 of the file
统计信息
- 总下载量: 48
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-15