define('BADWEIGHT',10);
define('GOODWEIGHT',1);
define('BADMULTIPLIER',150);
define('BADTIMEOUT',10);
define('SLOWDEVIATIONS',2);
$filename = 'C:\Program Files\ICW\home\nagiosremote\selenium.txt';
 
$execCommand="phpunit.bat selenium.php";
$trial=0;
function logToFile($somecontent)
{
	global $filename;
	if (is_writable($filename)) 
	{
		if (!$handle = fopen($filename, 'a')) 
		{
			 echo "Cannot open file ($filename)";
			 exit;
		}
		if (fwrite($handle, $somecontent) === FALSE) 
		{
			echo "Cannot write to file ($filename)";
			exit;
		}
		fclose($handle);
	} 
	else 
	{
		echo "The file $filename is not writable";
	}
}
 
function sd_square($x, $mean) { return pow($x - $mean,2); }
 
function sd($array) 
{
   return sqrt(array_sum(array_map("sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)-1) );
}
 
function getStats($trials)
{	
	foreach($trials as $trial)
	{
		$sum+=$trial['time'];
		$timeArray[]=$trial['time'];
	}	
	$counter=(count($trials)>0)?count($trials):1;	
	$average=sprintf("%.2f",$sum/count($trials));
	if (count($trials)>1)
	{
		$stdDev=sprintf("%.2f",sd($timeArray));
	}	
	else 
	{
		$stdDev=0.00;
	}
	return array($average,$stdDev);
}
 
function stripOutliers($times)
{
	$noOutlierArray=array();
	list($avg,$stDev)=getStats($times);
	foreach($times as $lineItem)
	{
		if ($lineItem['time']<=($avg+$stDev))
		{	
			$noOutlierArray[]['time']=$lineItem['time'];
		}
	}	
	return $noOutlierArray;	
}
$flagCnt=0;
$statCnt=0;
while(1)
{
	$output=array();
	exec($execCommand,$output);
	$time=intval(trim(substr($output[4],6,3))); //parse seconds
	list($status,$hud)=split(' ',$output[6],2); //parse status
	$status=trim($status);
	if ($status!='OK') 
	{
		$status=implode("-",array($output[6],$output[8],$output[9],$output[10]));
		$statCnt+=BADWEIGHT;
	}
	else
	{
		$statCnt-=GOODWEIGHT;
	}
	$resultArray[$trial]['time']=$time;
	$resultArray[$trial]['status']=$status;
 
	list($avg,$stDev)=getStats(stripOutliers($resultArray));
	$FLAG=($time>($avg+SLOWDEVIATIONS*$stDev))?"SLOW":'OK';
	if ($status!='OK') $FLAG='WARNING! Bad Status ';
	if ($FLAG=='SLOW')
	{ 
		$flagCnt+=BADWEIGHT;
	}
	else
	{
		$flagCnt-=GOODWEIGHT;
	}
	if ($flagCnt<=0) $flagCnt=0;
	if ($statCnt<=0) $statCnt=0;
	if ($flagCnt>(BADWEIGHT*BADMULTIPLIER)) $FLAG='ERROR! TOO MANY SLOW TRANSACTIONS';
	if ($statCnt>(BADWEIGHT*BADMULTIPLIER)) $FLAG='ERROR! TOO MANY TRANSACTION FAILURES';
	if ($FLAG=='ERROR! TOO MANY TRANSACTION FAILURES'&&$status=='OK') {$FLAG='OK';$flagCnt=0;}
	if ($FLAG=='ERROR! TOO MANY SLOW TRANSACTIONS'&&$status=='OK') {$FLAG='OK';$flagCnt=0;}
	$lineItem="${trial},".date('d/m/Y H:i:s').",$time sec,$status,Avg:$avg,StdDev:$stDev,$FLAG,$flagCnt,$statCnt\n";
	echo($lineItem);
	logToFile($lineItem);
	if (($FLAG!='OK')||($status!='OK')) sleep(BADTIMEOUT);
	$trial++;
}

which will produce a text file like this:

C:\xampp\php>php runTests.php
0,29/07/2010 12:06:01,34 sec,OK,Avg:34.00,StdDev:0,OK,0,0
1,29/07/2010 12:06:20,19 sec,OK,Avg:26.50,StdDev:10.61,OK,0,0
2,29/07/2010 12:06:39,19 sec,OK,Avg:19.00,StdDev:0.00,OK,0,0
3,29/07/2010 12:06:58,18 sec,OK,Avg:18.67,StdDev:0.58,OK,0,0
4,29/07/2010 12:07:17,18 sec,OK,Avg:18.50,StdDev:0.58,OK,0,0
5,29/07/2010 12:07:36,19 sec,OK,Avg:18.60,StdDev:0.55,OK,0,0
6,29/07/2010 12:07:55,18 sec,OK,Avg:18.50,StdDev:0.55,OK,0,0
7,29/07/2010 12:08:14,18 sec,OK,Avg:18.43,StdDev:0.53,OK,0,0
8,29/07/2010 12:08:38,24 sec,OK,Avg:19.12,StdDev:2.03,SLOW,10,0
9,29/07/2010 12:09:12,24 sec,OK,Avg:19.67,StdDev:2.50,OK,9,0