Php-timer - Utility class for timing

Overview

phpunit/php-timer

CI Status Type Coverage

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:

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

require __DIR__ . '/vendor/autoload.php';

use SebastianBergmann\Timer\Timer;

$timer = new Timer;

$timer->start();

foreach (\range(0, 100000) as $i) {
    // ...
}

$duration = $timer->stop();

var_dump(get_class($duration));
var_dump($duration->asString());
var_dump($duration->asSeconds());
var_dump($duration->asMilliseconds());
var_dump($duration->asMicroseconds());
var_dump($duration->asNanoseconds());

The code above yields the output below:

string(32) "SebastianBergmann\Timer\Duration"
string(9) "00:00.002"
float(0.002851062)
float(2.851062)
float(2851.062)
int(2851062)

Resource Consumption

Explicit duration

require __DIR__ . '/vendor/autoload.php';

use SebastianBergmann\Timer\ResourceUsageFormatter;
use SebastianBergmann\Timer\Timer;

$timer = new Timer;
$timer->start();

foreach (\range(0, 100000) as $i) {
    // ...
}

print (new ResourceUsageFormatter)->resourceUsage($timer->stop());

The code above yields the output below:

Time: 00:00.002, Memory: 6.00 MB

Duration since PHP Startup (using unreliable $_SERVER['REQUEST_TIME_FLOAT'])

require __DIR__ . '/vendor/autoload.php';

use SebastianBergmann\Timer\ResourceUsageFormatter;

foreach (\range(0, 100000) as $i) {
    // ...
}

print (new ResourceUsageFormatter)->resourceUsageSinceStartOfRequest();

The code above yields the output below:

Time: 00:00.002, Memory: 6.00 MB
Comments
  •  Class 'PHP_Timer' not found

    Class 'PHP_Timer' not found

    Today, I got the following error, and I Found that the Timer.php class is changed this morning. The class name "PHP_Timer" is changed to "Timer".

    01-Feb-2018 11:24:54 [exec] PHP Fatal error: Uncaught Error: Class 'PHP_Timer' not found in /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/Framework/TestResult.php:577 01-Feb-2018 11:24:54 [exec] Stack trace: 01-Feb-2018 11:24:54 [exec] #0 /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/Framework/TestCase.php(702): PHPUnit_Framework_TestResult->run(Object(BenefitTest)) 01-Feb-2018 11:24:54 [exec] #1 /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/Framework/TestSuite.php(735): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult)) 01-Feb-2018 11:24:54 [exec] #2 /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/Framework/TestSuite.php(735): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult)) 01-Feb-2018 11:24:54 [exec] #3 /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(432): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult)) 01-Feb-2018 11:24:54 [exec] #4 /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/TextUI/Command in /bamboo/bamboo-home/xml-data/build-dir/196609/WEBS-WEB9-PUT/vendor/phpunit/phpunit/src/Framework/TestResult.php on line 577

    opened by jamesqjl 7
  • Pear channel download failure

    Pear channel download failure

    Hi Sebastian,

    Just wanted to let you know that the pear channel is giving a No releases available for package error.

    I tried all of the below commands in turn: pear install phpunit/PHP_Timer pear install phpunit/PHP_Times-1.0.5 pear install PHP_Times

    Smile, Juliette

    opened by jrfnl 7
  • 32-bit compatibility

    32-bit compatibility

    I see lot of test failure on 32-bit arch

    There were 16 failures:
    1) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #18 ('59:59.900', -695067296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'59:59.900'
    +'-12:3624.932'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    2) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #32 ('01:00:59.010', -635957296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:59.010'
    +'-11:3624.042'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    3) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #31 ('01:00:59.001', -635966296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:59.001'
    +'-11:3624.033'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    4) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #30 ('01:00:59.999', -634967396)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:59.999'
    +'-11:3625.032'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    5) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #29 ('01:00:01.999', -692967396)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:01.999'
    +'-12:3627.032'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    6) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #28 ('01:00:01.999', -692968296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:01.999'
    +'-12:3627.031'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    7) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #27 ('01:00:01.990', -692977296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:01.990'
    +'-12:3627.022'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    8) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #26 ('01:00:01.900', -693067296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:01.900'
    +'-12:3626.932'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    9) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #25 ('01:00:01', -693967296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:01'
    +'-12:3626.032'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    10) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #24 ('01:00:00', -694967296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:00:00'
    +'-12:3625.032'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    11) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #23 ('59:59.010', -695957296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'59:59.010'
    +'-12:3624.042'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    12) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #22 ('59:59.001', -695966296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'59:59.001'
    +'-12:3624.033'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    13) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #21 ('59:59.999', -694967396)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'59:59.999'
    +'-12:3625.032'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    14) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #20 ('59:59.999', -694968296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'59:59.999'
    +'-12:3625.031'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    15) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #19 ('59:59.990', -694977296)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'59:59.990'
    +'-12:3625.022'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    16) SebastianBergmann\Timer\DurationTest::testCanBeFormattedAsString with data set #33 ('01:59:59.999', -1389934692)
    Failed asserting that two strings are identical.
    --- Expected
    +++ Actual
    @@ @@
    -'01:59:59.999'
    +'-24:3650.065'
    /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/tests/DurationTest.php:50
    FAILURES!
    Tests: 42, Assertions: 48, Failures: 16.
    
    
    opened by remicollet 3
  • fix for 32-bit where large value are converted to float

    fix for 32-bit where large value are converted to float

    Test suite fails on 32-bit computer (yes, this still exists.... arm...)

    There were 2 errors:
    1) SebastianBergmann\Timer\TimerTest::testCanFormatBytesAsString with data set #7 ('3.00 GB', 3221225472.0)
    TypeError: Argument 2 passed to SebastianBergmann\Timer\TimerTest::testCanFormatBytesAsString() must be of the type integer, float given, called in /usr/share/php/PHPUnit7/Framework/TestCase.php on line 1153
    /builddir/build/BUILD/php-timer-8b389aebe1b8b0578430bda0c7c95a829608e059/tests/TimerTest.php:115
    2) SebastianBergmann\Timer\TimerTest::testCanFormatBytesAsString with data set #8 ('3.50 GB', 3758096384.0)
    TypeError: Argument 2 passed to SebastianBergmann\Timer\TimerTest::testCanFormatBytesAsString() must be of the type integer, float given, called in /usr/share/php/PHPUnit7/Framework/TestCase.php on line 1153
    /builddir/build/BUILD/php-timer-8b389aebe1b8b0578430bda0c7c95a829608e059/tests/TimerTest.php:115
    

    Allowing float (large integer) seems to me the simple way (despite I really think memory consumption should never be so high on a 32-bit computer... but some still running 32-bit software on big hardware for strange reasons...)

    opened by remicollet 3
  • Installation failed - Requirements could not be resolved

    Installation failed - Requirements could not be resolved

    I know this isn't a specific problem of this package, but I kindly ask for your help.

    I've a Laravel 7 project with this composer.json:

    "require-dev": { ... "phpunit/phpunit": "^8.5" }, ... "minimum-stability": "dev", "prefer-stable": true,

    When I run composer require phpunit/php-timer, I got this error:

    Using version ^3.0 for phpunit/php-timer ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Your requirements could not be resolved to an installable set of packages.

    Problem 1 - phpunit/phpunit 8.5.2 requires phpunit/php-timer ^2.1.2 -> satisfiable by phpunit/php-timer[2.1.2] but these conflict with your requirements or minimum-stability. - phpunit/phpunit 8.5.2 requires phpunit/php-timer ^2.1.2 -> satisfiable by phpunit/php-timer[2.1.2] but these conflict with your requirements or minimum-stability. - phpunit/phpunit 8.5.2 requires phpunit/php-timer ^2.1.2 -> satisfiable by phpunit/php-timer[2.1.2] but these conflict with your requirements or minimum-stability. - Installation request for phpunit/phpunit (locked at 8.5.2, required as ^8.5) -> satisfiable by phpunit/phpunit[8.5.2].

    What can I do to install this package?

    Thank you very much for any help.

    opened by eleftrik 2
  • View timer progress

    View timer progress

    Can we have a way to fetch the current timer progress ie though a say $timer->watch() that returns a Duration object at the point in time.

    Ideally it would be great to just have a timer object that you can have constantly running.

    opened by tsmgeek 1
  • Nano optimization when acquiring time

    Nano optimization when acquiring time

    Hi,

    I'm building for fun a small library for benchmarking Closures and I started it by doing my own Stopwatch class. Then, by chance I found this package and I decided to use it as hard dependency instead.

    This PR:

    • [x] Move the hrtime() call at the beginning of the stop() method, for a better accuracy. Indeed, executing the if condition takes time and this will prevent that loss of time.
    • [x] Do not cast hrtime() before saving it in $this->startTimes. Once again, casting the value will take time, so, we can do it at the very end in order to prevent that loss of time due to the casting.
    • [x] This is not even micro optimization, this is nano optimization! :-)
    opened by drupol 1
  • Fix #34 test failing on 32-bit

    Fix #34 test failing on 32-bit

    On 32-bit arch

    Runtime:       PHP 7.3.23
    Configuration: /builddir/build/BUILD/php-timer-5a63ce20ed1b5bf577850e2c4e87f4aa902afbd2/phpunit.xml
    
    ..........................................                        42 / 42 (100%)
    
    Time: 00:00.005, Memory: 4.00 MB
    
    OK (42 tests, 48 assertions)
    
    
    opened by remicollet 1
  • php-cs-fixer malware detected?

    php-cs-fixer malware detected?

    Hello, we are using some of your module in our php web site that use YII2 and we are getting some warning from antivirus regarding php-cs-fixer file contained in more than one project under tools folder. It is detected as generic malware or from virustotal website as VEX.Webshell. Are you sure it is safe? Thank you!

    opened by leonardoburchi 1
  • Type Error on 32-bit systems

    Type Error on 32-bit systems

    PHPUnit 9.2.1 PHPTimer 4.0.0 Runtime: PHP 7.4.6 with Xdebug 2.9.5

    After updating to PHPUnit 9.2.1 with PHPTimer 4.0.0 I got this error.

    In version PHPUnit 9.1.5 with PHPTimer 3.1.4 this error doesn't occure.

    PHPUnit\Framework\Exception: PHP Fatal error: Uncaught TypeError: Argument 1 passed to SebastianBergmann\Timer\Duration::fromNanoseconds() must be of the type int, float given, called in /vendor/phpunit/php-timer/src/Timer.php on line 35 and defined in /vendor/phpunit/php-timer/src/Duration.php:47 │ Stack trace: │ #0 /vendor/phpunit/php-timer/src/Timer.php(35): SebastianBergmann\Timer\Duration::fromNanoseconds() │ #1 /vendor/phpunit/phpunit/src/Framework/TestResult.php(743): SebastianBergmann\Timer\Timer->stop() │ #2 /vendor/phpunit/phpunit/src/Framework/TestCase.php(771): PHPUnit\Framework\TestResult->run() │ #3 Standard input code(392): PHPUnit\Framework\TestCase->run() │ #4 Standard input code(584): __phpunit_run_isolated_test() │ #5 {main} │ thrown in /vendor/phpunit/php-timer/src/Duration.php on line 47 │ │ Fatal error: Uncaught TypeError: Argument 1 passed to SebastianBergmann\Timer\Duration::fromNanoseconds() must be of the type int, float given, called in /vendor/phpunit/php-timer/src/Timer.php on line 35 and defined in /vendor/phpunit/php-timer/src/Duration.php on line 47 │ │ TypeError: Argument 1 passed to SebastianBergmann\Timer\Duration::fromNanoseconds() must be of the type int, float given, called in /vendor/phpunit/php-timer/src/Timer.php on line 35 in /vendor/phpunit/php-timer/src/Duration.php on line 47 │ │ Call Stack: │ 0.0019 511792 1. {main}() Standard input code:0 │ 0.2221 4949016 2. __phpunit_run_isolated_test() Standard input code:584 │ 0.2251 5035944 3. ApplicationTest\ModuleTest->run() Standard input code:392 │ 0.2252 5035944 4. PHPUnit\Framework\TestResult->run() /vendor/phpunit/phpunit/src/Framework/TestCase.php:771 │ 0.3580 6261320 5. SebastianBergmann\Timer\Timer->stop() /vendor/phpunit/phpunit/src/Framework/TestResult.php:743 │ 0.3592 6270248 6. SebastianBergmann\Timer\Duration::fromNanoseconds() /vendor/phpunit/php-timer/src/Timer.php:35

    bug 
    opened by CreativeNative 1
  • Fix: Remove sudo configuration

    Fix: Remove sudo configuration

    This PR

    • [x] removes outdated sudo configuration from .travis.yml

    💁‍♂️ For reference, see https://blog.travis-ci.com/2018-11-19-required-linux-infrastructure-migration.

    opened by localheinz 1
Owner
Sebastian Bergmann
Sebastian Bergmann is the creator of PHPUnit. He co-founded thePHP.cc and helps PHP teams build better software.
Sebastian Bergmann
My first attempt at creating my own Rubik's Cube timing interface with PHP and JS!

Cubing Sessions My first attempt at creating my own Rubik's Cube timing interface with PHP and JS! I haved named it the PA Timer for fun, as I hail fr

Adam Gradess 0 Jan 12, 2022
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-PHP Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects. Lodash-PHP tr

Lodash PHP 474 Dec 31, 2022
Exploiting and fixing security vulnerabilities of an old version of E-Class. Project implemented as part of the class YS13 Cyber-Security.

Open eClass 2.3 Development of XSS, CSRF, SQLi, RFI attacks/defences of an older,vulnerable version of eclass. Project implemented as part of the clas

Aristi_Papastavrou 11 Apr 23, 2022
A utility package that helps inspect functions in PHP.

A utility package that helps inspect functions in PHP. This package provides some utilities for inspecting functions (callables) in PHP. You can use i

Ryan Chandler 14 May 24, 2022
An utility component for XML usage and best practices in PHP

An utility component for XML usage and best practices in PHP

Laminas Project 13 Nov 26, 2022
Utility that helps you switch git configurations with ease.

git-profile Utility that helps you switch git configurations with ease Preface It is possible that you have multiple git configurations. For example:

Zeeshan Ahmad 240 Jul 18, 2022
[READ-ONLY] CakePHP Utility classes such as Inflector, Text, Hash, Security and Xml. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP Utility Classes This library provides a range of utility classes that are used throughout the CakePHP framework What's in the toolbox? Hash A

CakePHP 112 Feb 15, 2022
Small utility library that handles metadata minification and expansion.

composer/metadata-minifier Small utility library that handles metadata minification and expansion.

Composer 134 Dec 26, 2022
m4b-tool is a command line utility to merge, split and chapterize audiobook files such as mp3, ogg, flac, m4a or m4b

m4b-tool m4b-tool is a is a wrapper for ffmpeg and mp4v2 to merge, split or and manipulate audiobook files with chapters. Although m4b-tool is designe

Andreas 798 Jan 8, 2023
Xr - Lightweight debug server utility built on ReactPHP.

XR ?? Subscribe to the newsletter to don't miss any update regarding Chevere. XR is a dump server utility built on top of ReactPHP. Features ✨ Dump si

Chevere 195 Dec 17, 2022
Warden is a CLI utility for orchestrating Docker based developer environments

Warden Warden is a CLI utility for orchestrating Docker based developer environments, and enables multiple local environments to run simultaneously wi

David Alger 314 Dec 2, 2022
Glob-like file and pattern matching utility.

Glob-like file and pattern matching utility.

Chris Kankiewicz 94 Dec 14, 2022
The Pantheon CLI — a standalone utility for performing operations on the Pantheon Platform

terminus : Pantheon's Command-Line Interface Status About Terminus is Pantheon's Command Line Interface (CLI), providing at least equivalent functiona

Pantheon 290 Dec 26, 2022
Optimizes class loading performance by generating a single PHP file containing all of the autoloaded files.

Class Preloader for PHP This tool is used to generate a single PHP script containing all of the classes required for a specific use case. Using a sing

Class Preloader 356 Nov 26, 2022
A small package to add computed properties to any PHP class. 🐘

Computed Properties This package provides a trait and attribute that can provide computed property support. Installation This package can be installed

Ryan Chandler 22 Mar 28, 2022
Class for manage directly leds on raspberry pi in PHP

led_raspberry Class for manage directly leds on raspberry pi in PHP We can access GPIO ports directly with fwrite() for power on|off leds, without use

Rafael Martin Soto 8 Sep 15, 2021
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

Doctrine 1.4k Dec 29, 2022
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
Simple class that implement a CAPTCHA for your PHP App.

simple-captcha Simple class that implement a CAPTCHA for your PHP App. Installation Use the package manager composer to install. composer require will

WILLIAM B. SAMPAIO 3 Nov 9, 2022