#!/usr/bin/perl
#
# Adam Pribyl, covex@ahoj.fsik.cvut.cz
# Petr Simandl, www.simandl.cz


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

print "<HTML><HEAD><META HTTP-EQUIV=Pragma CONTENT=no-cache></HEAD><BODY>\n";

#nocache!


$kb = 1024;
$nkba = 20;		# size of a transfer test file
$nkbb = 50;		# size of b transfer test file
$nkbc = 100;		# size of c transfer test file
$nkbd = 300;		# size of d transfer test file
$nkbe = 500;		# size of e transfer test file

$svrtimeout = 30; 	# httpd server timeout in seconds

$testtime = 10;		#maximum test time

$lospd = $nkba/$svrtimeout;

print "<center>\nIf no speed information is given then either your connection is too slow (< $lospd kiB/s) or you are experiencing signal dropouts. <br>\n";

print "Sending";

$tm1 = time;

for ($f = 1; $f <= $nkba; $f++) {
    print "<!--";
    for ($g = 1; $g <= ($kb/2-9) ; $g++) { print "aa"; }
    print "-->. ";
}
$tm2 = time;
$nkb = $nkba;
print "$nkb";
print "kiB";

if ( ($tm2-$tm1) < $testtime )
{
for ($f = 1; $f <= $nkbb; $f++) {
    print "<!--";
    for ($g = 1; $g <= ($kb/2-9) ; $g++) { print "bb"; }
    print "-->. ";
}
$tm2 = time;
$nkb = $nkb + $nkbb;
print "$nkb";
print "kiB";
}

if ( ($tm2-$tm1) < $testtime )
{
for ($f = 1; $f <= $nkbc; $f++) {
    print "<!--";
    for ($g = 1; $g <= ($kb/2-9) ; $g++) { print "cc"; }
    print "-->. ";
}
$tm2 = time;
$nkb = $nkb + $nkbc;
print "$nkb";
print "kiB";
}

if ( ($tm2-$tm1) < $testtime )
{
for ($f = 1; $f <= $nkbd; $f++) {
    print "<!--";
    for ($g = 1; $g <= ($kb/2-9) ; $g++) { print "dd"; }
    print "-->. ";
}
$tm2 = time;
$nkb = $nkb + $nkbd;
print "$nkb";
print "kiB";
}

if ( ($tm2-$tm1) < $testtime )
{
for ($f = 1; $f <= $nkbe; $f++) {
    print "<!--";
    for ($g = 1; $g <= ($kb/2-9) ; $g++) { print "ee"; }
    print "-->. ";
}
$tm2 = time;
$nkb = $nkb + $nkbe;
print "$nkb";
print "kiB";
}

print "<br>\n";

$adresa   = "$ENV{'REMOTE_ADDR'}";
$server   = "$ENV{'SERVER_NAME'}";

print "\n<P>";
$time = ($tm2-$tm1);
printf "Sent $nkb kiB from $server to $adresa in $time seconds<br>\n";
print "Approximate download bandwidth speed of your connection is: ";
#to not divide accidentaly by zero we accept inacurrancy of 0.001s
$speed = $nkb*$kb/($tm2-$tm1+0.001);
$speedkb = $speed/$kb;
$speedkbps = 8*$speed/$kb;
printf "%.2f B/s = %.2f kiB/s = %.2f kib/s\n", $speed, $speedkb, $speedkbps;
$date = `date`;
print "<BR>$date\n<BR>";
print "<A HREF=\"http://bbs.cvut.cz/~covex/czfree/index.html\#bander\">Bander speed test</A>\n";

print "</center></BODY></HTML>";
