Skip to content

Commit

Permalink
Merge branch 'master' into 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Feldmann committed Jul 29, 2018
2 parents b90665e + 4415519 commit 3c88627
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 16 deletions.
9 changes: 0 additions & 9 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -385,15 +385,6 @@
</fileset>
</copy>

<!-- PHPUNIT LIBS -->
<copy file="${basedir}/vendor/phpunit/php-timer/LICENSE" tofile="${basedir}/build/phar/lib/php-timer/LICENSE"/>
<copy todir="${basedir}/build/phar/lib/php-timer">
<fileset dir="${basedir}/vendor/phpunit/php-timer/src">
<include name="**/*.php"/>
<exclude name="**/Autoload.*"/>
</fileset>
</copy>

<copy file="${basedir}/vendor/sebastian/environment/LICENSE"
tofile="${basedir}/build/phar/lib/sebastian-environment/LICENSE"/>
<copy todir="${basedir}/build/phar/lib/sebastian-environment">
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@
"ext-dom": "*",
"ext-json": "*",
"ext-spl": "*",
"phpunit/php-timer": "~1.0",
"sebastian/environment": "~1.1",
"sebastian/version": "~1.0",
"sebastian/environment": "~1.1|~2.0|~3.0",
"sebastian/version": "~1.0|~2.0",
"sebastianfeldmann/cli": "~2.0",
"sebastianfeldmann/ftp": "~0.9",
"swiftmailer/swiftmailer": "~5.3|~6.0",
Expand Down
30 changes: 30 additions & 0 deletions src/Cli/Statistics.php
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
);
}
}
4 changes: 2 additions & 2 deletions src/Log/Mail.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
namespace phpbu\App\Log;

use phpbu\App\Cli\Statistics;
use phpbu\App\Exception;
use phpbu\App\Event;
use phpbu\App\Listener;
use phpbu\App\Result;
use phpbu\App\Log\MailTemplate as TPL;
use phpbu\App\Util\Arr;
use phpbu\App\Util\Str;
use PHP_Timer;
use Swift_Mailer;
use Swift_Message;

Expand Down Expand Up @@ -549,7 +549,7 @@ protected function getInfoHtml(Result $result)
*/
protected function getFooterHtml()
{
return '<p ' . TPL::getSnippet('sStats') . '>' . PHP_Timer::resourceUsage() . '</p>' .
return '<p ' . TPL::getSnippet('sStats') . '>' . Statistics::resourceUsage() . '</p>' .
'</td></tr></table>';
}
}
4 changes: 2 additions & 2 deletions src/Result/PrinterCli.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace phpbu\App\Result;

use phpbu\App\Cli\Statistics;
use phpbu\App\Event;
use phpbu\App\Listener;
use phpbu\App\Result;
use phpbu\App\Util;
use PHP_Timer;
use SebastianBergmann\Environment\Console;
use SebastianBergmann\Environment\Runtime;

Expand Down Expand Up @@ -440,7 +440,7 @@ public function printResult(Result $result)
*/
protected function printHeader()
{
$this->write(PHP_Timer::resourceUsage() . PHP_EOL . PHP_EOL);
$this->write(Statistics::resourceUsage() . PHP_EOL . PHP_EOL);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Runner/Simulate.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function run(Configuration $configuration) : Result
}
// setup target and collector
$target = $this->factory->createTarget($backup->getTarget());
$target->setSize(20000000);
$collector = new Local($target);
$collector->setSimulation(true);

Expand Down
66 changes: 66 additions & 0 deletions src/Util/Time.php
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);
}
}
27 changes: 27 additions & 0 deletions tests/phpbu/Cli/StatisticsTest.php
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);
}
}
76 changes: 76 additions & 0 deletions tests/phpbu/Util/TimeTest.php
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));
}
}

0 comments on commit 3c88627

Please sign in to comment.