Skip to content

Instantly share code, notes, and snippets.

@tcz
Created February 24, 2014 13:50
Show Gist options
  • Save tcz/9188760 to your computer and use it in GitHub Desktop.
Save tcz/9188760 to your computer and use it in GitHub Desktop.
Check MySQL queries per second periodically from PHP
<?php
$mysqli = new mysqli("db", "user", "pass", "db");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
while (true)
{
$results = $mysqli->query("show global status like 'Questions'")->fetch_array();
$questions_1 = $results[1] - 2;
$results = $mysqli->query("show global status like 'Uptime'")->fetch_array();
$uptime_1 = $results[1];
sleep(3);
$results = $mysqli->query("show global status like 'Questions'")->fetch_array();
$questions_2 = $results[1] - 2;
$results = $mysqli->query("show global status like 'Uptime'")->fetch_array();
$uptime_2 = $results[1];
echo "Q1:", $questions_1, "\n";
echo "U1:", $uptime_1, "\n";
echo "Q2:", $questions_2, "\n";
echo "U2:", $uptime_2, "\n";
echo "QPS:", ($questions_2-$questions_1)/($uptime_2-$uptime_1), "\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment