Created
June 28, 2018 17:22
-
-
Save camflan/4fee63320d30514bb5f48158f42ed764 to your computer and use it in GitHub Desktop.
Debugging class for mysqli connections in php. Lets you mark places in the code that you'd like statistics about. Place many markers, and see the stats differentials between them, as well as totals for the entire run.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* QueryCounter | |
* A class useful for debugging mysqli connections | |
*/ | |
class QueryCounter { | |
public $connection; | |
private $stats = []; | |
/** | |
* constructor | |
* | |
* Requires a reference to your mysqli connection and optionally | |
* a boolean which will automatically start the stats collection | |
* once created | |
* | |
* @param mysqli_connection & $connection | |
* @param boolean $start | |
*/ | |
public function __construct(&$connection, $start=False) { | |
$this->connection = $connection; | |
if($start) { | |
$this->start(); | |
} | |
} | |
private function addStats($tag) { | |
array_push($this->stats, [ | |
"tag" => $tag, | |
"timestamp" => microtime(true), | |
"data" => mysqli_get_connection_stats($this->connection) | |
]); | |
} | |
/** | |
* getStats | |
* Returns a StatsDiff, which is a comparison of our datasets, | |
* in sequence, as well as the elapsed php time in seconds. | |
* Optionally, you can request a subset of stats by array of keys | |
* | |
* @param string or string[] $selectStats | |
* @return void | |
*/ | |
public function getStats($selectStats = null) { | |
$collectedStats = []; | |
$statsCount = sizeof($this->stats); | |
for($i = 1; $i < $statsCount; $i++) { | |
$start = $this->stats[$i - 1]; | |
$end = $this->stats[$i]; | |
$collectedStats[] = $this->diff($start, $end, $selectStats); | |
} | |
if($statsCount > 2) { | |
return [ | |
"intervals" => $collectedStats, | |
"totals" => $this->diff( | |
$this->stats[0], | |
$this->stats[$statsCount -1], | |
$selectStats | |
) | |
]; | |
} | |
return [ | |
"totals" => $collectedStats | |
]; | |
} | |
/** | |
* diff | |
* Compares stats from the start and latest dataset | |
* | |
* @param StatsArray $start | |
* @param StatsArray $end | |
* @return StatsDiff | |
*/ | |
private function diff($start, $end, $selectStats=null) { | |
$startData = $start["data"]; | |
$endData = $end["data"]; | |
$stats = []; | |
if(isset($selectStats)) { | |
if(!is_array($selectStats)) { | |
$selectStats = [$selectStats]; | |
} | |
$endData = array_intersect_key( | |
$endData, | |
array_flip($selectStats) | |
); | |
} | |
foreach($endData as $key => $value) { | |
if(!array_key_exists($key, $startData)) { | |
continue; | |
} | |
try { | |
$val = $value - $startData[$key]; | |
} catch(\Exception $e) { | |
continue; | |
} | |
$stats[$key] = $val; | |
} | |
return [ | |
"from" => $start["tag"], | |
"to" => $end["tag"], | |
"elapsed_seconds" => $end["timestamp"] - $start["timestamp"], | |
"data" => $stats | |
]; | |
} | |
/** | |
* mark | |
* Mark current stats with tag, for stats later | |
* | |
* @param string $tag | |
* @return void | |
*/ | |
public function mark($tag) { | |
$this->addStats($tag); | |
return $this; | |
} | |
/** | |
* start | |
* Clears previous stats and grabs a starting dataset | |
* | |
* @param string $tag | |
* @return void | |
*/ | |
public function start() { | |
$this->stats = []; | |
return $this->mark("start"); | |
} | |
public function end($selectStats=null) { | |
$this->mark("end"); | |
return $this->getStats($selectStats); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment