-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
206 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
namespace phpbu\App\Cli; | ||
|
||
use phpbu\App\Util\Time; | ||
|
||
/** | ||
* Statistics class. | ||
* | ||
* @package phpbu | ||
* @subpackage Cli | ||
* @author Sebastian Feldmann <[email protected]> | ||
* @copyright Sebastian Feldmann <[email protected]> | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
* @link http://phpbu.de/ | ||
* @since Class available since Release 5.1.2 | ||
*/ | ||
final class Statistics | ||
{ | ||
/** | ||
* Returns a string like 'Time: 1 minute 20 seconds Memory: 3,5 MB' | ||
*/ | ||
public static function resourceUsage() : string | ||
{ | ||
return \sprintf( | ||
'Time: %s, Memory: %4.2fMB', | ||
Time::formatTime(Time::timeSinceExecutionStart()), | ||
\memory_get_peak_usage(true) / 1048576 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
namespace phpbu\App\Util; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Time utility class. | ||
* | ||
* @package phpbu | ||
* @subpackage Util | ||
* @author Sebastian Feldmann <[email protected]> | ||
* @copyright Sebastian Feldmann <[email protected]> | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
* @link http://phpbu.de/ | ||
* @since Class available since Release 5.1.2 | ||
*/ | ||
class Time | ||
{ | ||
/** | ||
* Time formatting helper | ||
* | ||
* @var array | ||
*/ | ||
private static $times = [ | ||
'hour' => 3600, | ||
'minute' => 60, | ||
'second' => 1 | ||
]; | ||
|
||
/** | ||
* Returns the time passed since execution start. | ||
* | ||
* @throws \RuntimeException | ||
*/ | ||
public static function timeSinceExecutionStart() : float | ||
{ | ||
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 execution started'); | ||
} | ||
return \microtime(true) - $startOfRequest; | ||
} | ||
|
||
/** | ||
* Return string like '1 hour 3 minutes 12 seconds'. | ||
* | ||
* @param float $time | ||
* @return string | ||
*/ | ||
public static function formatTime(float $time) : string | ||
{ | ||
$time = $time < 1 ? 1 : round($time); | ||
$formatted = []; | ||
foreach (self::$times as $unit => $value) { | ||
if ($time >= $value) { | ||
$units = \floor($time / $value); | ||
$time -= $units * $value; | ||
$formatted[] = $units . ' ' . ($units == 1 ? $unit : $unit . 's'); | ||
} | ||
} | ||
return implode(' ', $formatted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
namespace phpbu\App\Cli; | ||
|
||
/** | ||
* StatisticsTest | ||
* | ||
* @package phpbu | ||
* @subpackage tests | ||
* @author Sebastian Feldmann <[email protected]> | ||
* @copyright Sebastian Feldmann <[email protected]> | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
* @link http://www.phpbu.de/ | ||
* @since Class available since Release 5.1.2 | ||
*/ | ||
class StatisticsTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Tests Statistics::resourceUsage | ||
*/ | ||
public function testResourceUsage() | ||
{ | ||
$usage = Statistics::resourceUsage(); | ||
|
||
$this->assertTrue(strpos($usage, 'Time:') !== false); | ||
$this->assertTrue(strpos($usage, 'Memory:') !== false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
namespace phpbu\App\Util; | ||
|
||
/** | ||
* Time utility test | ||
* | ||
* @package phpbu | ||
* @subpackage tests | ||
* @author Sebastian Feldmann <[email protected]> | ||
* @copyright Sebastian Feldmann <[email protected]> | ||
* @license https://opensource.org/licenses/MIT The MIT License (MIT) | ||
* @link http://www.phpbu.de/ | ||
* @since Class available since Release 5.1.2 | ||
*/ | ||
class TimeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* Tests Time::timeSinceExecutionStart | ||
* | ||
* @expectedException \RuntimeException | ||
*/ | ||
public function testTimeSinceExecutionStartFail() | ||
{ | ||
$SERVER = $_SERVER; | ||
unset($_SERVER['REQUEST_TIME_FLOAT']); | ||
unset($_SERVER['REQUEST_TIME']); | ||
|
||
try { | ||
$time = Time::timeSinceExecutionStart(); | ||
} catch (\Exception $e) { | ||
$_SERVER = $SERVER; | ||
throw $e; | ||
} | ||
|
||
$this->assertFalse($time); | ||
} | ||
|
||
/** | ||
* Tests Time::timeSinceExecutionStart | ||
*/ | ||
public function testTimeSinceExecutionStartFloat() | ||
{ | ||
$SERVER = $_SERVER; | ||
$_SERVER['REQUEST_TIME_FLOAT'] = microtime(true) - 60; | ||
$time = Time::timeSinceExecutionStart(); | ||
$this->assertEquals(60, floor($time)); | ||
$_SERVER = $SERVER; | ||
} | ||
|
||
/** | ||
* Tests Time::timeSinceExecutionStart | ||
*/ | ||
public function testTimeSinceExecutionStart() | ||
{ | ||
$SERVER = $_SERVER; | ||
unset($_SERVER['REQUEST_TIME_FLOAT']); | ||
$_SERVER['REQUEST_TIME'] = time() - 60; | ||
$time = Time::timeSinceExecutionStart(); | ||
$this->assertEquals(60, floor($time)); | ||
$_SERVER = $SERVER; | ||
} | ||
|
||
/** | ||
* Tests Time::formatTime | ||
*/ | ||
public function testFormatTime() | ||
{ | ||
$this->assertEquals('37 seconds', Time::formatTime(37)); | ||
$this->assertEquals('1 hour 1 second', Time::formatTime(3601)); | ||
$this->assertEquals('1 hour 1 minute 1 second', Time::formatTime(3661)); | ||
$this->assertEquals('2 hours 2 minutes 2 seconds', Time::formatTime(7322)); | ||
$this->assertEquals('1 hour', Time::formatTime(3600)); | ||
$this->assertEquals('1 minute', Time::formatTime(60)); | ||
$this->assertEquals('1 second', Time::formatTime(1)); | ||
} | ||
} |