Pārlūkot izejas kodu

:octocat: run PHPCS on the benchmarks

smiley 1 gadu atpakaļ
vecāks
revīzija
1617ed0168

+ 8 - 1
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<int, string> */
 	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<int, string>
 	 */
 	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<string, mixed> $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<string, mixed> $params
 	 */
 	public function assignParams(array $params):void{
 		foreach($params as $k => $v){

+ 14 - 3
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 = '<!DOCTYPE html>
 <html lang="en">
@@ -63,7 +69,12 @@ $html['index'][] = '<tr><th>Name</th><th>Value</th></tr>';
 $html['index'][] = '</thead><tbody>';
 
 $html['index'][] = sprintf('<tr><td>date</td><td style="text-align: left;">%s %s</td></tr>', $suite['date'], $suite['time']);
-$html['index'][] = sprintf('<tr><td>environment</td><td style="text-align: left;">%s %s, %s</td></tr>', $env['uname_os'], $env['uname_version'], $env['uname_machine']);
+$html['index'][] = sprintf(
+	'<tr><td>environment</td><td style="text-align: left;">%s %s, %s</td></tr>',
+	$env['uname_os'],
+	$env['uname_version'],
+	$env['uname_machine'],
+);
 $html['index'][] = sprintf('<tr><td>tag</td><td style="text-align: left;">%s</td></tr>', htmlspecialchars($suite['tag']));
 
 foreach(['php_version', 'php_ini', 'php_extensions', 'php_xdebug', 'opcache_extension_loaded', 'opcache_enabled'] as $field){

+ 8 - 0
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<int, array<int, mixed>> $variants
+ */
 function parseVariants(array $variants):string{
 	$data = [];
 
@@ -65,6 +68,11 @@ function parseVariants(array $variants):string{
 	return implode("\n", $table);
 }
 
+/**
+ * @param array<int, array<int, mixed>> $results
+ *
+ * @return array{0: float, 1: int}
+ */
 function parseVariantResults(array $results):array{
 	$iterations = count($results);
 	$mem_peak   = array_column($results, 'mem_peak');

+ 19 - 5
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)));

+ 2 - 0
phpcs.xml.dist

@@ -4,6 +4,7 @@
          xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
 	<description>php-qrcode rules for phpcs</description>
 
+	<file>benchmark</file>
 	<file>examples</file>
 	<file>src</file>
 	<file>tests</file>
@@ -209,6 +210,7 @@
 
 	<rule ref="PSR1.Files.SideEffects">
 		<exclude-pattern>examples</exclude-pattern>
+		<exclude-pattern>benchmark</exclude-pattern>
 	</rule>