sebastianbergmann / php-timer Goto Github PK
View Code? Open in Web Editor NEWUtility class for timing
License: BSD 3-Clause "New" or "Revised" License
Utility class for timing
License: BSD 3-Clause "New" or "Revised" License
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();
});
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.
Hi
If possible can you add a way to output in HH:MM:SS
thanks
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();
tools/composer
and managed through composer self-update
(see update-tools
target in build.xmltools/psalm
(phive install --copy psalm
)tools/php-cs-fixer
(phive install --copy php-cs-fixer
)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?
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.
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
PHP_TIMER requires php version gt 5.3.3
$_SERVER['REQUEST_TIME_FLOAT'] is available since php version 5.4.0
so, if SERVER['REQUEST_TIME_FLOAT'] is not available use SERVER['REQUEST_TIME']
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.
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!
How I can count time of all executed files in my script? I want check which one files take too long loading time.
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.
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?
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?
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
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.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.