| 1 |
<?php
|
| 2 |
//DEFINES
|
| 3 |
/**
|
| 4 |
* @author Jonathan Hendler
|
| 5 |
* @package SONIA
|
| 6 |
* */
|
| 7 |
|
| 8 |
|
| 9 |
//-----
|
| 10 |
//print test name
|
| 11 |
/**
|
| 12 |
* basic printing for the result of a test
|
| 13 |
* */
|
| 14 |
function printTest($name, $description, $content, $success)
|
| 15 |
{
|
| 16 |
global $max_memory_usage;
|
| 17 |
if (function_exists('memory_get_usage')) $current_usage = memory_get_usage();
|
| 18 |
if ($current_usage > $max_memory_usage) $max_memory_usage = $current_usage;
|
| 19 |
|
| 20 |
if ($success)
|
| 21 |
{
|
| 22 |
printSuccess();
|
| 23 |
}
|
| 24 |
else
|
| 25 |
{
|
| 26 |
printFail();
|
| 27 |
}
|
| 28 |
printTestName($name, $description);
|
| 29 |
pmsg($content);
|
| 30 |
|
| 31 |
flush();
|
| 32 |
}
|
| 33 |
|
| 34 |
|
| 35 |
function printFail()
|
| 36 |
{
|
| 37 |
print '<div class="testFailed"> FAILED </div>';
|
| 38 |
|
| 39 |
}
|
| 40 |
|
| 41 |
function printSuccess()
|
| 42 |
{
|
| 43 |
print '<div class="testPassed"> SUCCESS </div>';
|
| 44 |
}
|
| 45 |
|
| 46 |
function printTestName($testname, $description)
|
| 47 |
{
|
| 48 |
print '<div class="testname">'.$testname.'</div>';
|
| 49 |
print '<div class="test_description">'.$description.'</div>';
|
| 50 |
flush();
|
| 51 |
}
|
| 52 |
|
| 53 |
/*print a general message*/
|
| 54 |
function pmsg($msg)
|
| 55 |
{
|
| 56 |
print '<div class="pmsg">'.$msg.'</div>';
|
| 57 |
flush();
|
| 58 |
}
|
| 59 |
|
| 60 |
//------------------------------------------------------
|
| 61 |
/**
|
| 62 |
* requires DBG
|
| 63 |
* */
|
| 64 |
function doDebugBreak()
|
| 65 |
{
|
| 66 |
if (function_exists('DebugBreak'))
|
| 67 |
{
|
| 68 |
DebugBreak();
|
| 69 |
}
|
| 70 |
else
|
| 71 |
{
|
| 72 |
trigger_error( 'You must install the debugger "DBG":<br/> see <a href="http://www.plog4u.org/index.php/Using_PHPEclipse_:_Installation_:_Installing_the_DBG_Debugger">http://www.plog4u.org/</a>', E_USER_ERROR);
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
function write_spacer($x = '')
|
| 77 |
{
|
| 78 |
echo "<hr>$x<br>";
|
| 79 |
flush();
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* A debug method.
|
| 84 |
* Use to print contents of an array and gives an indication
|
| 85 |
* of where dbg() was called
|
| 86 |
*
|
| 87 |
* @todo
|
| 88 |
* - add support for object debugging
|
| 89 |
* - have default $lable parameter be the name of $param passed in
|
| 90 |
*
|
| 91 |
* @param mixed $array the item to debug
|
| 92 |
* @param string $label optional html to label the $array
|
| 93 |
* @param string $bt whethor or not to enable back trace
|
| 94 |
*
|
| 95 |
* */
|
| 96 |
function dbg($array, $label = '-', $bt = false)
|
| 97 |
{
|
| 98 |
$backtrace_string = '';
|
| 99 |
if ($bt)
|
| 100 |
{
|
| 101 |
$backtraces = debug_backtrace();
|
| 102 |
$backtrace_string = '';
|
| 103 |
$first_run = true;
|
| 104 |
|
| 105 |
foreach ($backtraces as $backtrace)
|
| 106 |
{
|
| 107 |
$backtrace_string .= '<strong>line '.$backtrace['line']. '</strong> calls function <strong>';
|
| 108 |
$backtrace_string .= $backtrace['function'].'()</strong> in ';
|
| 109 |
$backtrace_string .= '<span style="font-size: 9pt;">'.$backtrace['file'].'</span><br />';
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
| 113 |
echo '<hr /><div style="pmsg"><strong>'. $label . '</strong><br />';
|
| 114 |
echo '<div class="arrayDebug" ><pre>';
|
| 115 |
print_r($array);
|
| 116 |
echo '</pre></div>' ;
|
| 117 |
echo "<em>" ;
|
| 118 |
echo $backtrace_string;
|
| 119 |
echo '</em></div>';
|
| 120 |
flush();
|
| 121 |
|
| 122 |
}
|
| 123 |
|
| 124 |
function ArrayDepth($Array,$DepthCount=-1,$DepthArray=array())
|
| 125 |
{
|
| 126 |
$DepthCount++;
|
| 127 |
if (is_array($Array))
|
| 128 |
foreach ($Array as $Key => $Value)
|
| 129 |
$DepthArray[]=ArrayDepth($Value,$DepthCount);
|
| 130 |
else
|
| 131 |
return $DepthCount;
|
| 132 |
foreach($DepthArray as $Value)
|
| 133 |
$Depth=$Value>$DepthCount?$Value:$DepthCount;
|
| 134 |
return $Depth;
|
| 135 |
}
|
| 136 |
|
| 137 |
function make_seed()
|
| 138 |
{
|
| 139 |
list($usec, $sec) = explode(' ', microtime());
|
| 140 |
return (float) $sec + ((float) $usec * 100000);
|
| 141 |
}
|
| 142 |
|
| 143 |
// from php.net e dot a dot schultz at gmail dot com
|
| 144 |
if( !function_exists('memory_get_usage') )
|
| 145 |
{
|
| 146 |
function memory_get_usage()
|
| 147 |
{
|
| 148 |
//If its Windows
|
| 149 |
//Tested on Win XP Pro SP2. Should work on Win 2003 Server too
|
| 150 |
//Doesn't work for 2000
|
| 151 |
//If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
|
| 152 |
if ( substr(PHP_OS,0,3) == 'WIN')
|
| 153 |
{
|
| 154 |
if ( substr( PHP_OS, 0, 3 ) == 'WIN' ){
|
| 155 |
$output = array();
|
| 156 |
exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
|
| 157 |
|
| 158 |
if (isset($output[5]) )return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
|
| 159 |
}
|
| 160 |
}
|
| 161 |
else if (function_exists('exec'))
|
| 162 |
{
|
| 163 |
//We now assume the OS is UNIX
|
| 164 |
//Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
|
| 165 |
//This should work on most UNIX systems
|
| 166 |
$pid = getmypid();
|
| 167 |
$output = array();
|
| 168 |
exec("ps -eo%mem,rss,pid | grep $pid", $output);
|
| 169 |
$output = explode(" ", $output[0]);
|
| 170 |
//rss is given in 1024 byte units
|
| 171 |
return $output[1] * 1024;
|
| 172 |
}
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
function this_microtime()
|
| 177 |
{
|
| 178 |
list($usec, $sec) = explode(" ", microtime());
|
| 179 |
return ((float)$usec + (float)$sec);
|
| 180 |
}
|
| 181 |
|
| 182 |
function sonia_test_output_results($all_test_results, $type = 'tabbed')
|
| 183 |
{
|
| 184 |
$output = '';
|
| 185 |
$first_row = '';
|
| 186 |
|
| 187 |
switch($type)
|
| 188 |
{
|
| 189 |
case 'excel':
|
| 190 |
|
| 191 |
break;
|
| 192 |
case 'pretty':
|
| 193 |
break;
|
| 194 |
case 'tabbed':
|
| 195 |
default:
|
| 196 |
//firefox preserves state here unless name changes
|
| 197 |
$start = '<h2>Copy and paste below into excel</h2><textarea name="'.time().'" cols="100" rows="40" >'."\n";
|
| 198 |
$end = '</textarea>';
|
| 199 |
$first_row .= "Test Group\tTest\tNumber of Objects\tNumber of Classes\tTime\tSubtitle\tComment\tMemory\tSuccess";
|
| 200 |
|
| 201 |
foreach ($all_test_results as $title => $data)
|
| 202 |
{
|
| 203 |
$output .= "\n".SONIA_TEST_NAME."\t$title\t".SONIA_TEST_NUMBER_OF_OBJECTS."\t".SONIA_TEST_NUMBER_OF_CLASSESS."";
|
| 204 |
foreach($data as $name =>$value)
|
| 205 |
{
|
| 206 |
sonia_clean_output($value);
|
| 207 |
//sonia_clean_output($name);
|
| 208 |
$output .= "\t".$value;
|
| 209 |
}
|
| 210 |
}
|
| 211 |
|
| 212 |
echo $start;
|
| 213 |
echo $first_row."\n";
|
| 214 |
echo $output."\n";
|
| 215 |
echo $end;
|
| 216 |
|
| 217 |
|
| 218 |
break;
|
| 219 |
}
|
| 220 |
}
|
| 221 |
|
| 222 |
function sonia_clean_output(&$op)
|
| 223 |
{
|
| 224 |
$op = str_replace("\n",' ',$op);
|
| 225 |
$op = str_replace("\r",' ',$op);
|
| 226 |
$op = str_replace("\t",' ',$op);
|
| 227 |
}
|
| 228 |
|
| 229 |
////////////////////////////////////////////////////////////////////////////////
|
| 230 |
// Drupal dummy functions
|
| 231 |
// could produce flaw in tests
|
| 232 |
/**
|
| 233 |
* replaces Drupal cache function
|
| 234 |
* */
|
| 235 |
// function (){}
|
| 236 |
|
| 237 |
function db_set_active(){}
|
| 238 |
function cache_get($key)
|
| 239 |
{
|
| 240 |
if(isset($_SESSION[$key])) return unserialize(base64_decode($_SESSION[$key]));
|
| 241 |
return null;
|
| 242 |
}
|
| 243 |
|
| 244 |
function cache_set($key, $value)
|
| 245 |
{
|
| 246 |
$_SESSION[$key] = base64_encode(serialize($value));
|
| 247 |
}
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
?>
|