PHPackages                             jamesgober/benchmark - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. jamesgober/benchmark

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

jamesgober/benchmark
====================

A PHP benchmarking library for measuring code performance and memory usage with precision, extensibility, and advanced reporting features.

v1.0.0.0(1y ago)058↓100%Apache-2.0PHPPHP &gt;=8.2

Since Dec 6Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jamesgober/Benchmark)[ Packagist](https://packagist.org/packages/jamesgober/benchmark)[ Docs](https://github.com/jamesgober/Benchmark)[ RSS](/packages/jamesgober-benchmark/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (4)Used By (0)

   ![Official brand mark and logo of James Gober. Image shows JG stylish initials encased in a hexagon outline.](./docs/media/jamesgober-logo.png)
 **BENCHMARK**
  PHP PERFORMANCE INSIGHTS

==================================================================================================================================================================================================

[](#--------------------------------benchmark------------------------php-performance-insights----------------)

 [![GitHub Stars](https://camo.githubusercontent.com/230280397d1bb90d1d9e6c0e7cd5e616b2550719dc6c3c98fe24dd4c0b7ed303/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c6174)](https://camo.githubusercontent.com/230280397d1bb90d1d9e6c0e7cd5e616b2550719dc6c3c98fe24dd4c0b7ed303/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c6174) [![GitHub Issues](https://camo.githubusercontent.com/bd613083bb27fbf989c8f3c2ef06c89a8ef421fa172b43d30771ccb48333793c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c6174)](https://camo.githubusercontent.com/bd613083bb27fbf989c8f3c2ef06c89a8ef421fa172b43d30771ccb48333793c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c6174) [![GitHub Release](https://camo.githubusercontent.com/096e42a57f68a5d239d55671db90ec51cffff415c4506ff0f4887d81fa59ba67/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6a616d6573676f6265722f42656e63686d61726b3f646973706c61795f6e616d653d746167267374796c653d666c6174)](https://camo.githubusercontent.com/096e42a57f68a5d239d55671db90ec51cffff415c4506ff0f4887d81fa59ba67/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6a616d6573676f6265722f42656e63686d61726b3f646973706c61795f6e616d653d746167267374796c653d666c6174) [![GitHub License](https://camo.githubusercontent.com/87f795687b8467112916bda4b3f77279b893b8a7b4098ca38ef187bf4a9be069/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c6174)](https://camo.githubusercontent.com/87f795687b8467112916bda4b3f77279b893b8a7b4098ca38ef187bf4a9be069/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c6174) [![PHP Version](https://camo.githubusercontent.com/b9d936ad3cc55dae0242b76220d637004127271d9859f9ce6ed5ab231229aac9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322d626c75653f7374796c653d666c6174)](https://camo.githubusercontent.com/b9d936ad3cc55dae0242b76220d637004127271d9859f9ce6ed5ab231229aac9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322d626c75653f7374796c653d666c6174) [ ![Packagist Downloads](https://camo.githubusercontent.com/840faf0ca656a55ddb6caddf834cda69c0bf37ecf01e75a8e7ad177a9a153e83/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a616d6573676f6265722f42656e63686d61726b3f7374796c653d666c617426636f6c6f723d253233663236663161) ](https://packagist.org/packages/jamesgober/Benchmark)

Benchmark is a high-performance PHP library for measuring execution time and memory usage of code. Designed with precision, fault tolerance, and extensibility in mind, it provides advanced features like middleware hooks, configurable options, and detailed reporting to help developers optimize their applications.

Key Features
------------

[](#key-features)

- **High-Resolution Timing**: Nanosecond-level precision using `hrtime`.
- **Memory Tracking**: Track memory usage and peak memory consumption.
- **Middleware Support**: Pre- and post-processing hooks for advanced customization.
- **Grouped Benchmarking**: Run and analyze multiple benchmarks simultaneously.
- **Statistical Analysis**: Generate averages, variances, and detailed reports over multiple iterations.
- **Configurable Options**: Toggle features like verbose output and peak memory tracking.
- **Integration Ready**: Export reports in JSON, array, or human-readable formats.
- **Lightweight and Efficient**: Minimal overhead to keep performance at its peak.

Installation
------------

[](#installation)

Install via [composer](https://getcomposer.org/download/):

```
composer "require jamesgober/benchmark"
```

Usage
-----

[](#usage)

### Basic Benchmark

[](#basic-benchmark)

```
use JG\Benchmark\Benchmark;

$benchmark = new Benchmark();

$benchmark->start('example');
// Code to benchmark
usleep(1000); // Simulate a short delay
$benchmark->stop('example');

// Get results
echo $benchmark->getTime('example'); // Outputs elapsed time
echo $benchmark->getMemory('example'); // Outputs memory usage
```

### Grouped Benchmark Report

[](#grouped-benchmark-report)

```
$benchmark->start('task1');
usleep(2000);
$benchmark->stop('task1');

$benchmark->start('task2');
usleep(1000);
$benchmark->stop('task2');

print_r($benchmark->getReport());
```

#### Example Output

[](#example-output)

```
Array
(
    [task1] => Array
        (
            [time] => 0.002 ms
            [memory] => 1.23 KB
            [peak_memory] => 1.45 KB
        )

    [task2] => Array
        (
            [time] => 0.001 ms
            [memory] => 1.12 KB
            [peak_memory] => 1.32 KB
        )
)

```

### Middleware Example

[](#middleware-example)

```
$benchmark->addMiddleware('before', function ($name, $data) {
    echo "Starting benchmark: $name\n";
});

$benchmark->addMiddleware('after', function ($name, $data) {
    echo "Finished benchmark: $name\n";
});

$benchmark->start('middleware_example');
usleep(1500);
$benchmark->stop('middleware_example');
```

---

Configuration
-------------

[](#configuration)

Customize behavior by passing an array of options to the constructor:

```
$config = [
    'track_peak_memory' => true,
    'verbose' => false,
];

$benchmark = new Benchmark($config);
```

OptionDefaultDescription`track_peak_memory``true`Tracks peak memory usage if true.`verbose``false`Enables verbose output if true.---

Testing
-------

[](#testing)

Run PHPUnit tests:

```
composer test
```

Run static analysis with PHPStan:

```
composer phpstan
```

Run both tests and static analysis:

```
composer check
```

---

Reporting Bugs and Feature Requests
-----------------------------------

[](#reporting-bugs-and-feature-requests)

For bugs, feature requests, or security issues, please visit our **[Issue Tracker](https://github.com/jamesgober/Benchmark/issues)**.

If you discover a vulnerability, refer to our [Security Policy](.github/SECURITY.md).

Contributing
------------

[](#contributing)

Contributions are welcome! Please follow our [Contribution Guidelines](.github/CONTRIBUTING.md):

1. Fork the repository.
2. Create a feature branch.
3. Commit your changes with descriptive messages.
4. Open a pull request.

Ensure all tests pass and adhere to the project's coding standards.

License
-------

[](#license)

This library is licensed under the [Apache-2.0](LICENSE) License.

---

  COPYRIGHT © 2025 **JAMES GOBER.**

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance43

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

523d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e09f360f8998d68822aca7bb668a16c70cd4f250e246e30252d2246d1449ac3b?d=identicon)[jamesgober](/maintainers/jamesgober)

---

Top Contributors

[![jamesgober](https://avatars.githubusercontent.com/u/12473969?v=4)](https://github.com/jamesgober "jamesgober (9 commits)")

---

Tags

benchmarkbenchmarkingexecution-timememory-usagephpphp-benchmarkprofilerphpperformanceprofilingtimerbenchmarkexecution timememory\_usagejamesgoberbenchmarking-library

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jamesgober-benchmark/health.svg)

```
[![Health](https://phpackages.com/badges/jamesgober-benchmark/health.svg)](https://phpackages.com/packages/jamesgober-benchmark)
```

###  Alternatives

[laracraft-tech/laravel-xhprof

Easy XHProf setup to profile your laravel application!

235321.4k](/packages/laracraft-tech-laravel-xhprof)[bavix/laravel-xhprof

Quick profiling of your code for Laravel

22156.6k](/packages/bavix-laravel-xhprof)[koriym/xdebug-mcp

Universal PHP Xdebug MCP Server with AI-optimized debugging support

4011.6k1](/packages/koriym-xdebug-mcp)[wikimedia/arc-lamp

Flame graphs and log processing for PHP stack traces.

434.4k](/packages/wikimedia-arc-lamp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
