1748198 Members
2570 Online
108759 Solutions
New Discussion юеВ

PERL - find MAX, MIN

 
Rahul_13
Advisor

PERL - find MAX, MIN

Hi,

I am written a PERL script which calculates the average response time of a system.

Now, I need to find the MAX time and the MIN time from the response times.

Also, I need to find the 10% of the worst calls ( the calls which have taken the maximum times) and also find the maximum and minimum time among the worst 10% calls.

Can anyone please help me.

Thanks,
Rahul
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor

Re: PERL - find MAX, MIN

Hi Rahul:

Here's a suggestion.

One method of attack would be to collect your response time samples into a list, array or hash, depending on your processing requirements. Having counted the total samples as you collect them, you can find the 10% (0.1 N) of the samples (N) that represent the worst response times by taking the first 10% of a list of response times sorted in descending order (longest/worst to shortest/best).

Finding a minimum and maximum is simple too.
Assuming that @ary contains your response times:

$min=2**32-1;
$max=0;
for $t (@ary) {$min=$t if $t < $min};
for $t (@ary) {$max=$t if $t > $max};

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: PERL - find MAX, MIN

SMOP...

----- First create a sample data file:

# perl -e 'while ($i++<70) {printf "%05d %5f\n", $i, rand}' > x

------ Perl script x.p
$w = 10;
# read times into array keyed by call

while (<>){
($i,$x)=split;
$x{$i}=$x;
}

# create list of calls ordered by time, and count

@o = sort {$x{$a} <=> $x{$b}} keys %x;
$n = scalar @o;

# report result

print "There were $n calls, min time = $x{@o[0]}, max time = $x{@o[$n-1]}\nThe worst $w% calls were:\n";
$w *= $n/100;
$j = int($n - $w);
while ( $j < $n ) {
$i=@o[$j++];
print "$j $i $x{$i}\n";
}

----- sample execute
# perl x.p x
There were 70 calls, min time = 0.002133, max time = 0.990953
The worst 10% calls were:
64 00036 0.933632
65 00030 0.935374
66 00067 0.935947
67 00001 0.956462
68 00019 0.967803
69 00052 0.969352
70 00031 0.990953


Hein.