Skip to content

Commit

Permalink
- added since success, since fail to each ping
Browse files Browse the repository at this point in the history
- added total time, success time, fail time, fail% time
  • Loading branch information
ofbeaton committed Aug 5, 2016
1 parent ce53246 commit 0354b93
Showing 1 changed file with 115 additions and 11 deletions.
126 changes: 115 additions & 11 deletions src/Command/PingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()

Expand All @@ -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()


/**
Expand All @@ -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

0 comments on commit 0354b93

Please sign in to comment.