package Smokeping::probes::FileRead;

=head1 301 Moved Permanently

This is a Smokeping probe module. Please use the command 

C<smokeping -man Smokeping::probes::FileRead>

to view the documentation or the command

C<smokeping -makepod Smokeping::probes::FileRead>

to generate the POD document.

=cut

use strict;
use base qw(Smokeping::probes::basefork);
use IO::File;
use Time::HiRes qw(gettimeofday tv_interval);
use Carp;

sub pod_hash {
      return {
              name => <<DOC,
Smokeping::probes::FileRead - FileRead Probe for SmokePing
DOC
              description => <<DOC,
Times how long it takes to read a random 4k from a 40mb file
DOC
              authors => <<'DOC',
Daniel Drown <dan-fileping AT drown.org>
DOC
	}
}

sub new($$$)
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = $class->SUPER::new(@_);

    return $self;
}

sub ProbeDesc($){
    my $self = shift;
    return "4k read operation";
}

sub pingone ($){
    my $self = shift;
    my $target = shift;

    my(@times);
    for(my $i = 0; $i < $self->pings($target); $i++) {
      my $start = [gettimeofday];
      my $fh = new IO::File "/usr/lib/smokeping/40mb", "r";
      $fh->sysseek(4096*int(rand(10000)),0);
      $fh->sysread(my $data, 4096);
      if(length($data) != 4096) {
	push(@times,undef);
      } else {
	push(@times,tv_interval($start));
      }
      sleep(1);
    }

    return sort {$a <=> $b} @times;
}

sub probevars {
	my $class = shift;
	return $class->_makevars($class->SUPER::probevars, {
		_mandatory => [ ],
	});
}

1;
