From d8f6fee0b979e29ba04ec78982b251e27501f47e Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 16 Nov 2015 09:45:35 -0500 Subject: [PATCH] add debug mode --- retailcrm/app.php | 2 +- retailcrm/src/Components/Command.php | 15 ++++++++- retailcrm/src/Helpers/DebugHelper.php | 48 +++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 retailcrm/src/Helpers/DebugHelper.php diff --git a/retailcrm/app.php b/retailcrm/app.php index 15bd6ee..7583def 100644 --- a/retailcrm/app.php +++ b/retailcrm/app.php @@ -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); diff --git a/retailcrm/src/Components/Command.php b/retailcrm/src/Components/Command.php index b4d9539..c4b28aa 100644 --- a/retailcrm/src/Components/Command.php +++ b/retailcrm/src/Components/Command.php @@ -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() diff --git a/retailcrm/src/Helpers/DebugHelper.php b/retailcrm/src/Helpers/DebugHelper.php new file mode 100644 index 0000000..8c91eef --- /dev/null +++ b/retailcrm/src/Helpers/DebugHelper.php @@ -0,0 +1,48 @@ +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); + } +} \ No newline at end of file