initial
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
# ChangeLog
|
||||
|
||||
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
|
||||
|
||||
## [2.1.4] - 2024-03-01
|
||||
|
||||
* No code changes, only updated `.gitattributes` to not export non-essential files.
|
||||
|
||||
## [2.1.3] - 2020-11-30
|
||||
|
||||
### Changed
|
||||
|
||||
* Changed PHP version constraint in `composer.json` from `^7.1` to `>=7.1`
|
||||
|
||||
## [2.1.2] - 2019-06-07
|
||||
|
||||
### Fixed
|
||||
|
||||
* Fixed [#21](https://github.com/sebastianbergmann/php-timer/pull/3352): Formatting of memory consumption does not work on 32bit systems
|
||||
|
||||
## [2.1.1] - 2019-02-20
|
||||
|
||||
### Changed
|
||||
|
||||
* Improved formatting of memory consumption for `resourceUsage()`
|
||||
|
||||
## [2.1.0] - 2019-02-20
|
||||
|
||||
### Changed
|
||||
|
||||
* Improved formatting of memory consumption for `resourceUsage()`
|
||||
|
||||
## [2.0.0] - 2018-02-01
|
||||
|
||||
### Changed
|
||||
|
||||
* This component now uses namespaces
|
||||
|
||||
### Removed
|
||||
|
||||
* This component is no longer supported on PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, and PHP 7.0
|
||||
|
||||
[2.1.4]: https://github.com/sebastianbergmann/diff/compare/2.1.3...2.1.4
|
||||
[2.1.3]: https://github.com/sebastianbergmann/diff/compare/2.1.2...2.1.3
|
||||
[2.1.2]: https://github.com/sebastianbergmann/diff/compare/2.1.1...2.1.2
|
||||
[2.1.1]: https://github.com/sebastianbergmann/diff/compare/2.1.0...2.1.1
|
||||
[2.1.0]: https://github.com/sebastianbergmann/diff/compare/2.0.0...2.1.0
|
||||
[2.0.0]: https://github.com/sebastianbergmann/diff/compare/1.0.9...2.0.0
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
phpunit/php-timer
|
||||
|
||||
Copyright (c) 2010-2019, Sebastian Bergmann <sebastian@phpunit.de>.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions
|
||||
are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in
|
||||
the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
* Neither the name of Sebastian Bergmann nor the names of his
|
||||
contributors may be used to endorse or promote products derived
|
||||
from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
[](https://travis-ci.org/sebastianbergmann/php-timer)
|
||||
|
||||
# phpunit/php-timer
|
||||
|
||||
Utility class for timing things, factored out of PHPUnit into a stand-alone component.
|
||||
|
||||
## Installation
|
||||
|
||||
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
|
||||
|
||||
composer require phpunit/php-timer
|
||||
|
||||
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:
|
||||
|
||||
composer require --dev phpunit/php-timer
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Timing
|
||||
|
||||
```php
|
||||
use SebastianBergmann\Timer\Timer;
|
||||
|
||||
Timer::start();
|
||||
|
||||
// ...
|
||||
|
||||
$time = Timer::stop();
|
||||
var_dump($time);
|
||||
|
||||
print Timer::secondsToTimeString($time);
|
||||
```
|
||||
|
||||
The code above yields the output below:
|
||||
|
||||
double(1.0967254638672E-5)
|
||||
0 ms
|
||||
|
||||
### Resource Consumption Since PHP Startup
|
||||
|
||||
```php
|
||||
use SebastianBergmann\Timer\Timer;
|
||||
|
||||
print Timer::resourceUsage();
|
||||
```
|
||||
|
||||
The code above yields the output below:
|
||||
|
||||
Time: 0 ms, Memory: 0.50MB
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "phpunit/php-timer",
|
||||
"description": "Utility class for timing",
|
||||
"type": "library",
|
||||
"keywords": [
|
||||
"timer"
|
||||
],
|
||||
"homepage": "https://github.com/sebastianbergmann/php-timer/",
|
||||
"license": "BSD-3-Clause",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Sebastian Bergmann",
|
||||
"email": "sebastian@phpunit.de",
|
||||
"role": "lead"
|
||||
}
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/sebastianbergmann/php-timer/issues"
|
||||
},
|
||||
"prefer-stable": true,
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^8.5"
|
||||
},
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true
|
||||
},
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"src/"
|
||||
]
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.1-dev"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-timer.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Timer;
|
||||
|
||||
interface Exception
|
||||
{
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-timer.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Timer;
|
||||
|
||||
final class RuntimeException extends \RuntimeException implements Exception
|
||||
{
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This file is part of phpunit/php-timer.
|
||||
*
|
||||
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
namespace SebastianBergmann\Timer;
|
||||
|
||||
final class Timer
|
||||
{
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private static $sizes = [
|
||||
'GB' => 1073741824,
|
||||
'MB' => 1048576,
|
||||
'KB' => 1024,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private static $times = [
|
||||
'hour' => 3600000,
|
||||
'minute' => 60000,
|
||||
'second' => 1000,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var float[]
|
||||
*/
|
||||
private static $startTimes = [];
|
||||
|
||||
public static function start(): void
|
||||
{
|
||||
self::$startTimes[] = \microtime(true);
|
||||
}
|
||||
|
||||
public static function stop(): float
|
||||
{
|
||||
return \microtime(true) - \array_pop(self::$startTimes);
|
||||
}
|
||||
|
||||
public static function bytesToString(float $bytes): string
|
||||
{
|
||||
foreach (self::$sizes as $unit => $value) {
|
||||
if ($bytes >= $value) {
|
||||
return \sprintf('%.2f %s', $bytes >= 1024 ? $bytes / $value : $bytes, $unit);
|
||||
}
|
||||
}
|
||||
|
||||
return $bytes . ' byte' . ((int) $bytes !== 1 ? 's' : '');
|
||||
}
|
||||
|
||||
public static function secondsToTimeString(float $time): string
|
||||
{
|
||||
$ms = \round($time * 1000);
|
||||
|
||||
foreach (self::$times as $unit => $value) {
|
||||
if ($ms >= $value) {
|
||||
$time = \floor($ms / $value * 100.0) / 100.0;
|
||||
|
||||
return $time . ' ' . ($time == 1 ? $unit : $unit . 's');
|
||||
}
|
||||
}
|
||||
|
||||
return $ms . ' ms';
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public static function timeSinceStartOfRequest(): string
|
||||
{
|
||||
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
|
||||
$startOfRequest = $_SERVER['REQUEST_TIME_FLOAT'];
|
||||
} elseif (isset($_SERVER['REQUEST_TIME'])) {
|
||||
$startOfRequest = $_SERVER['REQUEST_TIME'];
|
||||
} else {
|
||||
throw new RuntimeException('Cannot determine time at which the request started');
|
||||
}
|
||||
|
||||
return self::secondsToTimeString(\microtime(true) - $startOfRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public static function resourceUsage(): string
|
||||
{
|
||||
return \sprintf(
|
||||
'Time: %s, Memory: %s',
|
||||
self::timeSinceStartOfRequest(),
|
||||
self::bytesToString(\memory_get_peak_usage(true))
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user