#!/usr/athena/bin/perl

$arg = $ARGV[1];
$intensity = $ARGV[0];
$debug = 0;
print("Requiring\n");
require 'should.pl';            # Should and auxilliary routines
require 'log.pl';               # Logging routines & badlink
require 'analyze.pl';           # Analyze document.  get_* & is_real...
require 'queue.pl';             # Qeueuing routines
require 'url.pl';               # get_url and auxilliary stuff
$|=1;
print("Forking...\n");
for $i (1..$intensity) {
	if(!fork()){
	    $hammerstart = time();
	    &hammer($i, $intensity);
	}
}
for $i (1..$intensity) {
	wait;
}

sub hammer {
    local($demon, $hammers) = @_;
	print `date`;
    print("Hammer $demon started\n");
	$start = time();
    $uct = 1;
    while(1) {
	$ct=0;
	$url = $ARGV[$uct];
	$uct++;
	$uct = 1 if($uct > $#ARGV);
	($site, $path, $port) = &url'ParseURL($url);
#	print("Getting $url\n");
	($head, $body) = &get_url($url);
	$ct++;
	$size += length($body);
	@images = &get_images($body);
	for $image (@images){
		next if $image =~/nph/;
	    $fullimage = &url'ConstructURL($url, $image);
#	    print("Getting $fullimage\n");
	    ($head, $body) = &get_url($fullimage);
	    $size+= length($body);
	    $ct++;
	}
	$end = time();
	$diff = $end-$start;
#	    print("$url $mon/$day/$hms $diff $size\n");
	$tdif+=$diff;
	$hits++;
	if(time() > ($start+10)){
	    $ttdif += $tdif;
	    $thits +=$hits;
	    $cumhpm = int((60*$ct*$thits)/(time()-$hammerstart));
	    $hpm = int((60*$ct*$hits)/(time()-$start));

	    $tot = $hpm*$hammers;
	    $cumtot = $cumhpm*$hammers;
	    print `date`;
	    print("Hammer $demon is doing $hpm hits per minute ($tot for all hammers)\n");
	    print("Hammer $demon is doing average of $cumhpm per min ($cumtot for all hammers)\n");
	    print("Average response time of ", $tdif/$hits, " Cumulative: ", 
$ttdif/$thits, "\n");
	    $start = time();
	    $hits=0;
	    $tdif = 0;
	}
    }
}
