├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src ├── Duration.php ├── ResourceUsageFormatter.php ├── Timer.php └── exceptions ├── Exception.php ├── NoActiveTimerException.php └── TimeSinceStartOfRequestNotAvailableException.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # ChangeLog 2 | 3 | All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. 4 | 5 | ## [8.0.0] - 2025-02-07 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.1 10 | 11 | ## [7.0.1] - 2024-07-03 12 | 13 | ### Changed 14 | 15 | * This project now uses PHPStan instead of Psalm for static analysis 16 | 17 | ## [7.0.0] - 2024-02-02 18 | 19 | ### Removed 20 | 21 | * This component is no longer supported on PHP 8.1 22 | 23 | ## [6.0.0] - 2023-02-03 24 | 25 | ### Removed 26 | 27 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 28 | 29 | ## [5.0.3] - 2020-10-26 30 | 31 | ### Fixed 32 | 33 | * `SebastianBergmann\Timer\Exception` now correctly extends `\Throwable` 34 | 35 | ## [5.0.2] - 2020-09-28 36 | 37 | ### Changed 38 | 39 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 40 | 41 | ## [5.0.1] - 2020-06-26 42 | 43 | ### Added 44 | 45 | * This component is now supported on PHP 8 46 | 47 | ## [5.0.0] - 2020-06-07 48 | 49 | ### Changed 50 | 51 | * Parameter type for `SebastianBergmann\Timer\Duration::fromMicroseconds()` was changed from `int` to `float` 52 | * Parameter type for `SebastianBergmann\Timer\Duration::fromNanoseconds()` was changed from `int` to `float` 53 | * Return type for `SebastianBergmann\Timer\Duration::asNanoseconds()` was changed from `int` to `float` 54 | 55 | ### Fixed 56 | 57 | * [#31](https://github.com/sebastianbergmann/php-timer/issues/31): Type Error on 32-bit systems (where `hrtime()` returns `float` instead of `int`) 58 | 59 | ## [4.0.0] - 2020-06-01 60 | 61 | ### Added 62 | 63 | * Introduced `Duration` value object for encapsulating a duration with nanosecond granularity 64 | * Introduced `ResourceUsageFormatter` object for formatting resource usage with option to explicitly pass a duration (instead of looking at the unreliable `$_SERVER['REQUEST_TIME_FLOAT']` variable) 65 | 66 | ### Changed 67 | 68 | * The methods of `Timer` are no longer static 69 | * `Timer::stop()` now returns a `Duration` value object 70 | 71 | ### Removed 72 | 73 | * Functionality that is now implemented in `Duration` and `ResourceUsageFormatter` has been removed from `Timer` 74 | 75 | ## [3.1.4] - 2020-04-20 76 | 77 | ### Changed 78 | 79 | * `Timer::timeSinceStartOfRequest()` no longer tries `$_SERVER['REQUEST_TIME']` when `$_SERVER['REQUEST_TIME_FLOAT']` is not available (`$_SERVER['REQUEST_TIME_FLOAT']` was added in PHP 5.4 and this library requires PHP 7.3) 80 | * Improved exception messages when `$_SERVER['REQUEST_TIME_FLOAT']` is not set or is not of type `float` 81 | 82 | ### Changed 83 | 84 | ## [3.1.3] - 2020-04-20 85 | 86 | ### Changed 87 | 88 | * `Timer::timeSinceStartOfRequest()` now raises an exception if `$_SERVER['REQUEST_TIME_FLOAT']` does not contain a `float` (or `$_SERVER['REQUEST_TIME']` does not contain an `int`) 89 | 90 | ## [3.1.2] - 2020-04-17 91 | 92 | ### Changed 93 | 94 | * Improved the fix for [#30](https://github.com/sebastianbergmann/php-timer/issues/30) and restored usage of `hrtime()` 95 | 96 | ## [3.1.1] - 2020-04-17 97 | 98 | ### Fixed 99 | 100 | * [#30](https://github.com/sebastianbergmann/php-timer/issues/30): Resolution of time returned by `Timer::stop()` is different than before (this reverts using `hrtime()` instead of `microtime()`) 101 | 102 | ## [3.1.0] - 2020-04-17 103 | 104 | ### Added 105 | 106 | * `Timer::secondsToShortTimeString()` as alternative to `Timer::secondsToTimeString()` 107 | 108 | ### Changed 109 | 110 | * `Timer::start()` and `Timer::stop()` now use `hrtime()` (high resolution monotonic timer) instead of `microtime()` 111 | * `Timer::timeSinceStartOfRequest()` now uses `Timer::secondsToShortTimeString()` for time formatting 112 | * Improved formatting of `Timer::secondsToTimeString()` result 113 | 114 | ## [3.0.0] - 2020-02-07 115 | 116 | ### Removed 117 | 118 | * This component is no longer supported on PHP 7.1 and PHP 7.2 119 | 120 | ## [2.1.2] - 2019-06-07 121 | 122 | ### Fixed 123 | 124 | * [#21](https://github.com/sebastianbergmann/php-timer/pull/21): Formatting of memory consumption does not work on 32bit systems 125 | 126 | ## [2.1.1] - 2019-02-20 127 | 128 | ### Changed 129 | 130 | * Improved formatting of memory consumption for `resourceUsage()` 131 | 132 | ## [2.1.0] - 2019-02-20 133 | 134 | ### Changed 135 | 136 | * Improved formatting of memory consumption for `resourceUsage()` 137 | 138 | ## [2.0.0] - 2018-02-01 139 | 140 | ### Changed 141 | 142 | * This component now uses namespaces 143 | 144 | ### Removed 145 | 146 | * This component is no longer supported on PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, and PHP 7.0 147 | 148 | [8.0.0]: https://github.com/sebastianbergmann/php-timer/compare/7.0...8.0.0 149 | [7.0.1]: https://github.com/sebastianbergmann/php-timer/compare/7.0.0...7.0.1 150 | [7.0.0]: https://github.com/sebastianbergmann/php-timer/compare/6.0...7.0.0 151 | [6.0.0]: https://github.com/sebastianbergmann/php-timer/compare/5.0.3...6.0.0 152 | [5.0.3]: https://github.com/sebastianbergmann/php-timer/compare/5.0.2...5.0.3 153 | [5.0.2]: https://github.com/sebastianbergmann/php-timer/compare/5.0.1...5.0.2 154 | [5.0.1]: https://github.com/sebastianbergmann/php-timer/compare/5.0.0...5.0.1 155 | [5.0.0]: https://github.com/sebastianbergmann/php-timer/compare/4.0.0...5.0.0 156 | [4.0.0]: https://github.com/sebastianbergmann/php-timer/compare/3.1.4...4.0.0 157 | [3.1.4]: https://github.com/sebastianbergmann/php-timer/compare/3.1.3...3.1.4 158 | [3.1.3]: https://github.com/sebastianbergmann/php-timer/compare/3.1.2...3.1.3 159 | [3.1.2]: https://github.com/sebastianbergmann/php-timer/compare/3.1.1...3.1.2 160 | [3.1.1]: https://github.com/sebastianbergmann/php-timer/compare/3.1.0...3.1.1 161 | [3.1.0]: https://github.com/sebastianbergmann/php-timer/compare/3.0.0...3.1.0 162 | [3.0.0]: https://github.com/sebastianbergmann/php-timer/compare/2.1.2...3.0.0 163 | [2.1.2]: https://github.com/sebastianbergmann/php-timer/compare/2.1.1...2.1.2 164 | [2.1.1]: https://github.com/sebastianbergmann/php-timer/compare/2.1.0...2.1.1 165 | [2.1.0]: https://github.com/sebastianbergmann/php-timer/compare/2.0.0...2.1.0 166 | [2.0.0]: https://github.com/sebastianbergmann/php-timer/compare/1.0.9...2.0.0 167 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2010-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # phpunit/php-timer 2 | 3 | [![Latest Stable Version](https://poser.pugx.org/phpunit/php-timer/v)](https://packagist.org/packages/phpunit/php-timer) 4 | [![CI Status](https://github.com/sebastianbergmann/php-timer/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/php-timer/actions) 5 | [![codecov](https://codecov.io/gh/sebastianbergmann/php-timer/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/php-timer) 6 | 7 | Utility class for timing things, factored out of PHPUnit into a stand-alone component. 8 | 9 | ## Installation 10 | 11 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 12 | 13 | ``` 14 | composer require phpunit/php-timer 15 | ``` 16 | 17 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 18 | 19 | ``` 20 | composer require --dev phpunit/php-timer 21 | ``` 22 | 23 | ## Usage 24 | 25 | ### Basic Timing 26 | 27 | ```php 28 | require __DIR__ . '/vendor/autoload.php'; 29 | 30 | use SebastianBergmann\Timer\Timer; 31 | 32 | $timer = new Timer; 33 | 34 | $timer->start(); 35 | 36 | foreach (\range(0, 100000) as $i) { 37 | // ... 38 | } 39 | 40 | $duration = $timer->stop(); 41 | 42 | var_dump(get_class($duration)); 43 | var_dump($duration->asString()); 44 | var_dump($duration->asSeconds()); 45 | var_dump($duration->asMilliseconds()); 46 | var_dump($duration->asMicroseconds()); 47 | var_dump($duration->asNanoseconds()); 48 | ``` 49 | 50 | The code above yields the output below: 51 | 52 | ``` 53 | string(32) "SebastianBergmann\Timer\Duration" 54 | string(9) "00:00.002" 55 | float(0.002851062) 56 | float(2.851062) 57 | float(2851.062) 58 | int(2851062) 59 | ``` 60 | 61 | ### Resource Consumption 62 | 63 | #### Explicit duration 64 | 65 | ```php 66 | require __DIR__ . '/vendor/autoload.php'; 67 | 68 | use SebastianBergmann\Timer\ResourceUsageFormatter; 69 | use SebastianBergmann\Timer\Timer; 70 | 71 | $timer = new Timer; 72 | $timer->start(); 73 | 74 | foreach (\range(0, 100000) as $i) { 75 | // ... 76 | } 77 | 78 | print (new ResourceUsageFormatter)->resourceUsage($timer->stop()); 79 | ``` 80 | 81 | The code above yields the output below: 82 | 83 | ``` 84 | Time: 00:00.002, Memory: 6.00 MB 85 | ``` 86 | 87 | #### Duration since PHP Startup (using unreliable `$_SERVER['REQUEST_TIME_FLOAT']`) 88 | 89 | ```php 90 | require __DIR__ . '/vendor/autoload.php'; 91 | 92 | use SebastianBergmann\Timer\ResourceUsageFormatter; 93 | 94 | foreach (\range(0, 100000) as $i) { 95 | // ... 96 | } 97 | 98 | print (new ResourceUsageFormatter)->resourceUsageSinceStartOfRequest(); 99 | ``` 100 | 101 | The code above yields the output below: 102 | 103 | ``` 104 | Time: 00:00.002, Memory: 6.00 MB 105 | ``` 106 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phpunit/php-timer", 3 | "description": "Utility class for timing", 4 | "type": "library", 5 | "keywords": [ 6 | "timer" 7 | ], 8 | "homepage": "https://github.com/sebastianbergmann/php-timer/", 9 | "license": "BSD-3-Clause", 10 | "authors": [ 11 | { 12 | "name": "Sebastian Bergmann", 13 | "email": "sebastian@phpunit.de", 14 | "role": "lead" 15 | } 16 | ], 17 | "support": { 18 | "issues": "https://github.com/sebastianbergmann/php-timer/issues", 19 | "security": "https://github.com/sebastianbergmann/php-timer/security/policy" 20 | }, 21 | "prefer-stable": true, 22 | "require": { 23 | "php": ">=8.3" 24 | }, 25 | "require-dev": { 26 | "phpunit/phpunit": "^12.0" 27 | }, 28 | "config": { 29 | "platform": { 30 | "php": "8.3.0" 31 | }, 32 | "optimize-autoloader": true, 33 | "sort-packages": true 34 | }, 35 | "autoload": { 36 | "classmap": [ 37 | "src/" 38 | ] 39 | }, 40 | "extra": { 41 | "branch-alias": { 42 | "dev-main": "8.0-dev" 43 | } 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/Duration.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\Timer; 11 | 12 | use function floor; 13 | use function sprintf; 14 | 15 | /** 16 | * @immutable 17 | */ 18 | final readonly class Duration 19 | { 20 | private float $nanoseconds; 21 | private int $hours; 22 | private int $minutes; 23 | private int $seconds; 24 | private int $milliseconds; 25 | 26 | public static function fromMicroseconds(float $microseconds): self 27 | { 28 | return new self($microseconds * 1000); 29 | } 30 | 31 | public static function fromNanoseconds(float $nanoseconds): self 32 | { 33 | return new self($nanoseconds); 34 | } 35 | 36 | private function __construct(float $nanoseconds) 37 | { 38 | $this->nanoseconds = $nanoseconds; 39 | $timeInMilliseconds = $nanoseconds / 1000000; 40 | $hours = floor($timeInMilliseconds / 60 / 60 / 1000); 41 | $hoursInMilliseconds = $hours * 60 * 60 * 1000; 42 | $minutes = floor($timeInMilliseconds / 60 / 1000) % 60; 43 | $minutesInMilliseconds = $minutes * 60 * 1000; 44 | $seconds = floor(($timeInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds) / 1000); 45 | $secondsInMilliseconds = $seconds * 1000; 46 | $milliseconds = $timeInMilliseconds - $hoursInMilliseconds - $minutesInMilliseconds - $secondsInMilliseconds; 47 | $this->hours = (int) $hours; 48 | $this->minutes = $minutes; 49 | $this->seconds = (int) $seconds; 50 | $this->milliseconds = (int) $milliseconds; 51 | } 52 | 53 | public function asNanoseconds(): float 54 | { 55 | return $this->nanoseconds; 56 | } 57 | 58 | public function asMicroseconds(): float 59 | { 60 | return $this->nanoseconds / 1000; 61 | } 62 | 63 | public function asMilliseconds(): float 64 | { 65 | return $this->nanoseconds / 1000000; 66 | } 67 | 68 | public function asSeconds(): float 69 | { 70 | return $this->nanoseconds / 1000000000; 71 | } 72 | 73 | public function asString(): string 74 | { 75 | $result = ''; 76 | 77 | if ($this->hours > 0) { 78 | $result = sprintf('%02d', $this->hours) . ':'; 79 | } 80 | 81 | $result .= sprintf('%02d', $this->minutes) . ':'; 82 | $result .= sprintf('%02d', $this->seconds); 83 | 84 | if ($this->milliseconds > 0) { 85 | $result .= '.' . sprintf('%03d', $this->milliseconds); 86 | } 87 | 88 | return $result; 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/ResourceUsageFormatter.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\Timer; 11 | 12 | use function is_float; 13 | use function memory_get_peak_usage; 14 | use function microtime; 15 | use function sprintf; 16 | 17 | final class ResourceUsageFormatter 18 | { 19 | /** 20 | * @var array 21 | */ 22 | private const array SIZES = [ 23 | 'GB' => 1073741824, 24 | 'MB' => 1048576, 25 | 'KB' => 1024, 26 | ]; 27 | 28 | public function resourceUsage(Duration $duration): string 29 | { 30 | return sprintf( 31 | 'Time: %s, Memory: %s', 32 | $duration->asString(), 33 | $this->bytesToString(memory_get_peak_usage(true)), 34 | ); 35 | } 36 | 37 | /** 38 | * @throws TimeSinceStartOfRequestNotAvailableException 39 | */ 40 | public function resourceUsageSinceStartOfRequest(): string 41 | { 42 | if (!isset($_SERVER['REQUEST_TIME_FLOAT'])) { 43 | throw new TimeSinceStartOfRequestNotAvailableException( 44 | 'Cannot determine time at which the request started because $_SERVER[\'REQUEST_TIME_FLOAT\'] is not available', 45 | ); 46 | } 47 | 48 | if (!is_float($_SERVER['REQUEST_TIME_FLOAT'])) { 49 | throw new TimeSinceStartOfRequestNotAvailableException( 50 | 'Cannot determine time at which the request started because $_SERVER[\'REQUEST_TIME_FLOAT\'] is not of type float', 51 | ); 52 | } 53 | 54 | return $this->resourceUsage( 55 | Duration::fromMicroseconds( 56 | (1000000 * (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'])), 57 | ), 58 | ); 59 | } 60 | 61 | private function bytesToString(int $bytes): string 62 | { 63 | foreach (self::SIZES as $unit => $value) { 64 | if ($bytes >= $value) { 65 | return sprintf('%.2f %s', $bytes / $value, $unit); 66 | } 67 | } 68 | 69 | // @codeCoverageIgnoreStart 70 | return $bytes . ' byte' . ($bytes !== 1 ? 's' : ''); 71 | // @codeCoverageIgnoreEnd 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/Timer.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\Timer; 11 | 12 | use function array_pop; 13 | use function hrtime; 14 | 15 | final class Timer 16 | { 17 | /** 18 | * @var list 19 | */ 20 | private array $startTimes = []; 21 | 22 | public function start(): void 23 | { 24 | $this->startTimes[] = (float) hrtime(true); 25 | } 26 | 27 | /** 28 | * @throws NoActiveTimerException 29 | */ 30 | public function stop(): Duration 31 | { 32 | if ($this->startTimes === []) { 33 | throw new NoActiveTimerException( 34 | 'Timer::start() has to be called before Timer::stop()', 35 | ); 36 | } 37 | 38 | return Duration::fromNanoseconds((float) hrtime(true) - array_pop($this->startTimes)); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/exceptions/Exception.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\Timer; 11 | 12 | use Throwable; 13 | 14 | interface Exception extends Throwable 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/exceptions/NoActiveTimerException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\Timer; 11 | 12 | use LogicException; 13 | 14 | final class NoActiveTimerException extends LogicException implements Exception 15 | { 16 | } 17 | -------------------------------------------------------------------------------- /src/exceptions/TimeSinceStartOfRequestNotAvailableException.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\Timer; 11 | 12 | use RuntimeException; 13 | 14 | final class TimeSinceStartOfRequestNotAvailableException extends RuntimeException implements Exception 15 | { 16 | } 17 | --------------------------------------------------------------------------------