#!/usr/bin/env perl
#
# FILE:
# pagestats
#
# FUNCTION:
# compute various averages from multiuser report files.
#
# USAGE:
# pagestats reports/*
# or
# cat * | pagestats
#
# HISTORY:
# Written Linas Vepstas May 1998
#
# --------------------------------------------------------
# --------------------------------------------------------


# Add a search path to find "timelocal.pl" and other required 
# bits and bytes on various oddly-installed machines.
# This is a hack, as the sysadmin really should install correctly, 
# but for now, this will hold water.
push (@INC, "/usr/local/lib/perl5/5.00501");
($inky = $0) =~ s/pagestats//;
push (@INC, $inky);

require "pagestats.pl";
# --------------------------------------------------------
# --------------------------------------------------------
# --------------------------------------------------------
# --------------------------------------------------------
# main

# Read the list of urls.  This is done by ripping the urls 
# out of one of the reports.
&geturlist;

# Print the url list to stdout. This is a handy cross-reference 
# for the user.
for ($i=0; $i<$numurls; $i++) {
	print "Info: $i $method[$i] $url[$i] \n";
}

if (0 >= $numurls) {
	die "Error: unable to read the list of URL's from the report files";
}
print ("-----------------------------------\n");

# Look for these statistics
# Basically, we'll digest these stats.
$statname[0] = "first_data_response_time";
$statname[1] = "response_times";
$statname[2] = " net_delay";
$statname[3] = " ssl_ovhd";
$statname[4] = "html_response_times";
$statname[5] = " html_net_delay";
$statname[6] = " html_ssl_ovhd";
$statname[7] = "connect_times";
$statname[8] = " tcp_connect_times";
$statname[9] = " ssl_connect_ovhd";
$statname[10] = " ssl_net_delay_connect";
$statname[11] = "header_delays";
$statname[12] = " ssl_header_ovhd";
$statname[13] = " ssl_net_delay_header";
$statname[14] = "transfer_times";
$statname[15] = " ssl_transfer_ovhd";
$statname[16] = " ssl_net_delay_transfer";
$statname[17] = "think_time";
$statname[18] = "request_time";

# Get the statistics
&getstats;
print "\n\n";

# Compute the mean and the stddev for each.
foreach $s (@statname) {
	foreach $j (@titles) {
		$cnt = $nobs {$s, $j };
		# if ssl is turned off, then the nobs will be zero
		# for some of the stats.  This will lead to a divide by zero...
		if (0 < $cnt) {
			$avgtime {$s, $j} = $meantime {$s, $j } / $cnt;
			$avg = $avgtime {$s, $j };
			$stdev = $msqtime {$s, $j } / $cnt; 
			$stdev -= $avg * $avg;
			# due to round-off errors in the printed figures, 
			# the computed mean-square can go negative.  
			# In that case, clamp it to zero.
			if (0 > $stdev) { $stdev = 0; }
			$stdevtime {$s, $j } = sqrt ( $stdev );
		}
	}
}

#
# Now round everything to milliseconds
#
foreach $s (@statname) {
	foreach $j (@titles) {
		$avgtime {$s, $j }   = &round ($avgtime {$s, $j }, 1000);
		$stdevtime {$s, $j } = &round ($stdevtime {$s, $j }, 1000);
		$mintime {$s, $j }   = &round ($mintime {$s, $j }, 1000);
		$maxtime {$s, $j }   = &round ($maxtime {$s, $j }, 1000);
	}
}

# Find the average think time for the session
$totalnobs = 0;
$thinky = 0;
$s = "think_time";

foreach $j (@titles) {
        if ($j =~ /Statistics Summary for Requests/) {
                $totalnobs += $nobs{$s, $j};
                $thinky += $meantime{$s, $j};
        }
}

$think_mean = $thinky / $totalnobs;

#      @## @###.### @###.### @###.### @####  @###.### @###.### @<<< @<<<<<<<<<<<<<<<<<<
print "      think  response response        fastest  slowest                 \n";
print "page  time   avg (secs) stddev  nobs  response response       request  \n";
print "------------------------------------------------------------------------------  \n";

format STDOUT=
@## @###.### @###.### @###.### @####  @###.### @###.### @<<< @<<<<<<<<<<<<<<<<<<
$i, $thinky, $meanie, $steady, $knob, $minnie, $maxie,  $m,  $u
.
# finally, condense per-page stats down to a single line
foreach $j (@titles) {
	if ($j =~ /Statistics Summary for Requests/) {
		$s = "response_times";

                $thinky = $think_mean;
                $meanie = $avgtime{$s,$j};
                $steady = $stdevtime{$s,$j};
                $knob = $nobs{$s,$j};
                $minnie = $mintime{$s,$j};
                $maxie  = $maxtime{$s,$j};
                @words = split (/ /, $j);
		$i = $words[4];
		$m = $method[$i];
                $u = $url[$i];


	write STDOUT;
	}
}

# ===================== end of file ========================
