Queries per second

It’s obviously very important to know how your application and DBs are performing during business as usual. Otherwise you might find yourself in a situation where you’re facing performance degradation, and you have no clue what it’s down to. One important aspect of this is knowing how many queries per second your DBs are processing. If you know that it normally does 8000 queries per second, and you all of a sudden see an average of 12000, you know where to start looking. But if you don’t know what your normal load is, 12000 might strike you as normal and you initially overlook the cause for the slow-down.

I’ve written a perl script which gathers the average amount of queries per second over a given period of time. For the heck of it, I’ve written it in Perl, and I make no claims of being extremely knowledgeable in this language, so you might be horrified at what you’re about to see:

#!/usr/bin/perl
use POSIX;
use integer;
use DBI;
$user="root";
$pass="";
@hosts = qw \
10.0.0.201
10.0.0.202
10.0.0.203
10.0.0.204
10.0.0.205
\;
###################################################################
sub getQueries {
      foreach (@hosts) {
        $db="test";
        $cs="dbi:mysql:$db;$_";
        $dbh = DBI->connect($cs,$user,$pass);
        $sth = $dbh->prepare("SHOW STATUS LIKE 'questions'");
        $sth->execute();
        $sth->bind_columns(\$var, \$queries);
        $sth->fetch();
        chomp($queries);
        push(@queries, $queries);
        $sth->finish();
        $dbh->disconnect;
      }
}
$interval = 5;
getQueries();
@queries2 = @queries;
@queries = ();
sleep $interval;
getQueries();
$i = 0;
$tot = 0;
print "==========================================\n";
while ($i != $#queries+1){
     $res = ($queries[$i]-$queries2[$i])/$interval;
     $tot = $tot + ceil($res);
     print @hosts[$i] . " = ". $res."\n";
     $i++;
}
print "Total: " . $tot;
print "\n==========================================\n";

 You might want to change the user, pass and IP addresses in the top of the code. The output of this code looks like this:

# perl qps.pl
==========================================
10.0.0.201 = 1752
10.0.0.202 = 1908
10.0.0.203 = 1719
10.0.0.204 = 2031
10.0.0.205 = 1809
Total: 9219
==========================================

I might post a bit later on other tools you can use to get to know your setup a bit better.

About Erik Ljungstrom

I'm Erik Ljungstrom and I work in a datacenter as a technical team leader. In this blog I will mostly jot down things I consider noteworthy things I encounter in my work. For more information, please see http://northernmost.org
This entry was posted in MySQL and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>