Location: PHPKode > scripts > Httperf server stressing and benchmark tool > httperf-server-stressing-and-benchmark-tool/httperf.php
<?
/*
 * httperf.php
 *
 * @(#) $Header: /cvsroot/httperf/httperf.php,v 1.6 2000/02/17 23:46:36 mlemos Exp $
 *
 */

class httperf_class
{
 var $httperf_path="httperf";
 var $server="localhost";
 var $port=80;
 var $uri="/";
 var $hog=0;
 var $connections=1;
 var $error="";
 var $white_rgb=array(240,240,240);
 var $black_rgb=array(0,0,0);
 var $white=0;
 var $black=0;
 var $graph=0;
 var $tick=4;
 var $gap=2;
 var $left=8;
 var $top=8;
 var $right=8;
 var $bottom=0;
 var $width=320;
 var $height=200;
 var $font=4;
 var $decimals=3;

 Function RunTest($output="")
 {
  $results=array();
  if(GetType($output)=="string")
   $output=array();
  $command=$this->httperf_path." \"--server=".EscapeShellCmd($this->server)."\" --port=".$this->port." \"--uri=".EscapeShellCmd($this->uri)."\" --num-conns=".$this->connections." ".($this->hog ? " --hog" : "");
  exec($command,$output,$result);
  if($result==0)
  {
   if(ereg("^Total: connections ([0-9]+) requests ([0-9]+) replies ([0-9]+) test\\-duration ([0-9]+\\.[0-9]+) s$",$output[3],$values))
    $results["duration"]=$values[4];
   else
    $this->error="the httperf command did not output the results in the expected format";
  }
  else
   $this->error="the execution of the httperf command failed ($command)";
  return($results);
 }

 Function CreateGraphImage($width,$height)
 {
  if(($this->graph=ImageCreate($width,$height)))
  {
   $this->white=ImageColorAllocate($this->graph,$this->white_rgb[0],$this->white_rgb[1],$this->white_rgb[2]);
   $this->black=ImageColorAllocate($this->graph,$this->black_rgb[0],$this->black_rgb[1],$this->black_rgb[2]);
   ImageInterlace($this->graph,1);
   ImageFilledRectangle($this->graph,0,0,$width-1,$height-1,$this->white);
  }
  else
   $this->error="Could not create the graph image";
  return($this->graph);
 }

 Function DrawGraph(&$results)
 {
  $connections=count($results);
  Reset($results);
  $first=Key($results);
  Next($results);
  for($minimum=$maximum=$results[$first]["duration"],$connection=1;$connection<$connections;Next($results),$connection++)
  {
   $value=Key($results);
   if($minimum>$results[$value]["duration"])
    $minimum=$results[$value]["duration"];
   if($maximum<$results[$value]["duration"])
    $maximum=$results[$value]["duration"];
  }
  $last=$value;
  $amplitude=($maximum==$minimum ? 1 : $maximum-$minimum);
  $font_height=ImageFontHeight($this->font);
  $top=$this->top;
  $bottom=$this->bottom+$this->tick+$font_height+$this->gap;
  $graph_height=$this->height-$top-$bottom;
  $font_width=ImageFontWidth($this->font);
  for($last_value="",$maximum_width=0,$y=$graph_height-$font_height/2;$y>0;$y-=$font_height+$this->gap)
  {
   $value=number_format($maximum-$y*$amplitude/($graph_height-1),$this->decimals,".","");
   if(strcmp($last_value,$value))
   {
    $value_width=strlen($value)*$font_width;
    $maximum_width=max($maximum_width,$value_width);
    $last_value=$value;
   }
  }
  $left=$this->left+$maximum_width+$this->gap+$this->tick;
  $right=$this->right;
  $graph_width=$this->width-$left-$right;
  if($this->CreateGraphImage($graph_width+$left+$right,$graph_height+$top+$bottom))
  {
   ImageLine($this->graph,$left,$top,$left,$top+$graph_height-1,$this->black);
   ImageLine($this->graph,$left,$top+$graph_height-1,$left+$graph_width-1,$top+$graph_height-1,$this->black);
   for($last_value="",$y=$graph_height-$font_height/2;$y>0;$y-=$font_height+$this->gap)
   {
    $value=number_format($maximum-$y*$amplitude/($graph_height-1),$this->decimals,".","");
    if(strcmp($last_value,$value))
    {
     $value_width=strlen($value)*$font_width;
     $tick_y=$this->top+$y;
     $tick_x=$left-$this->tick;
     ImageString($this->graph,$this->font,$this->left+$maximum_width-$value_width,$tick_y-$font_height/2,$value,$this->black);
     ImageLine($this->graph,$tick_x,$tick_y,$tick_x+$this->tick-1,$tick_y,$this->black);      
     $last_value=$value;
    }
   }
   Reset($results);
   $first=Key($results);
   for($last_tick_x=$left,$last_x=0,$last_y=$graph_height-1,$connection=0;$connection<$connections;Next($results),$connection++)
   {
    $value=Key($results);
    $x=($value-$first)*($graph_width-1)/($last-$first);
    $y=($graph_height-1)-($results[$value]["duration"]-$minimum)*($graph_height-1)/($amplitude);
    ImageLine($this->graph,$left+$last_x,$top+$last_y,$left+$x,$top+$y,$this->black);
    $last_x=$x;
    $last_y=$y;
    $value_width=($font_width*strlen($value));
    $tick_x=$left+$x;
    $tick_y=$top+$graph_height;
    $value_x=$tick_x-$value_width/2;
    if($value_x>$last_tick_x+$this->gap)
    {
     ImageLine($this->graph,$tick_x,$tick_y,$tick_x,$tick_y+$this->tick-1,$this->black);      
     $value_y=$tick_y+$this->tick+$this->gap;
     ImageString($this->graph,$this->font,$value_x,$value_y,$value,$this->black);
     $last_tick_x=$tick_x+$value_width/2;
    }
   }
  }
  else
   $this->error="could not create the graphic image";
  return($this->graph);
 }

 Function DrawMessage($message)
 {
  if($this->graph
  || $this->CreateGraphImage($this->width,$this->height))
  {
   $fit=intval($this->width/ImageFontWidth($this->font));
   for($y=$position=0;$position<strlen($message);$y+=ImageFontHeight($this->font))
   {
    $length=strlen($message)-$position;
    $next=$position+$length;
    if($length>$fit)
    {
     $length=$fit;
     if(($space=strrpos(substr($message,$position,$length)," "))>0)
     {
      $next=$length=$space;
      $next=$position+$length+1;
     }
     else
      $next=$position+$length;
    }
    ImageString($this->graph,$this->font,0,$y,substr($message,$position,$length),$this->black);
    $position=$next;
   }
  }
 }

 Function GenerateGraph()
 {
  Header("Content-Type: image/gif");
  ImageGif($this->graph);
 }

};

?>
Return current item: Httperf server stressing and benchmark tool