Login   Register  
PHP Classes
elePHPant
Icontem

File: benchmark/benchmark.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Oleg Butuzov  >  Cacheme  >  benchmark/benchmark.php  >  Download  
File: benchmark/benchmark.php
Role: Example script
Content type: text/plain
Description: benchmark
Class: Cacheme
Cache data in different types of container
Author: By
Last change:
Date: 6 years ago
Size: 12,919 bytes
 

Contents

Class file image Download
<?       
// php.spb.ru
// dima's bench

 $cn = (isset($_GET['cn']) && is_numeric($_GET['cn']) && $_GET['cn'] > 0 )? $_GET['cn'] : 1;


//
$path = dirname(dirname(__FILE__)).'/Cacheme/Cacheme.php';
include_once $path;
/*************************************************************************/

class Test{
	public function __construct(){
		$this->var = rand(0,1000);
	}

	public function showme(){
		return $this->$var;
	}
}


$variables = array(
	range(0,100),
	'string is a string, life is life',
	new Test()
);

function simrun($dsn, $driver, $method){
	$c = CacheMeLoader::Factory($dsn);
	return $method($dsn, $driver, $c);
}

function objrun($dsn, $driver, $method){
	$objname =  $driver.'Object';
	global ${$objname};
	$objname = (isset($objname) && is_object($objname)) ? $objname : CacheMeLoader::Factory($dsn);
	return $method($dsn, $driver, $objname);
}

function cacheme_set_clear($dsn, $driver, $obj){
	global $variables;
	$obj->lifetime = 60;
	foreach($variables as $k=>$v){
		foreach(range(0,32) as $i){
			$obj->set($k,$v);
			$obj->clear($k);
		}
	}
}

function cacheme_set_clear_all($dsn, $driver, $obj){
	global $variables;
	$obj->lifetime = 60;
	foreach($variables as $k=>$v){
		foreach(range(0,32) as $i){
			$obj->set($k,$v);
		}
	}
	$obj->clear();

}


function cacheme_set_get($dsn, $driver, $obj){
	$obj->lifetime = 60;
	$obj->set('foo','bar');
	foreach(range(0,99) as $i){
		$obj->get('foo');
	}
}
function cacheme_set($dsn, $driver, $obj){
	global $variables;
	$obj->lifetime = 60;
	foreach($variables as $k=>$v){
		foreach(range(0,32) as $i){
			$obj->set($k,$v);
		}
	}
}

function cacheme_get($dsn, $driver, $obj){
	global $variables;
	
	$obj->lifetime = 60;
	foreach($variables as $k=>$v){
		if (!$obj->is_cached($k)){
		    $obj->set($k,$v);
		}
		foreach(range(0,32) as $i){
			$obj->get($k);
		}
	}
	return true;
}

/**************************************************************************************************************************/


    $phpbegin='
    $_test = "";
	define("test","");
	
   ';

$methods_desc = array();
$methods_names = array();

$descriptions = array(
        'cacheme_get'           => '3 set itteration = 3*33 Get itterators (33 per each set)
----
Test cache 3 variables (array, string, and object), after each setting - test get cached variable 33 times.
<ul>
<li><b>eaccelerator</b> - can work only in admin allowed path, take look at <a href="http://eaccelerator.net/wiki/Settings#eAccelerator0.9.5andhigher" target="_black">eaccelerator settings</a> more detailed.</li>
</ul>',
        'cacheme_set'           => '3*33 Set itterators
----
Just setting 33 times pere each variable time (array, string, object)
<ul>
<li><b>eaccelerator</b> - can work only in admin allowed path, take look at <a href="http://eaccelerator.net/wiki/Settings#eAccelerator0.9.5andhigher" target="_black">eaccelerator settings</a> more detailed.</li>

</ul>',
        'cacheme_set_clear'     => '3*33 Set itterators and clear for each
----
Clear speed testing. setting 33 times 3 data types (string,array,object) and making clear for each.<br>
<b>NOTES</b>
<ol>
	<li><b>memcached</b> dosen\'t actualy delate any value - it just mark it as expired</li>
	<li><b>eaccelerator</b> - can work only in admin allowed path, take look at <a href="http://eaccelerator.net/wiki/Settings#eAccelerator0.9.5andhigher" target="_black">eaccelerator settings</a> more detailed.</li>
</ol>',
        'cacheme_set_clear_all' => '100 Set itterators and one clear all
----
Clear speed testing. setting 33 times 3 data types (string,array,object) and making clear for each.
<ol>
	<li><b>memcached</b> dosen\'t actualy delate any value - it just mark it as expired</li>
	<li><b>xcache</b> can flush cache but require a authorization for this action, so i deside to disable this method for xcache</li>
	<li><b>eaccelerator</b> - can work only in admin allowed path, take look at <a href="http://eaccelerator.net/wiki/Settings#eAccelerator0.9.5andhigher" target="_black">eaccelerator settings</a> more detailed.</li>
</ol>',
        'cacheme_set_get'       => '1 Set itterators and 100 Get for each
----
1 Set itterators and 100 Get for each
'
);

foreach($descriptions as $_k=>$item){
        list($title, $description) = explode('----', trim($item));
	$methods_names[$_k] = trim($title);
        $methods_desc[$_k] = trim($description);
}


$type   = isset($_GET['method']) ?  substr($_GET['method'], strlen($_GET['method'])-3, 3) : 'sim';
$method = isset($_GET['method']) ? substr($_GET['method'], 0, strlen($_GET['method'])-4) : $methods[0];

$methods = array_keys($methods_names);
$method = isset($method) && in_array($method, $methods) ? $method : $methods[0];



$phpexplode="~~~";

$phptest  = '';

$dsnArray = array(
	
	'file'			=> 'file://'.dirname(__FILE__).DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, array('CacheStorage','FileCache')),	
/*	'sqlite'   		=> 'sqlite://'.dirname(__FILE__).DIRECTORY_SEPARATOR.'CacheStorage/cache.sqlite',
	'eaccelerator'  	=> 'eaccelerator://',*/
/*	'xcache'		=> 'xcache://',
                   */     'memcache' 		=> 'memcache://127.0.0.1:11211',
        'apc'			=> 'apc://',
/**/
  
        
);
$numers = array('foobar');
foreach($dsnArray as $k=>$i){
	$phptest[] =  " $type"."run(".'$dsnArray[\''.$k.'\']'.",'$k', '$method');\r\n" ;
        $numers[]  = $k;
}

	$cna = array('1', '10', '25', '50', '100', '250', '500', '1000');
	$t = array();
	
	foreach($cna as $i){ 
		if ($i != $cn){
			$t[] =  '<a href='.$_SERVER['PHP_SELF'].'?method='.$method.'_obj&cn='.$i.'>'.$i.'</a>';
		} else {
			$t[] =  '<b>'.$i.'</b> ';		
		}
	} 
	
	
	
	
	?> 
<table width="100%" border="0">
  <tr>
    <td colspan="<?php echo count($cna); ?>" style=" background:#404040; color:#fff;  ">Calls number <ul>
	<li>Current test -  <?=$methods_names[$method];?></li>
	<li>Type - <? echo ($type=='sim') ? ' Each call create new object ' : ' Each call use Single instance '; ?></li>
    </ul></td>
  </tr>
  <tr><td style='text-align:center;'>
	<?php
	 echo implode(' </td><td style="text-align:center;"> ', $t);
	 unset($t);
	?></td>
  </tr>
</table>

<?php
	$t = array();
	
	foreach($methods as $i){ 
		if ($i != $method || $type != 'sim'){
			$t[] =  '<a href='.$_SERVER['PHP_SELF'].'?method='.$i.'_sim&cn='.$cn.'>'.$methods_names[$i].'</a>';
		} else {
			$t[] =  '<b>'.$methods_names[$i].'</b> ';		
		}
	} 
	//echo implode(' &#8226; ', $t);
	
	
	
	?>
<table width="100%" border="0">
  <tr>
    <td colspan="<?php echo count($methods); ?>" style=" background:#404040; color:#fff; text-align:center">Tests ( Each call create own instance)</td>
  </tr>
  <tr><td style='text-align:center;'>
	<?php
	 echo implode(' </td><td style="text-align:center;"> ', $t);
	 unset($t);
	?></td>
  </tr>
</table>


<?php
	
	
	$t = array();
	
	foreach($methods as $i){ 
		if ($i != $method || $type != 'obj'){
			$t[] =  '<a href='.$_SERVER['PHP_SELF'].'?method='.$i.'_obj&cn='.$cn.'>'.$methods_names[$i].'</a>';
		} else {
			$t[] =  '<b>'.$methods_names[$i].'</b> ';		
		}
	} 
	
	
	?>
<table width="100%" border="0">
  <tr>
    <td colspan="<?php echo count($methods); ?>" style=" background:#404040; color:#fff; text-align:center">Tests ( enstance created just once - each call use existed instance)</td>
  </tr>
  <tr><td style='text-align:center;'>
	<?php
	 echo implode(' </td><td style="text-align:center;"> ', $t);
	 unset($t);
	?></td>
  </tr>
</table>

<table width="100%" border="0">
  <tr>
    <td style=" background:#404040; color:#fff; text-align:center">Tests description</td>
  </tr>
  <tr><td>
	<?php
		echo $methods_desc[$method];
	?></td>
  </tr>
</table>
<?php

	

error_reporting(E_ALL && ~E_NOTICES);

$mytimestats = array();

function timestart($name) {
   global $mytimestats;
   if (strlen($name)==0) {
      return;
   }
   
   $x=explode(" ",microtime());
   $x[1]=substr("$x[1]",2,14);
   
   
   $mytimestats[$name]['temp']=$x[1]+$x[0];
   flush();
   return true;
}

function timestop($name) {
   global $mytimestats;
   
   flush();
   
   if (strlen($name)==0) {
      return;
   }
   
   $x=explode(" ",microtime());
   
   $x[1]=substr("$x[1]",2,14);
   
   $param = ($x[1]+$x[0]) - $mytimestats[$name]['temp'];
   $mytimestats[$name]['all'] = $param;
   $mytimestats[$name]['counter']++;
   return true;
   
}

function timeprint($par="") {
   timestop("my_time");
   global $mytimestats,$numers;

   $k=array_keys($mytimestats);

   if (strstr($par,"nomain")) {
      $nomain=1;
   }
   if (strstr($par,"%min")) {
      $proc1=1;
      $procent1="<td>% (fro, min time)</td>";
   }
   if (strstr($par,"%max")) {
      $proc2=1;
      $procent2="<td>% (from max time)</td>";
   }
   if (strstr($par,"graf")) {
      $graf=1;
      $grafik="<td align=center>total time</td>";
   }
   if ($proc1 || $proc2 || $graf) {
      $mmin=999999;
      $mmax=-1;
      for ($i=0; $i<count($k); $i++) {
         if ($k[$i]=="my_time") continue;
         if ($mmin>$mytimestats[$k[$i]][all]) $mmin=$mytimestats[$k[$i]][all];
         if ($mmax<$mytimestats[$k[$i]][all]) $mmax=$mytimestats[$k[$i]][all];
      }
   }
 
   echo "<center><table border=1 cellspacing=0 cellpadding=3><tr><td align=center></td>
<td align=center>total calls</td>
<td align=center>total time</td><td align=center>	
the average time</td>
$procent1$procent2$grafik</tr>";
   for ($i=0; $i<count($k); $i++) {
      if ($k[$i]=="my_time") continue;
     
      @printf("<tr><td><b>$numers[$i]</b></td><td>%d</td><td>%.4f</td><td>%.4f</td>",
            $mytimestats[$k[$i]]['counter'],
            $mytimestats[$k[$i]]['all'],
            (float)$mytimestats[$k[$i]]['all']/$mytimestats[$k[$i]]['counter']);
      if ($k[$i]<>"my_time") {
         if ($proc1) {
            printf("<td>%02.1f%%</td>",(float)$mytimestats[$k[$i]]['all']/$mmin*100-100);
         }
         if ($proc2) {
            printf("<td>%02.1f%%</td>",(float)$mytimestats[$k[$i]]['all']/$mmax*100);
         }
         if ($graf) {
            $width=round(100*(float)$mytimestats[$k[$i]]['all']/$mmax);
            $width2=100-$width;
            echo "<td><table width=100 border=0 ".
                "cellspacing=0 cellpadding=0>".
                "<tr><td width=$width background=_dima_timestat1.gif>".
                "<img src='_dima_timestat2.gif' width=$width height=20><br>".
                "</td><td width=$width2 bgcolor=#ccaaaa>".
                "<img src='_dima_timestat2.gif' width=$width2 height=20><br>".
                "</td></tr></table></td>";
         }
         $tt+=$mytimestats[$k[$i]][all];
         $tc+=$mytimestats[$k[$i]][counter];
      }
      else {
         if ($proc1) echo "<td>&nbsp;</td>";
         if ($proc2) echo "<td>&nbsp;</td>";
         if ($graf) echo "<td>&nbsp;</td>";
      }
      echo "</tr>";
   }
   if (!$nomain)
      printf("
<tr><td colspan=4>вся пpогpамма pаботала %.4f сек</tD></tr>
<tr><td colspan=4>все внутpенные вызовы заняли %.4f сек (%d pаз)</tD></tr>
<tr><td colspan=4>остаток вpемени %.4f сек</tD>",
   $mytimestats[my_time][all],$tt,$tc,
   $mytimestats[my_time][all]-$tt);
   echo "</td></table></center>\r\n\r\n\r\n";
   
}


timestart("my_time");
   
   
   
   
   @set_time_limit(60*30); 
	//error_reporting(0);
   unset($cf);
   unset($evalcod);
   for ($i=0; $i<50; $i++) {
      echo "<!-- -->";
   }
   echo "Preparing...";
   flush();
   

   foreach($phptest as $s) {
      if (strlen(trim($s))>1) {
         $cf++;
         $evalcod[]= $s;;
      }
   }


   for ($i=0; $i<$cf; $i++) {
      $f[$i]=fopen("tmp.tmp$i","w+") or die("err# open '.tmp$i'");
      fputs($f[$i],"<?php"."\r\n");
   }

  
      for ($j=0; $j<$cf; $j++) {
         fputs($f[$j],$evalcod[$j]."\r\n");
      }
  

   for ($i=0; $i<$cf; $i++) {
      fputs($f[$i],"?>");
      fclose($f[$i]);
   }

   echo "Ready.<br>Lets! Test it!...<hr size=1 noshade>";
   flush();

   
   for ($i=0; $i<$cf; $i++) {
	 for ($j=0; $j<$cn; $j++) {
		timestart("test N".($i+1));
		require("tmp.tmp$i");
		timestop("test N".($i+1));
	 }
      unlink("tmp.tmp$i");	 
      flush();
   }


   echo "<hr size=1 noshade>\r\n\r\n<ol>";
   for ($i=0; $i<count($evalcod); $i++) {
      echo "<li><b><font color=red>{</font><tt><font color=blue face='Lucida'>".
         str_replace(" ","&nbsp;",htmlspecialchars($evalcod[$i]))."</font></tt><font color=red>}</font></b>\r\n";
   }    
   echo "</ol>\r\n\r\n\r\n\r\n";
   timeprint("%min %max graf nomain");



?>