Skip to content

Commit 13bc52f

Browse files
Remove PHP_Timer dependency
1 parent e5d9b7b commit 13bc52f

File tree

7 files changed

+205
-7
lines changed

7 files changed

+205
-7
lines changed

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@
3535
"ext-dom": "*",
3636
"ext-json": "*",
3737
"ext-spl": "*",
38-
"phpunit/php-timer": "~1.0",
39-
"sebastian/environment": "~1.1",
40-
"sebastian/version": "~1.0",
38+
"sebastian/environment": "~1.1|~2.0|~3.0",
39+
"sebastian/version": "~1.0|~2.0",
4140
"sebastianfeldmann/cli": "~2.0",
4241
"sebastianfeldmann/ftp": "~0.9",
4342
"swiftmailer/swiftmailer": "~5.3|~6.0",

src/Cli/Statistics.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace phpbu\App\Cli;
3+
4+
use phpbu\App\Util\Time;
5+
6+
/**
7+
* Statistics class.
8+
*
9+
* @package phpbu
10+
* @subpackage Cli
11+
* @author Sebastian Feldmann <[email protected]>
12+
* @copyright Sebastian Feldmann <[email protected]>
13+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
14+
* @link http://phpbu.de/
15+
* @since Class available since Release 5.1.2
16+
*/
17+
final class Statistics
18+
{
19+
/**
20+
* Returns a string like 'Time: 1 minute 20 seconds Memory: 3,5 MB'
21+
*/
22+
public static function resourceUsage() : string
23+
{
24+
return \sprintf(
25+
'Time: %s, Memory: %4.2fMB',
26+
Time::formatTime(Time::timeSinceExecutionStart()),
27+
\memory_get_peak_usage(true) / 1048576
28+
);
29+
}
30+
}

src/Log/Mail.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22
namespace phpbu\App\Log;
33

4+
use phpbu\App\Cli\Statistics;
45
use phpbu\App\Exception;
56
use phpbu\App\Event;
67
use phpbu\App\Listener;
78
use phpbu\App\Result;
89
use phpbu\App\Log\MailTemplate as TPL;
910
use phpbu\App\Util\Arr;
1011
use phpbu\App\Util\Str;
11-
use PHP_Timer;
1212
use Swift_Mailer;
1313
use Swift_Message;
1414

@@ -549,7 +549,7 @@ protected function getInfoHtml(Result $result)
549549
*/
550550
protected function getFooterHtml()
551551
{
552-
return '<p ' . TPL::getSnippet('sStats') . '>' . PHP_Timer::resourceUsage() . '</p>' .
552+
return '<p ' . TPL::getSnippet('sStats') . '>' . Statistics::resourceUsage() . '</p>' .
553553
'</td></tr></table>';
554554
}
555555
}

src/Result/PrinterCli.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22
namespace phpbu\App\Result;
33

4+
use phpbu\App\Cli\Statistics;
45
use phpbu\App\Event;
56
use phpbu\App\Listener;
67
use phpbu\App\Result;
78
use phpbu\App\Util;
8-
use PHP_Timer;
99
use SebastianBergmann\Environment\Console;
1010
use SebastianBergmann\Environment\Runtime;
1111

@@ -440,7 +440,7 @@ public function printResult(Result $result)
440440
*/
441441
protected function printHeader()
442442
{
443-
$this->write(PHP_Timer::resourceUsage() . PHP_EOL . PHP_EOL);
443+
$this->write(Statistics::resourceUsage() . PHP_EOL . PHP_EOL);
444444
}
445445

446446
/**

src/Util/Time.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace phpbu\App\Util;
3+
4+
use RuntimeException;
5+
6+
/**
7+
* Time utility class.
8+
*
9+
* @package phpbu
10+
* @subpackage Util
11+
* @author Sebastian Feldmann <[email protected]>
12+
* @copyright Sebastian Feldmann <[email protected]>
13+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
14+
* @link http://phpbu.de/
15+
* @since Class available since Release 5.1.2
16+
*/
17+
class Time
18+
{
19+
/**
20+
* Time formatting helper
21+
*
22+
* @var array
23+
*/
24+
private static $times = [
25+
'hour' => 3600,
26+
'minute' => 60,
27+
'second' => 1
28+
];
29+
30+
/**
31+
* Returns the time passed since execution start.
32+
*
33+
* @throws \RuntimeException
34+
*/
35+
public static function timeSinceExecutionStart() : float
36+
{
37+
if (isset($_SERVER['REQUEST_TIME_FLOAT'])) {
38+
$startOfRequest = $_SERVER['REQUEST_TIME_FLOAT'];
39+
} elseif (isset($_SERVER['REQUEST_TIME'])) {
40+
$startOfRequest = $_SERVER['REQUEST_TIME'];
41+
} else {
42+
throw new RuntimeException('Cannot determine time at which the execution started');
43+
}
44+
return \microtime(true) - $startOfRequest;
45+
}
46+
47+
/**
48+
* Return string like '1 hour 3 minutes 12 seconds'.
49+
*
50+
* @param float $time
51+
* @return string
52+
*/
53+
public static function formatTime(float $time) : string
54+
{
55+
$time = $time < 1 ? 1 : round($time);
56+
$formatted = [];
57+
foreach (self::$times as $unit => $value) {
58+
if ($time >= $value) {
59+
$units = \floor($time / $value);
60+
$time -= $units * $value;
61+
$formatted[] = $units . ' ' . ($units == 1 ? $unit : $unit . 's');
62+
}
63+
}
64+
return implode(' ', $formatted);
65+
}
66+
}

tests/phpbu/Cli/StatisticsTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace phpbu\App\Cli;
3+
4+
/**
5+
* StatisticsTest
6+
*
7+
* @package phpbu
8+
* @subpackage tests
9+
* @author Sebastian Feldmann <[email protected]>
10+
* @copyright Sebastian Feldmann <[email protected]>
11+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
12+
* @link http://www.phpbu.de/
13+
* @since Class available since Release 5.1.2
14+
*/
15+
class StatisticsTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* Tests Statistics::resourceUsage
19+
*/
20+
public function testResourceUsage()
21+
{
22+
$usage = Statistics::resourceUsage();
23+
24+
$this->assertTrue(strpos($usage, 'Time:') !== false);
25+
$this->assertTrue(strpos($usage, 'Memory:') !== false);
26+
}
27+
}

tests/phpbu/Util/TimeTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace phpbu\App\Util;
3+
4+
/**
5+
* Time utility test
6+
*
7+
* @package phpbu
8+
* @subpackage tests
9+
* @author Sebastian Feldmann <[email protected]>
10+
* @copyright Sebastian Feldmann <[email protected]>
11+
* @license https://opensource.org/licenses/MIT The MIT License (MIT)
12+
* @link http://www.phpbu.de/
13+
* @since Class available since Release 5.1.2
14+
*/
15+
class TimeTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* Tests Time::timeSinceExecutionStart
19+
*
20+
* @expectedException \RuntimeException
21+
*/
22+
public function testTimeSinceExecutionStartFail()
23+
{
24+
$SERVER = $_SERVER;
25+
unset($_SERVER['REQUEST_TIME_FLOAT']);
26+
unset($_SERVER['REQUEST_TIME']);
27+
28+
try {
29+
$time = Time::timeSinceExecutionStart();
30+
} catch (\Exception $e) {
31+
$_SERVER = $SERVER;
32+
throw $e;
33+
}
34+
35+
$this->assertFalse($time);
36+
}
37+
38+
/**
39+
* Tests Time::timeSinceExecutionStart
40+
*/
41+
public function testTimeSinceExecutionStartFloat()
42+
{
43+
$SERVER = $_SERVER;
44+
$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true) - 60;
45+
$time = Time::timeSinceExecutionStart();
46+
$this->assertEquals(60, floor($time));
47+
$_SERVER = $SERVER;
48+
}
49+
50+
/**
51+
* Tests Time::timeSinceExecutionStart
52+
*/
53+
public function testTimeSinceExecutionStart()
54+
{
55+
$SERVER = $_SERVER;
56+
unset($_SERVER['REQUEST_TIME_FLOAT']);
57+
$_SERVER['REQUEST_TIME'] = time() - 60;
58+
$time = Time::timeSinceExecutionStart();
59+
$this->assertEquals(60, floor($time));
60+
$_SERVER = $SERVER;
61+
}
62+
63+
/**
64+
* Tests Time::formatTime
65+
*/
66+
public function testFormatTime()
67+
{
68+
$this->assertEquals('37 seconds', Time::formatTime(37));
69+
$this->assertEquals('1 hour 1 second', Time::formatTime(3601));
70+
$this->assertEquals('1 hour 1 minute 1 second', Time::formatTime(3661));
71+
$this->assertEquals('2 hours 2 minutes 2 seconds', Time::formatTime(7322));
72+
$this->assertEquals('1 hour', Time::formatTime(3600));
73+
$this->assertEquals('1 minute', Time::formatTime(60));
74+
$this->assertEquals('1 second', Time::formatTime(1));
75+
}
76+
}

0 commit comments

Comments
 (0)