From 0354b9352285323618e9d115838cea663d78d97d Mon Sep 17 00:00:00 2001 From: Finlay Beaton Date: Fri, 5 Aug 2016 11:35:00 -0400 Subject: [PATCH] - added since success, since fail to each ping - added total time, success time, fail time, fail% time --- src/Command/PingCommand.php | 126 ++++++++++++++++++++++++++++++++---- 1 file changed, 115 insertions(+), 11 deletions(-) diff --git a/src/Command/PingCommand.php b/src/Command/PingCommand.php index 57c4b99..ea8df94 100644 --- a/src/Command/PingCommand.php +++ b/src/Command/PingCommand.php @@ -13,6 +13,12 @@ abstract class PingCommand extends Command { + /** + * @var float + * @since 2016-08-04 + */ + protected $badTime = 0.0; + /** * @var string * @since 2016-08-04 @@ -43,6 +49,12 @@ abstract class PingCommand extends Command */ protected $driver = null; + /** + * @var float + * @since 2016-08-05 + */ + protected $goodTime = 0.0; + /** * @var InputInterface * @since 2016-08-04 @@ -76,6 +88,18 @@ abstract class PingCommand extends Command */ protected $user = 'root'; + /** + * @var float + * @since 2016-08-05 + */ + protected $sinceBad = null; + + /** + * @var float + * @since 2016-08-05 + */ + protected $sinceGood = null; + /** * @var float * @since 2016-08-04 @@ -100,12 +124,18 @@ abstract class PingCommand extends Command */ protected $stopTime = 0.0; - /** - * @return void - * @since 2016-08-04 - * @throws \RuntimeException Driver must be specified in configure(). + * @var float + * @since 2016-08-05 */ + protected $totalTime = null; + + +/** + * @return void + * @since 2016-08-04 + * @throws \RuntimeException Driver must be specified in configure(). + */ protected function configure() { if ($this->driver === null) { @@ -206,7 +236,13 @@ protected function execute(InputInterface $input, OutputInterface $output) \pcntl_signal(SIGINT, [$this, 'handleSigint']); } + while ($iterations !== 0) { + $now = microtime(true); + if ($this->totalTime === null) { + $this->totalTime = $now; + } + if ($signals === true) { pcntl_signal_dispatch(); } @@ -218,11 +254,31 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->statsFailures++; } + if ($ret === true) { + if ($this->sinceBad !== null) { + $this->badTime = ($this->badTime + ($now - $this->sinceBad)); + $this->sinceBad = null; + } + + if ($this->sinceGood === null) { + $this->sinceGood = $now; + } + } else { + if ($this->sinceGood !== null) { + $this->goodTime = ($this->goodTime + ($now - $this->sinceGood)); + $this->sinceGood = null; + } + + if ($this->sinceBad === null) { + $this->sinceBad = $now; + } + } + $iterations--; if ($iterations !== 0) { usleep($delay); } - } + }//end while $this->stats($input, $output); @@ -314,7 +370,13 @@ protected function connect(InputInterface $input, OutputInterface $output) $this->connected = true; } catch (\PDOException $e) { $this->stopTime = microtime(true); - $this->writeReply('connection failed: '.rtrim($e->getMessage(), PHP_EOL), $input, $output); + + // No connection could be made because the target machine actively refused it. + if ($e->getCode() === 2002) { + $this->writeReply('connection refused.', $input, $output); + } else { + $this->writeReply('connection failed: '.rtrim($e->getMessage(), PHP_EOL.'.').'.', $input, $output); + } $this->dbh = null; $this->connected = false; return false; @@ -352,15 +414,41 @@ protected function handleSigint() */ protected function stats(InputInterface $input, OutputInterface $output) { + $now = microtime(true); + + if ($this->sinceBad !== null) { + $this->badTime = ($this->badTime + ($now - $this->sinceBad)); + $this->sinceBad = null; + } + + if ($this->sinceGood !== null) { + $this->goodTime = ($this->goodTime + ($now - $this->sinceGood)); + $this->sinceGood = null; + } + $success = ($this->statsIterations - $this->statsFailures); $failPercent = round(($this->statsFailures / $this->statsIterations * 100.0)); + $totalTime = ($now - $this->totalTime); + $totalTimeS = round($totalTime, 4); + $goodTimeS = round($this->goodTime, 4); + $badTimeS = round($this->badTime, 4); + if ($totalTime > 0.0) { + $badTimeP = round(($this->badTime / $totalTime * 100)); + } else { + $badTimeP = 0.0; + } + $output->writeln([ '--- '.$input->getOption('host').':'.$input->getOption('port') .' database ping statistics ---', $this->statsIterations.' tries, ' .$success.' successes, ' .$this->statsFailures.' failures, ' - .$failPercent.'% fail', + .$failPercent.'% fail tries', + $totalTimeS.'s time, ' + .$goodTimeS.'s success, ' + .$badTimeS.'s fail, ' + .$badTimeP.'% fail time', ]); }//end stats() @@ -380,11 +468,11 @@ protected function queryCheck(InputInterface $input, OutputInterface $output) * @return float elapsed time * @since 2016-08-04 */ - protected function time() + protected function execTime() { - $time = round(($this->stopTime - $this->startTime), 4); + $time = round(($this->stopTime - $this->startTime) * 100.0); return $time; - }//end time() + }//end execTime() /** @@ -396,7 +484,23 @@ protected function time() */ protected function writeReply($msg, InputInterface $input, OutputInterface $output) { - $msg = 'from '.$input->getOption('host').':'.$input->getOption('port').': '.$msg.' time='.$this->time().' ms'; + if ($this->sinceGood !== null) { + $sinceGood = round((microtime(true) - $this->sinceGood), 4); + } else { + $sinceGood = 0.0; + } + + if ($this->sinceBad !== null) { + $sinceBad = round((microtime(true) - $this->sinceBad), 4); + } else { + $sinceBad = 0.0; + } + $msg = 'from '.$input->getOption('host').':'.$input->getOption('port').': ' + .$msg + .' delay='.$input->getOption('delay').'ms,' + .' exec='.$this->execTime().'ms,' + .' since success='.$sinceGood.'s,' + .' since fail='.$sinceBad.'s'; $output->writeln($msg); }//end writeReply() }//end class