#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use CGI::Request;
use DhtmlURLTimingDataSet;
use strict;

my $request = new CGI::Request;
my $id = $request->param('id'); #XXX need to check for valid parameter id

print "Content-type: text/html\n\n";

print <<"ENDOFHTML";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <link rel="STYLESHEET" href="/style/black.css" type="text/css">
    <title>botbot Performance Server</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
    <div id="header">
      <span id="header-primary"><img src="/images/dhtmlpl-header.gif" alt=""></span>
      <span id="header-bestview"><img src="/images/conf.gif" alt=""></span>
      <div id="header-clear"></div>
    </div>
    <div id="allBodyText">
ENDOFHTML

#print "<p>See Notes at the bottom of this page for some details.</p>\n";
print "      <div id=\"ibenchResults\">\n";

my $rs = DhtmlURLTimingDataSet->new($id);

print  "Test id: $id\n";
print  "\nCombind Results:\n";
print  "Avg. Median: ", $rs->{avgmedian}, " msec\t\tMinimum: ", $rs->{minimum}, " msec\n";
print  "Average:     ", $rs->{average},   " msec\t\tMaximum: ", $rs->{maximum}, " msec\n";
#XXX print more info (test id, ua, start time, user, IP, etc.)


# dump some stats for tinderbox to snarf
#print "<script>\n";
#print "if (window.dump) dump('\\n_x_x_mozilla_page_load,", $rs->{avgmedian}, ',', $rs->{minimum}, ',', $rs->{maximum}, ',', $rs->{average}, "\\n\\n');\n";
#print "if (window.dump) dump('\\n_x_x_mozilla_test_id=", $id ,"\\n\\n');\n";
#print "</script>\n";

print "\nIndividual Page's Results:\n ID PATH                           AVG    MED    MAX    MIN  TIMES.....\n";

if ($request->param('sort'))
{
  print $rs->as_string_sorted();
}
else
{
  print $rs->as_string();
}

print "</div>\n";

# draw the chart sorted
#print "\n<p><img src='graph.pl?id=", $id, "' height='720' width='800'></p><br>\n";

#XXXjrgm hack alert
# write the single scalar avg-of-medians measure into a separate file for
# plotting iff this was logged in as basic-authentication for 'tw-*'

#my $authuser  = $request->cgi->var("REMOTE_USER");
#if ($authuser =~ /^tw-/)
#{
#  warn "authuser: $authuser";
#  my $ua     = $request->cgi->var("HTTP_USER_AGENT");
#  my $ipaddr = $request->cgi->var("REMOTE_ADDR");
#  my $datadir = "/var/ftp/pub/pageload-output";
#  my $datafile = $datadir . "/" . $authuser . ".dat";
#  my $rv = open (DATA, ">> $datafile");
#  if (!$rv)
#  {
#	  warn "Couldn't get output file $datafile: $!";
#  } else {
#    # '...0.9.2) Gecko/20010723 Netsc...'
#    $ua =~ m! Gecko/([0-9]+) ?!;
#    my $buildid = $1;
#    my ($mm, $dd, $yy);
#    $yy = substr($buildid, 2, 2);
#    $mm = substr($buildid, 4, 2);
#    $dd = substr($buildid, 6, 2);
#	  my @uncached = map { $_->[7] } @{$rs->{results}};
#	  my $uncached_avg = 0;
#    eval { # try/catch
#	    map { $uncached_avg += $_ } @uncached;
#	    $uncached_avg = $uncached_avg / scalar(@uncached); 
#	  };
#	  warn $@ if $@;
#    warn       "$mm/$dd/$yy\t", $rs->{avgmedian}, "\t", $uncached_avg, "\t\"$ua\"\t$ipaddr\t$id\n";
#    print DATA "$mm/$dd/$yy\t", $rs->{avgmedian}, "\t", $uncached_avg, "\t\"$ua\"\t$ipaddr\t$id\n";
#    close(DATA);
#  }
#}

#printEndNotes();

#exit;


#sub printEndNotes {
#    print <<"EndOfNotes";
#
#<hr>
#<p>
#<ol>
#<li>Times are in milliseconds.
#
#<li>AVG, MED, MIN and MAX are the average, median, maximum and
#minimum of the (non-NaN) test results for a given page.
#
#<li>If a page fails to fire the onload event within 30 seconds,
#the test for that page is "aborted": a different JS function kicks in,
#cleans up, and reports the time as "NaN". (The rest of the pages in
#the series should still be loaded normally after this).
#
#<li>The value for AVG reported for 'All Pages' is the average of
#the averages for all the pages loaded.
#
#<li>The value for MAX and MIN reported for 'All Pages' are the
#overall maximum and minimum for all pages loaded (keeping in mind that
#a load that never finishes is recorded as "NaN".)
#
#<li>The value for MED reported for 'All Pages' is the _average_ of
#the medians for all the pages loaded (i.e., it is not the median of
#the medians).
#
#</ol>
#
#</p>
#EndOfNotes
#}

print <<"ENDOFHTML";
    </div>

    <div id="end">
      <table border="0" cellspacing="0" cellpadding="0" width="100%" class="whiteTable"><tr><td>
      Email <a href="mailto:kerz\@netscape.com">Jason Kersey</a> to add things to this page.
      </td><td align=right><img src="/images/nfooter.gif" alt=""></td></tr></table>
    </div>
  </body>
</html>
ENDOFHTML

