Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ubench Laravel Package

devster/ubench

Ultra small PHP benchmarking library with a simple API to time code, measure memory usage, run iterations, and print readable results. Great for quick micro-benchmarks in scripts, tests, and CLI tools without dependencies.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require --dev devster/ubench. Create a Benchmark instance at the top of your test script or inline in code you want to profile. Use start() before the block and stop() after — the class stores the interval and memory delta automatically. For the first real use, benchmark a trivial operation (e.g., string concatenation vs. implode()) and call getReport() to see formatted output. Check the examples/ folder in the repo (if available) or inspect Benchmark::start(), Benchmark::stop(), and Benchmark::getReport() signatures in the source.

Implementation Patterns

  • Inline development benchmarks: Wrap suspected hot paths with start/stop blocks to compare refactorings or algorithms during development.
  • Regression guard: Add a simple Benchmark in your test suite’s development/test runner to assert performance thresholds (e.g., ensure a utility function never exceeds 2ms for typical inputs).
  • Comparison matrix: Run multiple variants of a function in a loop, collecting reports into an array, then output a table manually or via implode() for quick side-by-side analysis.
  • Logging + monitoring: Hook getReport() output into your dev-mode logging (e.g., info() with Monolog) to catch accidental slowdowns in local dev.
  • Test-driven performance: Use ->getElapsedMicroseconds() and ->getMemoryDelta() in assertions to prevent regressions (e.g., $this->assertLessThan(0.001, $bench->getElapsedSeconds())).

Gotchas and Tips

  • Warm-up matters: The first run of a function is often slower due to autoloading/JIT (if applicable). Run benchmarks multiple times and discard the first sample when comparing.
  • Memory delta can be negative: PHP’s memory management sometimes reclaims memory during execution — use getPeakMemoryUsage() instead of raw delta for stable measurements.
  • Micro-benchmarks ≠ real-world: This tool measures isolated code; avoid over-interpreting results without application-level profiling (e.g., Blackfire/Xdebug).
  • No built-in statistics: You must manage repetitions and averaging yourself (e.g., in a for loop). Consider wrapping Benchmark in a simple BenchmarkRunner utility.
  • No PHP 8.1+ deprecation warnings in tests: Given the last release (2019), test for strict type compatibility — some microtime(true) usages may need explicit float casting in newer PHP versions.
  • Reset is manual: If reusing a Benchmark instance, call reset() to clear previous runs — don’t rely on re-instantiation in tight loops.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope