Skip to content

Commit

Permalink
Modified the way of using external tools (such as cachegrind and smap…
Browse files Browse the repository at this point in the history
…s) so "--tool cachegrind" or "--tool memusage" is used instead of earlier implementation. phpversion() and php_uname() are also added to the output as. A couple of smaller fixes were also made.

git-svn-id: https://svn.php.net/repository/php/php-benchmarks/trunk@282697 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
hjalle committed Jun 24, 2009
1 parent fdd5259 commit d3149fb
Showing 1 changed file with 106 additions and 26 deletions.
132 changes: 106 additions & 26 deletions benchcli/bench.php
Expand Up @@ -79,6 +79,11 @@ class Benchmark
*/
var $test_files;

/**
* Switch for verbosity
* @var boolean
*/
var $verbose;

/**
* Whether cachegrind should be used or not
Expand All @@ -92,12 +97,24 @@ class Benchmark
*/
var $smapsparser;

/**
* Variable that holds the total results if
* a tool is used
* @var array
*/
var $totresults;

/**
* Switch for showing memory usage or not
* @var boolean
*/
var $mem_usage;
var $memusage;

/**
* Current tool that is used
* @var string
*/
var $tool;
/**
* Constructor for the benchmark class. Uses the PEAR package
* Console_getargs for command line handling.
Expand All @@ -124,7 +141,7 @@ function Benchmark()
$this->memory_limit = 128;
}
$this->paths = $args->getValue('path');
if (is_string($this->paths)) {
if (is_string($this->paths)) {
//user has one directory as input, therefore
//$this->paths is recognized as a string
$tmpdir = $this->paths;
Expand All @@ -145,9 +162,20 @@ function Benchmark()
if ($this->log_file == "") {
$this->log_file = false;
}
$valid_tools = array("memusage", "cachegrind", "papiex");
$tool = $args->getValue('tool');
if (!empty($tool)) {
if (in_array($tool, $valid_tools)) {
$this->$tool = true;
$this->tool = $tool;
} else {
$this->printHelp($args);
exit;
}
}
$this->quite = $args->getValue('quite');
$this->mem_usage = $args->getValue('memory-usage');
$this->cachegrind = $args->getValue('cachegrind');
$this->verbose = $args->getValue('verbose');
$this->totresults = array();
$this->setTestFiles();
}
/**
Expand Down Expand Up @@ -186,25 +214,26 @@ function setConfig()
'debug' => array('short' => 'd',
'max' => 0,
'desc' => 'Switch to debug mode.'),
'path ' => array('min' => 0,
'path' => array('min' => 0,
'max' => -1,
'desc' => 'Path/paths to php test files',
'default' => 'microtests, tests'),
'default' => 'microtests, tests'),
'help' => array('short' => 'h',
'max' => 0,
'desc' => 'Print this help message'),
'php' => array('min' => 0,
'max' => 1,
'default' => 'php',
'desc' => 'PHP binary path'),
'verbose' => array('short' => 'v',
'max' => 0),
'quite' => array('short' => 'q',
'max' => 0,
'desc' => 'Don\'t produce any output'),
'memory-usage' => array('short' => 'mu',
'max' => 0,
'desc' => 'Show memory statistics. This requires linux with kernel 2.6.14 or newer'),
'cachegrind' => array('max' => 0,
'desc' => 'Enable the valgrind tool that a cache simulation of the test'),
'tool' => array('max' => 1,
'min' => 0,
'desc' => 'Specify which tool you want to use for special measurements. Valid tools are cachegrind, memusage and papiex',
'default' => ""),
'log' => array('min' => 0,
'max' => 1,
'default' => 'benchlog.txt',
Expand Down Expand Up @@ -260,6 +289,14 @@ function setTestFiles()
$this->test_files = $files;
}

function addTotal($result)
{
foreach ($result as $key => $value) {
if (is_numeric($value)) {
$this->totresults[$key] += $value;
}
}
}
/**
* Runs the benchmark
*
Expand All @@ -272,43 +309,86 @@ function run()
$datetime = date("Y-m-d H:i:s", time());
$startstring = "";
$this->console("--------------- Bench.php ".$datetime."--------------\n");

$this->console("PHP version: ".phpversion()."\n");
$this->console(php_uname()."\n");
if (count($this->test_files) == 0) {
$this->console("No test files were found in chosen path: exiting.");
die();
}
foreach ($this->test_files as $test) {
$output = array();
if ($this->cachegrind) {
$startstring = "valgrind --tool=cachegrind --branch-sim=yes";
}
$cmd = "$startstring {$this->php} -d memory_limit={$this->memory_limit}M ".$test['filename'];

$this->console("Start of ".$test['name']."\n");
$this->console("$cmd\n", $this->debug);
$timer->start();
list($out, $err, $exit) = $this->completeExec($cmd, null, 0);
if ($this->mem_usage) {
if (!$this->quite) {
if ($this->memusage) {
$res = $this->smapsparser->peak;
if (!$this->quite && $this->verbose) {
$this->smapsparser->printMaxUsage(10);
}
if ($this->log_file) {
if ($this->log_file && $this->verbose) {
$this->smapsparser->printMaxUsage(10, $this->log_file);
}
$this->smapsparser->clear();
}
if ($this->cachegrind) {
} else if ($this->cachegrind) {
$cacheparser = new Cachegrindparser();
$result = $cacheparser->parse($err);
print_r($result);
$cacheparser->parse($err);
$res = $cacheparser->results;
if (!$this->quite && $this->verbose) {
$cacheparser->printResults();
}
if ($this->log_file && $this->verbose) {
$cacheparser->printResults($this->log_file);
}
}
if (empty($this->totresults)) {
$this->totresults = $res;
} else {
$this->addTotal($res);
}

$timer->stop();
$this->console($out, $this->debug);
$this->console("Results from ".$test['name'].": ".$timer->elapsed."\n");
$totaltime += $timer->elapsed;
}

switch ($this->tool) {
case "cachegrind":
$cacheparser = new Cachegrindparser();
$cacheparser->results = $this->totresults;
if (!$this->quite) {
$cacheparser->printResults();
}
if ($this->log_file) {
$cacheparser->printResults($this->log_file);
}
break;
case "memusage":
$this->smapsparser->peak = $this->totresults;
if (!$this->quite) {
$this->smapsparser->printSumUsage();
}
if ($this->log_file) {
$this->smapsparser->printSumUsage($this->log_file);
}
break;
case "papiex":
break;
default:
break;
}
$this->console("Total time for the benchmark: ".$totaltime." seconds\n");

$datetime = date("Y-m-d H:i:s", time());
$this->console("-------------- END ".$datetime."---------------------\n");
}
/**
* Executes a program in proper way. The function is borrowed
* Executes a program in proper way. The function is borrowed
* the php compiler project, http://www.phpcompiler.org.
*
* @param string $command The command to be executed
Expand Down Expand Up @@ -351,7 +431,7 @@ function completeExec($command, $stdin = null, $timeout = 20)
$out .= $new_out;
$err .= $new_err;
$pid = $this->getChildren($handle, $pipes);
if ($this->mem_usage) {
if ($this->memusage) {
if ($data = $this->smapsparser->readSmapsData($pid[0])) {
$this->smapsparser->parseSmapsData($data);
}
Expand Down Expand Up @@ -383,7 +463,7 @@ function completeExec($command, $stdin = null, $timeout = 20)
}
/**
* Get's the child processes of a shell execution.
*
*
* @param handler &$handle The handler
* @param array &$pipes The pipes
*
Expand All @@ -392,8 +472,8 @@ function completeExec($command, $stdin = null, $timeout = 20)
function getChildren(&$handle, &$pipes)
{
$status = proc_get_status($handle);
$ppid = $status["pid"];
$pids = preg_split("/\s+/", trim(`ps -o pid --no-heading --ppid $ppid`));
$ppid = $status["pid"];
$pids = preg_split("/\s+/", trim(`ps -o pid --no-heading --ppid $ppid`));
return $pids;
}
/**
Expand All @@ -406,7 +486,7 @@ function getChildren(&$handle, &$pipes)
*/
function killProperly(&$handle, &$pipes)
{

// proc_terminate kills the shell process, but won't kill a runaway infinite
// loop. Get the child processes using ps, before killing the parent.
$pids = $this->getChildren($handle, $pipes);
Expand Down

0 comments on commit d3149fb

Please sign in to comment.