diff --git a/benchmark/BenchmarkAbstract.php b/benchmark/BenchmarkAbstract.php index 3ee625a0b..451320e93 100644 --- a/benchmark/BenchmarkAbstract.php +++ b/benchmark/BenchmarkAbstract.php @@ -33,6 +33,7 @@ abstract class BenchmarkAbstract{ protected const ECC_LEVELS = [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]; protected const DATAMODES = Mode::INTERFACES; + /** @var array */ protected array $dataModeData; protected string $testData; protected QROptions $options; @@ -45,7 +46,7 @@ abstract class BenchmarkAbstract{ protected string $modeFQCN; /** - * + * @throws \RuntimeException */ public function __construct(){ @@ -64,6 +65,8 @@ abstract class BenchmarkAbstract{ /** * Generates test data strings for each mode + * + * @return array */ protected function generateDataModeData():array{ return [ @@ -90,6 +93,8 @@ abstract class BenchmarkAbstract{ /** * Initializes a QROptions instance and assigns it to its temp property + * + * @param array $options */ protected function initQROptions(array $options):void{ $this->options = new QROptions($options); @@ -114,6 +119,8 @@ abstract class BenchmarkAbstract{ /** * Assigns the parameter array from the providers to properties and enforces the types + * + * @param array $params */ public function assignParams(array $params):void{ foreach($params as $k => $v){ diff --git a/benchmark/generate-html.php b/benchmark/generate-html.php index cfd5d7ccc..a8cdb1741 100644 --- a/benchmark/generate-html.php +++ b/benchmark/generate-html.php @@ -30,10 +30,16 @@ use function sprintf; require_once __DIR__.'/parse-common.php'; if(!file_exists(FILE.'.json')){ - throw new RuntimeException('invalid benchmark report'); + throw new RuntimeException('invalid benchmark report [file_exists()]'); } -$json = json_decode(file_get_contents(FILE.'.json'), true); +$data = file_get_contents(FILE.'.json'); + +if($data === false){ + throw new RuntimeException('invalid benchmark report [file_get_contents()]'); +} + +$json = json_decode($data, true); $htmlHead = ' @@ -63,7 +69,12 @@ $html['index'][] = 'NameValue'; $html['index'][] = ''; $html['index'][] = sprintf('date%s %s', $suite['date'], $suite['time']); -$html['index'][] = sprintf('environment%s %s, %s', $env['uname_os'], $env['uname_version'], $env['uname_machine']); +$html['index'][] = sprintf( + 'environment%s %s, %s', + $env['uname_os'], + $env['uname_version'], + $env['uname_machine'], +); $html['index'][] = sprintf('tag%s', htmlspecialchars($suite['tag'])); foreach(['php_version', 'php_ini', 'php_extensions', 'php_xdebug', 'opcache_extension_loaded', 'opcache_enabled'] as $field){ diff --git a/benchmark/parse-common.php b/benchmark/parse-common.php index eacbc2357..cbb0d5428 100644 --- a/benchmark/parse-common.php +++ b/benchmark/parse-common.php @@ -25,6 +25,9 @@ use const SORT_NUMERIC; const BUILDDIR = __DIR__.'/../.build/phpbench'; const FILE = BUILDDIR.'/benchmark'; // without extension +/** + * @param array> $variants + */ function parseVariants(array $variants):string{ $data = []; @@ -65,6 +68,11 @@ function parseVariants(array $variants):string{ return implode("\n", $table); } +/** + * @param array> $results + * + * @return array{0: float, 1: int} + */ function parseVariantResults(array $results):array{ $iterations = count($results); $mem_peak = array_column($results, 'mem_peak'); diff --git a/benchmark/parse-result.php b/benchmark/parse-result.php index 931e97031..eea1616db 100644 --- a/benchmark/parse-result.php +++ b/benchmark/parse-result.php @@ -29,6 +29,17 @@ use const JSON_PRETTY_PRINT; require_once __DIR__.'/parse-common.php'; const SEPARATOR = '|'; +const BOOLEANS = ['has_baseline', 'env_php_xdebug', 'env_opcache_extension_loaded', 'env_opcache_enabled']; +const INTEGERS = [ + 'variant_index', 'variant_revs', 'variant_iterations', 'iteration_index', 'result_time_net', + 'result_time_revs', 'result_mem_peak', 'result_mem_real', 'result_mem_final', +]; +const FLOATS = [ + 'env_sampler_nothing', 'env_sampler_md5', 'env_sampler_file_rw', 'result_time_avg', + 'result_comp_z_value', 'result_comp_deviation', +]; +const ARRAYS = ['subject_groups', 'variant_params']; + function parseLine(string $line):array{ return array_map(fn(string $str):string => trim($str, "\ \n\r\t\v\0\""), explode(SEPARATOR, $line)); @@ -42,22 +53,22 @@ $json = ['env' => [], 'suite' => [], 'benchmark' => []]; foreach($parsed as $i => $result){ // booleans - foreach(['has_baseline', 'env_php_xdebug', 'env_opcache_extension_loaded', 'env_opcache_enabled'] as $bool){ + foreach(BOOLEANS as $bool){ $result[$bool] = (bool)$result[$bool]; } // integers - foreach(['variant_index', 'variant_revs', 'variant_iterations', 'iteration_index', 'result_time_net', 'result_time_revs', 'result_mem_peak', 'result_mem_real', 'result_mem_final'] as $int){ + foreach(INTEGERS as $int){ $result[$int] = intval($result[$int]); } // floats - foreach(['env_sampler_nothing', 'env_sampler_md5', 'env_sampler_file_rw', 'result_time_avg', 'result_comp_z_value', 'result_comp_deviation'] as $float){ + foreach(FLOATS as $float){ $result[$float] = floatval($result[$float]); } // arrays - foreach(['subject_groups', 'variant_params'] as $array){ + foreach(ARRAYS as $array){ $val = trim($result[$array], '"[]'); if($val === ''){ @@ -111,16 +122,19 @@ foreach($parsed as $i => $result){ continue; } + // phpcs:ignore $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']][str_replace('subject_', '', $k)] = $v; } // add variants if(str_starts_with($k, 'variant_')){ + // phpcs:ignore $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']][str_replace('variant_', '', $k)] = $v; } // add benchmark results per variant if(str_starts_with($k, 'result_')){ + // phpcs:ignore $json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']]['results'][$result['result_index']][str_replace('result_', '', $k)] = $v; } @@ -128,4 +142,4 @@ foreach($parsed as $i => $result){ } -file_put_contents(FILE.'.json', json_encode($json, (JSON_PRESERVE_ZERO_FRACTION|JSON_PRETTY_PRINT))); +file_put_contents(FILE.'.json', json_encode($json, (JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT))); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 38624bc4f..8819dc58c 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -4,6 +4,7 @@ xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> php-qrcode rules for phpcs + benchmark examples src tests @@ -209,6 +210,7 @@ examples + benchmark