cvs: php-benchmarks /benchcli bench.php
| From: | Alexander Hjalmarsson | Date: | Thu, 28 May 2009 13:40:59 +0000 |
| Subject: | cvs: php-benchmarks /benchcli bench.php | ||
| Groups: | php.qa | ||
| Request: | Send a blank email to php-qa+get-65038@lists.php.net to get a copy of this message | ||
hjalle Thu May 28 13:40:59 2009 UTC
Modified files:
/php-benchmarks/benchcli bench.php
Log:
Changed to class-structure
Added PEAR console_getargs usage instead
Fixed some (most?) CS issues
http://cvs.php.net/viewvc.cgi/php-benchmarks/benchcli/bench.php?r1=1.1&r2=1.2&diff_format=u Index: php-benchmarks/benchcli/bench.php diff -u php-benchmarks/benchcli/bench.php:1.1 php-benchmarks/benchcli/bench.php:1.2 --- php-benchmarks/benchcli/bench.php:1.1 Sun May 24 19:01:24 2009 +++ php-benchmarks/benchcli/bench.php Thu May 28 13:40:59 2009 @@ -1,196 +1,265 @@ <?php +// +----------------------------------------------------------------------+ +// | PHP Version 5 | +// +----------------------------------------------------------------------+ +// | Copyright (c) 2009 The PHP Group | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 3.0 of the PHP license, | +// | that is available through the world-wide-web at the following url: | +// | http://www.php.net/license/3_0.txt. | +// | If you were unable to obtain it though the world-wide-web, please | +// | send a note to license@php.net so we can mail you a copy immediately | +// +----------------------------------------------------------------------+ +// | Author: Alexander Hjalmarsson <hjalle@php.net> | +// +----------------------------------------------------------------------+ +// + /** - * Handles output and logging options. - * @param $msg The message to be handled - * @param $print Whether it should be printed - * or not. - * @param $log File to log the output to. - * Set to false if logging is - * disabled. + * This benchmark measures the performance of PHP + * Example of usage: + * bench.php --log log.txt -d -m 256 + * + * @author Alexander Hjalmarsson <hjalle@php.net> + * @copyright 2009 + * @license http://www.php.net/license/3_0.txt PHP License 3.0 */ -function console($msg,$print = true) -{ - if(VERBOSE == true) - { - if($print == true) - { - echo $msg; - } - - } - if(LOG_FILE != false && $print) - { - $fp = fopen(LOG_FILE,'a'); - fwrite($fp,$msg); - fclose($fp); - } -} + + +error_reporting(E_ALL | E_NOTICE); +set_time_limit(0); /** - * Return an array with all the paths to the test files found - * from the array of directories to search. - * Returning array have this structure: - * array("/path/to/test/test_name.php)" => "name.php",...,); - * @param $test_dirs An array of directories to search in - * @return Array of test files and their names. + * This is the main class of the benchmark script */ -function get_test_files($test_dirs) +class Benchmark { - $files = array(); - foreach($test_dirs as $dir) - { - foreach (glob("$dir/test_*.php") as $filename) { - $t['filename'] = $filename; - preg_match('/^'.preg_quote($dir,'/').'\/test_(.+)[.]php$/i', $filename, $matches); - $t['name'] = $matches[1]; - $files[] = $t; - } - } - - return $files; -} -/** - * Parses the arguments and returns them in a better structure. - * Flags enabled will get the value 1 and the arguments will get - * the value specified from the console. - * @param $argv The argument array ($argv) - * @param $flags Existing flag options - * @param $arguments Existing argument options - * @return If flags and arguments were correct, - * it will return an array with $key -> $value - * structure. - */ -function arguments($argv,$flags,$arguments) { - $ARGS = array(); - foreach ($argv as $arg) { - if (ereg('--[a-zA-Z0-9]*=.*',$arg)) { - $str = split("=",$arg); $arg = ''; - $key = ereg_replace("--",'',$str[0]); - for ( $i = 1; $i < count($str); $i++ ) { - $arg .= $str[$i]; + /** + * Maximum memory usage for php test files. + * @var int + */ + var $memory_limit; + + /** + * Debug switch + * @var boolean + */ + var $debug; + + /** + * Directories to search for php test files. + * @var array + */ + var $include; + + /** + * Path to the php binary + * @var string + */ + var $php; + + /** + * The config for command line arguments + * @var array + */ + var $config; + + /** + * The log file for output + * @var string + */ + var $log_file; + + /** + * Produce output or not + */ + var $quite; + + /** + * Holds the test files that will be tested + * @var array + */ + var $test_files; + + /** + * Constructor for the benchmark class. Uses the PEAR package + * Console_getargs for command line handling. + * + * @return void + */ + function Benchmark() + { + include_once 'misc/getargs.php'; + include_once 'misc/timer.php'; + + $this->setConfig(); + $args =& Console_Getargs::factory($this->config); + + if (PEAR::isError($args) || $args->getValue('help')) { + $this->printHelp($args); + exit; + } + + $this->debug = $args->getValue('debug'); + $this->memory_limit = $args->getValue('memory-limit'); + if ($this->memory_limit == "") { + $this->memory_limit = 128; + } + $this->include = $args->getValue('include'); + if (is_array($this->include)) { + $this->include[] = "tests"; // Append default directory + } else { + //user has one directory as input, therefore + //$this->include is recognized as a string + $tmpdir = $this->include; + $this->include = array(); + $this->include[] = $tmpdir; + $this->include[] = "tests"; + } + $this->php = $args->getValue('php'); + if ($this->php == "") { + $this->php = "php"; + } + $this->log_file = $args->getValue('log'); + if ($this->log_file == "") { + $this->log_file = false; + } + $this->quite = $args->getValue('quite'); + $this->setTestFiles(); + } + /** + * Prints message to the screen + * + * @param string $msg The message to print + * @param bool $print Whether to print or not print + * + * @return void + */ + function console($msg, $print=true) + { + if (!$this->quite) { + if ($print) { + echo $msg; } - if(in_array($key,$arguments)) - $ARGS[$key] = $arg; - else{ - console("Illegal argument: ".$key); - usage(); - } - - } elseif(ereg('-[a-zA-Z0-9]',$arg)) { - $arg = ereg_replace("-",'',$arg); - if(in_array($arg,$flags)) - { - $ARGS[$arg] = true; - } - else - { - console("Illegal flag: ".$arg); - usage(); - } } - + if ($this->log_file && $print) { + file_put_contents($this->log_file, $msg, FILE_APPEND); + } } -return $ARGS; -} -/** - * Prints help message and exits. - */ -function usage() -{ -console(<<<EOL -bench.php - Runs a sequence of algorithms to measure performance -Usage: bench [OPTIONS] + /** + * Sets the config for command line arguments + * + * @return void + */ + function setConfig() + { + $this->config = array( + 'memory-limit' => array('short' => 'm', + 'min' => 0, + 'max' => 1, + 'default' => 128, + 'desc' => 'Set the maximum memory usage.'), + 'debug' => array('short' => 'd', + 'max' => 0, + 'desc' => 'Switch to debug mode.'), + 'include' => array('min' => 0, + 'max' => -1, + 'desc' => 'Include additional test directories'), + 'help' => array('short' => 'h', + 'max' => 0, + 'desc' => 'Print this help message'), + 'php' => array('min' => 0, + 'max' => 1, + 'default' => 'php', + 'desc' => 'PHP binary path'), + 'quite' => array('short' => 'q', + 'max' => 0, + 'desc' => 'Don\'t produce any output'), + 'log' => array('min' => 0, + 'max' => 1, + 'default' => 'benchlog.txt', + 'desc' => 'Log file path') + ); + } -Options: - --help Print help message - -h - - -debug Debug mode. If combined with logging, - debug output will come to the log as well. - - --m=MEM Sets the maximum allowed memory to MEM, - default is 128. - - --include=DIR Includes DIR as a test directory. Files - with the filename structure of test_*.php - in this directory will be included in the - benchmark. Multiple directories can be - separated by commas (DIR,DIR,...,DIR). - - --log=FILE Logs output to FILE. Can be combined with - -debug. - - -q Sets benchmark to run quite. -EOL -); -die(); -} + /** + * Prints the help message for the benchmark + * + * @param array $args The arguments to be listed + * + * @return void + */ + function printHelp($args) + { + $header = "Php Benchmark Example\n". + 'Usage: '.basename($_SERVER['SCRIPT_NAME'])." [options]\n\n"; + if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { + echo Console_Getargs::getHelp($this->config, $header, $args->getMessage())."\n"; + } else if ($args->getCode() === CONSOLE_GETARGS_HELP) { + echo Console_Getargs::getHelp($this->config, $header)."\n"; + } + } + /** + * Return an array with all the paths to the test files found + * from the array of directories to search. It searches for files + * with the structure of test_*.php. It then strips the full path + * so the elements in the returning array will hold both the full + * path and the name of the script. + * Example: + * + * A file called test_array.php that lies in /home/user/tests can + * make the returning array look like this: + * + * array([0] => array ( [filename] => "/home/user/tests/test_array.php", + * [name] => "array.php") + * ); + * + * @return void + */ + function setTestFiles() + { + $files = array(); + foreach ($this->include as $dir) { + foreach (glob("$dir/test_*.php") as $filename) { + $t['filename'] = $filename; + preg_match('/test_(.+)[.]php$/i', $filename, $matches); + $t['name'] = $matches[1]; + $files[] = $t; + } + } + $this->test_files = $files; + } -error_reporting(E_ALL | E_STRICT); -set_time_limit(0); + /** + * Runs the benchmark + * + * @return void + */ + function run() + { + $timer = new Timer(); + $totaltime = 0; + $datetime = date("Y-m-d H:i:s", time()); -include("misc/timer.php"); -$arguments = array("m","include","php","log","help"); -$flags = array("debug","q"); -$MEMORY_LIMIT = 128; -$DEBUG = false; -$VERBOSE = true; -$test_dirs = array('tests'); -$LOG_FILE = false; -$PHP = "php"; -$user_arguments = arguments($argv,$flags,$arguments); + $this->console("--------------- Bench.php ".$datetime."--------------\n"); -foreach($user_arguments as $arg => $value) -{ -switch($arg) -{ - case 'm': $MEMORY_LIMIT = $value; break; - case 'debug': $DEBUG = true; break; - case 'include': - $dirs = explode(",",$value); - - foreach($dirs as $dir) - { - if($dir[strlen($dir)-1] == "/") - $test_dirs[] = substr($dir,0,strlen($dir)-1); - else - $test_dirs[] = $dir; - } - break; - case 'h': usage(); break; - case 'help': usage(); break; - case 'php': $PHP = $value; break; - case 'log':$LOG_FILE = $value; - break; - case 'q': $VERBOSE = false; break; -} -} + foreach ($this->test_files as $test) { + $cmd = "{$this->php} -d memory_limit={$this->memory_limit}M ".$test['filename']; + $this->console("$cmd\n", $this->debug); + $timer->start(); + $debug = `$cmd`; + $timer->stop(); + $this->console($debug, $this->debug); + $this->console("Results from ".$test['name'].": ".$timer->elapsed."\n"); + $totaltime += $timer->elapsed; + } -$tests_list = array(); -$results = array(); -define("VERBOSE",$VERBOSE); -define("DEBUG",$DEBUG); -define("LOG_FILE",$LOG_FILE); -$tests_list = get_test_files($test_dirs); -$length = 1; -$timer = new Timer(); -$total = 0; -$datetime = date("Y-m-d H:i:s",time()); -console("--------------- Bench.php ".$datetime."---------------------\n"); -foreach($tests_list as $test) -{ - $cmd = "$PHP -d memory_limit={$MEMORY_LIMIT}M ".$test['filename']; - console("$cmd\n",DEBUG); - $timer->start(); - $deb = `$cmd`; - $timer->stop(); - console($deb,DEBUG); - console("Results from ".$test['name'].": ".$timer->elapsed."\n"); - $total += $timer->elapsed; + $this->console("Total time for the benchmark: ".$totaltime." seconds\n"); - + $datetime = date("Y-m-d H:i:s", time()); + $this->console("-------------- END ".$datetime."---------------------\n"); + } } -console("Total time for the benchmark: ".$total." seconds\n"); -console("-------------- END ".$datetime."----------------------------\n"); +$bench = new Benchmark(); +$bench->run(); ?>
http://cvs.php.net/viewvc.cgi/php-benchmarks/benchcli/bench.php?r1=1.1&r2=1.2&diff_format=u Index: php-benchmarks/benchcli/bench.php diff -u php-benchmarks/benchcli/bench.php:1.1 php-benchmarks/benchcli/bench.php:1.2 --- php-benchmarks/benchcli/bench.php:1.1 Sun May 24 19:01:24 2009 +++ php-benchmarks/benchcli/bench.php Thu May 28 13:40:59 2009 @@ -1,196 +1,265 @@ <?php +// +----------------------------------------------------------------------+ +// | PHP Version 5 | +// +----------------------------------------------------------------------+ +// | Copyright (c) 2009 The PHP Group | +// +----------------------------------------------------------------------+ +// | This source file is subject to version 3.0 of the PHP license, | +// | that is available through the world-wide-web at the following url: | +// | http://www.php.net/license/3_0.txt. | +// | If you were unable to obtain it though the world-wide-web, please | +// | send a note to license@php.net so we can mail you a copy immediately | +// +----------------------------------------------------------------------+ +// | Author: Alexander Hjalmarsson <hjalle@php.net> | +// +----------------------------------------------------------------------+ +// + /** - * Handles output and logging options. - * @param $msg The message to be handled - * @param $print Whether it should be printed - * or not. - * @param $log File to log the output to. - * Set to false if logging is - * disabled. + * This benchmark measures the performance of PHP + * Example of usage: + * bench.php --log log.txt -d -m 256 + * + * @author Alexander Hjalmarsson <hjalle@php.net> + * @copyright 2009 + * @license http://www.php.net/license/3_0.txt PHP License 3.0 */ -function console($msg,$print = true) -{ - if(VERBOSE == true) - { - if($print == true) - { - echo $msg; - } - - } - if(LOG_FILE != false && $print) - { - $fp = fopen(LOG_FILE,'a'); - fwrite($fp,$msg); - fclose($fp); - } -} + + +error_reporting(E_ALL | E_NOTICE); +set_time_limit(0); /** - * Return an array with all the paths to the test files found - * from the array of directories to search. - * Returning array have this structure: - * array("/path/to/test/test_name.php)" => "name.php",...,); - * @param $test_dirs An array of directories to search in - * @return Array of test files and their names. + * This is the main class of the benchmark script */ -function get_test_files($test_dirs) +class Benchmark { - $files = array(); - foreach($test_dirs as $dir) - { - foreach (glob("$dir/test_*.php") as $filename) { - $t['filename'] = $filename; - preg_match('/^'.preg_quote($dir,'/').'\/test_(.+)[.]php$/i', $filename, $matches); - $t['name'] = $matches[1]; - $files[] = $t; - } - } - - return $files; -} -/** - * Parses the arguments and returns them in a better structure. - * Flags enabled will get the value 1 and the arguments will get - * the value specified from the console. - * @param $argv The argument array ($argv) - * @param $flags Existing flag options - * @param $arguments Existing argument options - * @return If flags and arguments were correct, - * it will return an array with $key -> $value - * structure. - */ -function arguments($argv,$flags,$arguments) { - $ARGS = array(); - foreach ($argv as $arg) { - if (ereg('--[a-zA-Z0-9]*=.*',$arg)) { - $str = split("=",$arg); $arg = ''; - $key = ereg_replace("--",'',$str[0]); - for ( $i = 1; $i < count($str); $i++ ) { - $arg .= $str[$i]; + /** + * Maximum memory usage for php test files. + * @var int + */ + var $memory_limit; + + /** + * Debug switch + * @var boolean + */ + var $debug; + + /** + * Directories to search for php test files. + * @var array + */ + var $include; + + /** + * Path to the php binary + * @var string + */ + var $php; + + /** + * The config for command line arguments + * @var array + */ + var $config; + + /** + * The log file for output + * @var string + */ + var $log_file; + + /** + * Produce output or not + */ + var $quite; + + /** + * Holds the test files that will be tested + * @var array + */ + var $test_files; + + /** + * Constructor for the benchmark class. Uses the PEAR package + * Console_getargs for command line handling. + * + * @return void + */ + function Benchmark() + { + include_once 'misc/getargs.php'; + include_once 'misc/timer.php'; + + $this->setConfig(); + $args =& Console_Getargs::factory($this->config); + + if (PEAR::isError($args) || $args->getValue('help')) { + $this->printHelp($args); + exit; + } + + $this->debug = $args->getValue('debug'); + $this->memory_limit = $args->getValue('memory-limit'); + if ($this->memory_limit == "") { + $this->memory_limit = 128; + } + $this->include = $args->getValue('include'); + if (is_array($this->include)) { + $this->include[] = "tests"; // Append default directory + } else { + //user has one directory as input, therefore + //$this->include is recognized as a string + $tmpdir = $this->include; + $this->include = array(); + $this->include[] = $tmpdir; + $this->include[] = "tests"; + } + $this->php = $args->getValue('php'); + if ($this->php == "") { + $this->php = "php"; + } + $this->log_file = $args->getValue('log'); + if ($this->log_file == "") { + $this->log_file = false; + } + $this->quite = $args->getValue('quite'); + $this->setTestFiles(); + } + /** + * Prints message to the screen + * + * @param string $msg The message to print + * @param bool $print Whether to print or not print + * + * @return void + */ + function console($msg, $print=true) + { + if (!$this->quite) { + if ($print) { + echo $msg; } - if(in_array($key,$arguments)) - $ARGS[$key] = $arg; - else{ - console("Illegal argument: ".$key); - usage(); - } - - } elseif(ereg('-[a-zA-Z0-9]',$arg)) { - $arg = ereg_replace("-",'',$arg); - if(in_array($arg,$flags)) - { - $ARGS[$arg] = true; - } - else - { - console("Illegal flag: ".$arg); - usage(); - } } - + if ($this->log_file && $print) { + file_put_contents($this->log_file, $msg, FILE_APPEND); + } } -return $ARGS; -} -/** - * Prints help message and exits. - */ -function usage() -{ -console(<<<EOL -bench.php - Runs a sequence of algorithms to measure performance -Usage: bench [OPTIONS] + /** + * Sets the config for command line arguments + * + * @return void + */ + function setConfig() + { + $this->config = array( + 'memory-limit' => array('short' => 'm', + 'min' => 0, + 'max' => 1, + 'default' => 128, + 'desc' => 'Set the maximum memory usage.'), + 'debug' => array('short' => 'd', + 'max' => 0, + 'desc' => 'Switch to debug mode.'), + 'include' => array('min' => 0, + 'max' => -1, + 'desc' => 'Include additional test directories'), + 'help' => array('short' => 'h', + 'max' => 0, + 'desc' => 'Print this help message'), + 'php' => array('min' => 0, + 'max' => 1, + 'default' => 'php', + 'desc' => 'PHP binary path'), + 'quite' => array('short' => 'q', + 'max' => 0, + 'desc' => 'Don\'t produce any output'), + 'log' => array('min' => 0, + 'max' => 1, + 'default' => 'benchlog.txt', + 'desc' => 'Log file path') + ); + } -Options: - --help Print help message - -h - - -debug Debug mode. If combined with logging, - debug output will come to the log as well. - - --m=MEM Sets the maximum allowed memory to MEM, - default is 128. - - --include=DIR Includes DIR as a test directory. Files - with the filename structure of test_*.php - in this directory will be included in the - benchmark. Multiple directories can be - separated by commas (DIR,DIR,...,DIR). - - --log=FILE Logs output to FILE. Can be combined with - -debug. - - -q Sets benchmark to run quite. -EOL -); -die(); -} + /** + * Prints the help message for the benchmark + * + * @param array $args The arguments to be listed + * + * @return void + */ + function printHelp($args) + { + $header = "Php Benchmark Example\n". + 'Usage: '.basename($_SERVER['SCRIPT_NAME'])." [options]\n\n"; + if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) { + echo Console_Getargs::getHelp($this->config, $header, $args->getMessage())."\n"; + } else if ($args->getCode() === CONSOLE_GETARGS_HELP) { + echo Console_Getargs::getHelp($this->config, $header)."\n"; + } + } + /** + * Return an array with all the paths to the test files found + * from the array of directories to search. It searches for files + * with the structure of test_*.php. It then strips the full path + * so the elements in the returning array will hold both the full + * path and the name of the script. + * Example: + * + * A file called test_array.php that lies in /home/user/tests can + * make the returning array look like this: + * + * array([0] => array ( [filename] => "/home/user/tests/test_array.php", + * [name] => "array.php") + * ); + * + * @return void + */ + function setTestFiles() + { + $files = array(); + foreach ($this->include as $dir) { + foreach (glob("$dir/test_*.php") as $filename) { + $t['filename'] = $filename; + preg_match('/test_(.+)[.]php$/i', $filename, $matches); + $t['name'] = $matches[1]; + $files[] = $t; + } + } + $this->test_files = $files; + } -error_reporting(E_ALL | E_STRICT); -set_time_limit(0); + /** + * Runs the benchmark + * + * @return void + */ + function run() + { + $timer = new Timer(); + $totaltime = 0; + $datetime = date("Y-m-d H:i:s", time()); -include("misc/timer.php"); -$arguments = array("m","include","php","log","help"); -$flags = array("debug","q"); -$MEMORY_LIMIT = 128; -$DEBUG = false; -$VERBOSE = true; -$test_dirs = array('tests'); -$LOG_FILE = false; -$PHP = "php"; -$user_arguments = arguments($argv,$flags,$arguments); + $this->console("--------------- Bench.php ".$datetime."--------------\n"); -foreach($user_arguments as $arg => $value) -{ -switch($arg) -{ - case 'm': $MEMORY_LIMIT = $value; break; - case 'debug': $DEBUG = true; break; - case 'include': - $dirs = explode(",",$value); - - foreach($dirs as $dir) - { - if($dir[strlen($dir)-1] == "/") - $test_dirs[] = substr($dir,0,strlen($dir)-1); - else - $test_dirs[] = $dir; - } - break; - case 'h': usage(); break; - case 'help': usage(); break; - case 'php': $PHP = $value; break; - case 'log':$LOG_FILE = $value; - break; - case 'q': $VERBOSE = false; break; -} -} + foreach ($this->test_files as $test) { + $cmd = "{$this->php} -d memory_limit={$this->memory_limit}M ".$test['filename']; + $this->console("$cmd\n", $this->debug); + $timer->start(); + $debug = `$cmd`; + $timer->stop(); + $this->console($debug, $this->debug); + $this->console("Results from ".$test['name'].": ".$timer->elapsed."\n"); + $totaltime += $timer->elapsed; + } -$tests_list = array(); -$results = array(); -define("VERBOSE",$VERBOSE); -define("DEBUG",$DEBUG); -define("LOG_FILE",$LOG_FILE); -$tests_list = get_test_files($test_dirs); -$length = 1; -$timer = new Timer(); -$total = 0; -$datetime = date("Y-m-d H:i:s",time()); -console("--------------- Bench.php ".$datetime."---------------------\n"); -foreach($tests_list as $test) -{ - $cmd = "$PHP -d memory_limit={$MEMORY_LIMIT}M ".$test['filename']; - console("$cmd\n",DEBUG); - $timer->start(); - $deb = `$cmd`; - $timer->stop(); - console($deb,DEBUG); - console("Results from ".$test['name'].": ".$timer->elapsed."\n"); - $total += $timer->elapsed; + $this->console("Total time for the benchmark: ".$totaltime." seconds\n"); - + $datetime = date("Y-m-d H:i:s", time()); + $this->console("-------------- END ".$datetime."---------------------\n"); + } } -console("Total time for the benchmark: ".$total." seconds\n"); -console("-------------- END ".$datetime."----------------------------\n"); +$bench = new Benchmark(); +$bench->run(); ?>