#!/usr/local/bin/perl
use IO::File;

$| = 1;

$trigger   = 6667;
@clients   = ( "grunt", "eb" );
$trials    = 5;
@targets   = ( "soco.lcs.mit.edu" );
@durations = ( 30 );
@windows   = (  10 );
$port      = 8443;

foreach $target ( @targets ) {
    foreach $duration ( @durations ) {
	foreach $window ( @windows ) {
	    print STDOUT "Duration: $duration sec  Concurrency: "
		. "$window  Trials: $trials\n";

	    print STDOUT "Name      bytes        usec       MB/s" 
		. "   Succ   Attm     Succ     Attm\n";
	    print STDOUT "                                      " 
		. "  plays  plays  plays/s  plays/s\n";

	    for ($trial = 0; $trial < $trials; $trial++ ) {
		foreach $client ( @clients ) {
		    $fh{$client} = new IO::Handle;
		    open ($fh{$client},
			  "ssh -f -x $client "
			  . "/tmp/maxssl_cps "
			  . "-n $window -t $duration -p $trigger -r 2 "
			  . "-l $client $target $port |")
			|| die "Can't fork: $!";
		}
		
		
		sleep 2;
		system "/disk/pu0/fubob/build/devel/trigger $trigger";
		sleep $duration + 1;
		
		foreach $client ( @clients ) {
		    my $fh = $fh{$client};
		    while (<$fh>) {
			print STDOUT;
		    }
		    close ($fh{$client});
		    
		  AGAIN:
		    $res = `ssh -x $client netstat -n -a \| grep TIME_WAIT \| wc -l`;
		    $res =~ s/ //g;
		    if ($res > 400) {
#		    print STDOUT "TIME_WAIT on $client: $res\n";
			sleep 5;
			goto AGAIN;
		    }
		}

	    }
	}
    }
}




