mirror of
https://github.com/retailcrm/legacy.git
synced 2024-11-22 05:16:02 +03:00
add debug mode
This commit is contained in:
parent
435081dbb6
commit
d8f6fee0b9
@ -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);
|
||||
|
@ -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()
|
||||
|
48
retailcrm/src/Helpers/DebugHelper.php
Normal file
48
retailcrm/src/Helpers/DebugHelper.php
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user