Code Monkey home page Code Monkey logo

php-timer's Introduction

phpunit/php-timer

Latest Stable Version CI Status Type Coverage codecov

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

php-timer's People

Contributors

alfredbez avatar ayesh avatar grahamcampbell avatar henriquemoody avatar localheinz avatar majkl578 avatar mbischof avatar remicollet avatar sebastianbergmann avatar sun avatar whatthejeff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-timer's Issues

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

Add a TimerInterface so external packages can mock or spy the timer

Currently the Timer class is final, which means it cannot be mocked or extended and that's ok for general usage.
But because of this external packages cannot properly test any classes using the this timer.
Introducing an interface for the timer would fix this problem... or am I missing something?

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.

Support milliseconds in resourceUsage, $requestTime

To provide better granularity into test times when writing and running tests, it would be nice if PHP_Timer had an option to report times in milliseconds.

One way to do this would be to modify

$requestTime = time() 

and

return self::secondsToTimeString(time() - self::$requestTime);

in timeSinceStartOfRequest() to instead store a microtime(true) value, and then add an optional parameter to resourceUsage() to report the time in milliseconds, instead of seconds.

Do you think this would be valuable? If so I'll write the code and add the tests.

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

Direct include/require of ezc/Base/base.php

PHP/Token/Stream/Autoload.php, line 235 makes a direct include into ezComponents rather than allowing ezComponents to autoload it. Should php-timer allow exComponents to load itself?

Anonymous autoload breaks PHP 5.2 compatibility

The latest version of the PHP_Timer package breaks PHP 5.2 compatiblity, because it includes an anonymous function in the autoloader but doesn't bump the required PHP version in package.xml.

Modernize build automation

  • Use GitHub Actions instead of Travis CI
  • Have Composer in tools/composer and managed through composer self-update (see update-tools target in build.xml
  • Install Psalm using Phive as tools/psalm (phive install --copy psalm)
  • Install PHP-CS-Fixer using Phive as tools/php-cs-fixer (phive install --copy php-cs-fixer)
  • Create Psalm configuration
  • Add Psalm build step to GitHub Actions-based CI workflow
  • Add PHP-CS-Fixer build step to GitHub Actions-based CI workflow

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.

PHP_Timer relies on $_SERVER['REQUEST_TIME'] for PHPUnit CLI

I have upgraded all installed tools through "pear upgrade-all" and now I'll see the following output on every run of PHPUnit:

PHPUnit 3.5.15 by Sebastian Bergmann.

.PHP Notice: Undefined index: REQUEST_TIME in /usr/local/zend/share/pear/PHP/Timer.php on line 123
PHP Stack trace:
PHP 1. {main}() /usr/local/zend/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/zend/bin/phpunit:49
PHP 3. PHPUnit_TextUI_Command->run() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:188
PHP 5. PHPUnit_TextUI_ResultPrinter->printResult() /usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:311
PHP 6. PHPUnit_TextUI_ResultPrinter->printHeader() /usr/local/zend/share/pear/PHPUnit/TextUI/ResultPrinter.php:155
PHP 7. PHP_Timer::resourceUsage() /usr/local/zend/share/pear/PHPUnit/TextUI/ResultPrinter.php:329
PHP 8. PHP_Timer::timeSinceStartOfRequest() /usr/local/zend/share/pear/PHP/Timer.php:135

Notice: Undefined index: REQUEST_TIME in /usr/local/zend/share/pear/PHP/Timer.php on line 123

Call Stack:
0.0006 630256 1. {main}() /usr/local/zend/bin/phpunit:0
0.0577 1120600 2. PHPUnit_TextUI_Command::main() /usr/local/zend/bin/phpunit:49
0.0577 1121216 3. PHPUnit_TextUI_Command->run() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:129
0.0982 4943816 4. PHPUnit_TextUI_TestRunner->doRun() /usr/local/zend/share/pear/PHPUnit/TextUI/Command.php:188
0.1078 5613944 5. PHPUnit_TextUI_ResultPrinter->printResult() /usr/local/zend/share/pear/PHPUnit/TextUI/TestRunner.php:311
0.1078 5613944 6. PHPUnit_TextUI_ResultPrinter->printHeader() /usr/local/zend/share/pear/PHPUnit/TextUI/ResultPrinter.php:155
0.1078 5613944 7. PHP_Timer::resourceUsage() /usr/local/zend/share/pear/PHPUnit/TextUI/ResultPrinter.php:329
0.1078 5614040 8. PHP_Timer::timeSinceStartOfRequest() /usr/local/zend/share/pear/PHP/Timer.php:135

Time: 365100:13:03, Memory: 5.75Mb

OK (1 test, 1 assertion)

As far as I see, on line 123 of file PHP/Timer.php there it is hardcoded:

public static function timeSinceStartOfRequest()
{
    return self::secondsToTimeString(time() - $_SERVER['REQUEST_TIME']);
}

Maybe you can check before if $_SERVER exists or not to differentiate between HTTP request and CLI request?

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!

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.

Feature request: lap time and objects over classes

I'd like to see the following features:

  • support multiple timers by using objects instead of classes (maybe with a factory method)

  • allow to wrap functionality in a timer without boilerplate code, something like:

    PHP_Timer::lap(function($timer) {
        // do stuff, may show intermediate timings
    })->then(function($timer) {
       echo $timer->timeAsString();
    });

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

Error in documentation

About this basic sample use:

PHP_Timer::start();
$timer->start();

It show this error:

Fatal error: Call to a member function start() on a non-object

Could you change the documentation to:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.