#!/usr/bin/perl -w use strict; $|++; use DBI (); use Statistics::Descriptive; ## BEGIN CONFIG ## my $DSN = 'dbi:mysql:stonehenge_httpd'; my $DB_AUTH = 'do_not:try_this_at_home'; my $DAYS = 2; ## END CONFIG ## my $dbh = DBI->connect($DSN, (split ':', $DB_AUTH), { RaiseError => 1 }); $dbh->do("SET OPTION SQL_BIG_TABLES = 1"); ## $dbh->trace(2); my $sth = $dbh->prepare(qq{ SELECT unix_timestamp(when), wall, bytes, cpuuser + cpusys + cpucuser + cpucsys FROM requests WHERE when >= DATE_SUB(NOW(), INTERVAL $DAYS DAY) ORDER BY when }); my $offset = time - 86400 * $DAYS; $sth->execute(); $sth->bind_columns(\my($when, $wall, $bytes, $cpu)); my @hits; my @bytes; my @cpu; while ($sth->fetch) { $when -= $offset; my $hits = 1 / ($wall + 1); $bytes *= $hits; $cpu *= $hits; for ($when..($when + $wall)) { $hits[$_] += $hits; $bytes[$_] += $bytes; $cpu[$_] += $cpu; } } $dbh->disconnect; ## fix the undef's defined $_ or $_ = 0 for @bytes; defined $_ or $_ = 0 for @hits; defined $_ or $_ = 0 for @cpu; if (0) { # dump raw data for (0..$#bytes) { printf "%30s %10.1f %4.1f %4.1f\n", scalar(localtime($offset+$_)), $bytes[$_], $hits[$_], $cpu[$_]; } } for my $pair ([bytes => \@bytes], [hits => \@hits], [cpu => \@cpu]) { my $stat = Statistics::Descriptive::Full->new; $stat->add_data(@{$pair->[1]}); for my $thing (qw(mean max)) { printf "%4s %5s/sec %10.3f\n", $thing, $pair->[0], $stat->$thing(); } }