#!/usr/bin/perl

if ($ARGV[0] eq "-graph") {
	system("$0 | pl -x11 -o tests.png -prefab lines data=- delim=tab x=1 y=4 err=5 xlbl=\"Agents\" ylbl=\"Time (min)\" title=\"\"");
	system("$0 | pl -eps -o tests.eps -prefab lines data=- delim=tab x=1 y=4 err=5 xlbl=\"Agents\" ylbl=\"Time (min)\" title=\"\"");
	exit;
}

for ($i = 1; $i <= 10; $i++) {
	print $i * 100;
	for ($j = 0; $j <= 1; $j++) {

		$total = 0;
		$count = 0;
		open (FH, "grep \"elapsed\" results*/*.$i.log|") || next;
		while (<FH>) {
			#44:26.30elapsed
			($value) = ($_ =~ /([\d:]+):\d+\.?\d*elapsed/);
			if ($value =~ /(\d+):(\d+)/) {
				$value = $1 * 60 + $2;
			}
			$value *= 60 * 1000;
			$total += $value;
			$count++;
		}
		close (FH);

		if ($j == 0) {
			$total = 100 * $i * 60 * 1000 / 5;
			$count = 1;
		}

		$average = $total / $count;

		$total = 0;
		open (FH, "grep \"elapsed\" results*/*.$i.log|") || next;
		while (<FH>) {
			($value) = ($_ =~ /([\d:]+):\d+\.?\d*elapsed/);
			if ($value =~ /(\d+):(\d+)/) {
				$value = $1 * 60 + $2;
			}
			$value *= 60 * 1000;
			$total += ($value - $average) * ($value - $average);
		}
		close (FH);
		if ($j == 0) {
			$total = 100 * $i * 60 * 1000 / 5;
			$count = 1;
		}
		$stddev = ($count > 1) ? sqrt($total / ($count - 1)) : 0;

		#print "$i\t$j\t$average\t$count\n";
		$average /= 1000 * 60;
		$stddev /= 1000 * 60;
		print "\t$average\t$stddev";
	}
	print "\n";

# pl -png -o tests.png -prefab lines data=- delim=tab x=1 y=2 y2=3 y3=4 yrange=0 xlbl=Sensors name="SPAM + SRTA" name2="SRTA" name3="Single commitment" ylbl="Goodness" title="Comparing different sensor allocation techniques"
# ./extractgood | pl -x11 -o tests.png -prefab lines data=- delim=tab x=1 y=2 err=3 y2=4 err2=5 y3=6 err3=7 yrange=0 xlbl=Sensors name="SPAM + SRTA" name2="SRTA" name3="Single commitment" ylbl="Goodness" title="Comparing different sensor allocation techniques"
}
