#!/usr/bin/perl -w

#
## Copyright (C) 2010 KARASZI Istvan <http://raszi.hu/>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; version 2 dated June,
## 1991.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##
#

use strict;
use Redis;
use Time::HiRes qw( gettimeofday tv_interval );

my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $PORT = exists $ENV{'port'} ? $ENV{'port'} : 6379;
my $PASSWORD = exists $ENV{'password'} ? $ENV{'password'} : undef ;

my $server = "$HOST:$PORT";
my $r = Redis->new( server => $server, password => $PASSWORD );

my $config = ( defined $ARGV[0] and $ARGV[0] eq "config" );

sub get_time {
    my ($times, $func) = @_;
    my $start = [ gettimeofday ];

    for (my $i = 0; $i < $times; $i++) {
        $func->();
    }

    return sprintf("%f", tv_interval($start) / $times);
}


if ( $config ) {
    print "graph_title Response time\n";
    print "graph_vlabel Response time\n";
    print "ping.label PING time\n";
    print "get.label GET time\n";
    print "set.label SET time\n";
    print "graph_category search\n";
    exit 0;
}

my $key = "test-key";
my $times = 20;

print "ping.value " . &get_time( $times, sub { $r->ping(); }) . "\n";
print "set.value " . &get_time( $times, sub { $r->set($key => 'SET ' . $key . ' 1'); }) . "\n";
print "get.value " . &get_time( $times, sub { $r->get($key); }) . "\n";

# vim: ft=perl ai ts=4 sw=4 et:
