add debug mode

This commit is contained in:
Dmitry Mamontov 2015-11-16 09:45:35 -05:00
parent 435081dbb6
commit d8f6fee0b9
3 changed files with 63 additions and 2 deletions

View File

@ -10,7 +10,7 @@ if (
require_once 'bootstrap.php';
$options = getopt('luce:m:p:r:h:');
$options = getopt('dluce:m:p:r:h:');
if (isset($options['e'])) {
$command = new Command($options);

View File

@ -10,6 +10,7 @@ class Command
private $limit;
private $update;
private $container;
private $debug;
public function __construct($arguments)
{
@ -22,6 +23,7 @@ class Command
$this->limit = isset($arguments['l']);
$this->update = isset($arguments['u']);
$this->custom = isset($arguments['c']);
$this->debug = isset($arguments['d']);
$this->container = Container::getInstance();
@ -40,9 +42,20 @@ class Command
return;
}
$debug = new DebugHelper();
if ($this->debug) {
$debug->write(sprintf('Start %s', ucfirst($this->run)));
}
$command = 'run' . ucfirst($this->run);
return $this->$command();
$result = $this->$command();
if ($this->debug) {
$debug->write(sprintf('End %s', ucfirst($this->run)));
}
return $result;
}
public function runDump()

View File

@ -0,0 +1,48 @@
<?php
class DebugHelper
{
private $baseMemoryUsage;
private $tusage;
private $rusage;
public function __construct()
{
$this->baseMemoryUsage = memory_get_usage(true);
$proc = getrusage();
$this->tusage = microtime(true);
$this->rusage = $proc['ru_utime.tv_sec'] * 1e6 + $proc['ru_utime.tv_usec'];
}
private function formatSize($size)
{
$postfix = array('b', 'Kb', 'Mb', 'Gb', 'Tb');
$position = 0;
while ($size >= 1024 && $position < 4) {
$size /= 1024;
$position++;
}
return sprintf('%s %s', round($size, 2), $postfix[$position]);
}
public function getMemoryUsage()
{
return $this->formatSize(memory_get_usage(true) - $this->baseMemoryUsage);
}
public function getCpuUsage()
{
$proc = getrusage();
$proc["ru_utime.tv_usec"] = ($proc["ru_utime.tv_sec"] * 1e6 + $proc["ru_utime.tv_usec"]) - $this->rusage;
$time = (microtime(true) - $this->tusage) * 1000000;
return $time > 0 ? sprintf("%01.2f", ($proc["ru_utime.tv_usec"] / $time) * 100) : '0.00';
}
public function write($string)
{
echo sprintf("%s\t%s\t%s%s", $string, $this->getCpuUsage(), $this->getMemoryUsage(), PHP_EOL);
}
}