diff --git a/build.xml b/build.xml index 613139ce..b71b8157 100644 --- a/build.xml +++ b/build.xml @@ -385,15 +385,6 @@ - - - - - - - - - diff --git a/composer.json b/composer.json index e64b47ac..e6876117 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Cli/Statistics.php b/src/Cli/Statistics.php new file mode 100644 index 00000000..2e56def2 --- /dev/null +++ b/src/Cli/Statistics.php @@ -0,0 +1,30 @@ + + * @copyright Sebastian Feldmann + * @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 + ); + } +} diff --git a/src/Log/Mail.php b/src/Log/Mail.php index 03f7a4a5..cc834f93 100644 --- a/src/Log/Mail.php +++ b/src/Log/Mail.php @@ -1,6 +1,7 @@ ' . PHP_Timer::resourceUsage() . '

' . + return '

' . Statistics::resourceUsage() . '

' . ''; } } diff --git a/src/Result/PrinterCli.php b/src/Result/PrinterCli.php index ca44a67d..401b8649 100644 --- a/src/Result/PrinterCli.php +++ b/src/Result/PrinterCli.php @@ -1,11 +1,11 @@ write(PHP_Timer::resourceUsage() . PHP_EOL . PHP_EOL); + $this->write(Statistics::resourceUsage() . PHP_EOL . PHP_EOL); } /** diff --git a/src/Runner/Simulate.php b/src/Runner/Simulate.php index f66540b5..46671ec2 100644 --- a/src/Runner/Simulate.php +++ b/src/Runner/Simulate.php @@ -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); diff --git a/src/Util/Time.php b/src/Util/Time.php new file mode 100644 index 00000000..0b61416b --- /dev/null +++ b/src/Util/Time.php @@ -0,0 +1,66 @@ + + * @copyright Sebastian Feldmann + * @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); + } +} diff --git a/tests/phpbu/Cli/StatisticsTest.php b/tests/phpbu/Cli/StatisticsTest.php new file mode 100644 index 00000000..6a18ec24 --- /dev/null +++ b/tests/phpbu/Cli/StatisticsTest.php @@ -0,0 +1,27 @@ + + * @copyright Sebastian Feldmann + * @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); + } +} diff --git a/tests/phpbu/Util/TimeTest.php b/tests/phpbu/Util/TimeTest.php new file mode 100644 index 00000000..c44e80a7 --- /dev/null +++ b/tests/phpbu/Util/TimeTest.php @@ -0,0 +1,76 @@ + + * @copyright Sebastian Feldmann + * @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)); + } +}