Skip to content

Commit 1617ed0

Browse files
committed
:octocat: run PHPCS on the benchmarks
1 parent 071bc42 commit 1617ed0

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

benchmark/BenchmarkAbstract.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ abstract class BenchmarkAbstract{
3333
protected const ECC_LEVELS = [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H];
3434
protected const DATAMODES = Mode::INTERFACES;
3535

36+
/** @var array<int, string> */
3637
protected array $dataModeData;
3738
protected string $testData;
3839
protected QROptions $options;
@@ -45,7 +46,7 @@ abstract class BenchmarkAbstract{
4546
protected string $modeFQCN;
4647

4748
/**
48-
*
49+
* @throws \RuntimeException
4950
*/
5051
public function __construct(){
5152

@@ -64,6 +65,8 @@ public function __construct(){
6465

6566
/**
6667
* Generates test data strings for each mode
68+
*
69+
* @return array<int, string>
6770
*/
6871
protected function generateDataModeData():array{
6972
return [
@@ -90,6 +93,8 @@ protected function getData(Version $version, EccLevel $eccLevel, int $mode):stri
9093

9194
/**
9295
* Initializes a QROptions instance and assigns it to its temp property
96+
*
97+
* @param array<string, mixed> $options
9398
*/
9499
protected function initQROptions(array $options):void{
95100
$this->options = new QROptions($options);
@@ -114,6 +119,8 @@ public function generateTestData():void{
114119

115120
/**
116121
* Assigns the parameter array from the providers to properties and enforces the types
122+
*
123+
* @param array<string, mixed> $params
117124
*/
118125
public function assignParams(array $params):void{
119126
foreach($params as $k => $v){

benchmark/generate-html.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@
3030
require_once __DIR__.'/parse-common.php';
3131

3232
if(!file_exists(FILE.'.json')){
33-
throw new RuntimeException('invalid benchmark report');
33+
throw new RuntimeException('invalid benchmark report [file_exists()]');
3434
}
3535

36-
$json = json_decode(file_get_contents(FILE.'.json'), true);
36+
$data = file_get_contents(FILE.'.json');
37+
38+
if($data === false){
39+
throw new RuntimeException('invalid benchmark report [file_get_contents()]');
40+
}
41+
42+
$json = json_decode($data, true);
3743

3844
$htmlHead = '<!DOCTYPE html>
3945
<html lang="en">
@@ -63,7 +69,12 @@
6369
$html['index'][] = '</thead><tbody>';
6470

6571
$html['index'][] = sprintf('<tr><td>date</td><td style="text-align: left;">%s %s</td></tr>', $suite['date'], $suite['time']);
66-
$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']);
72+
$html['index'][] = sprintf(
73+
'<tr><td>environment</td><td style="text-align: left;">%s %s, %s</td></tr>',
74+
$env['uname_os'],
75+
$env['uname_version'],
76+
$env['uname_machine'],
77+
);
6778
$html['index'][] = sprintf('<tr><td>tag</td><td style="text-align: left;">%s</td></tr>', htmlspecialchars($suite['tag']));
6879

6980
foreach(['php_version', 'php_ini', 'php_extensions', 'php_xdebug', 'opcache_extension_loaded', 'opcache_enabled'] as $field){

benchmark/parse-common.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
const BUILDDIR = __DIR__.'/../.build/phpbench';
2626
const FILE = BUILDDIR.'/benchmark'; // without extension
2727

28+
/**
29+
* @param array<int, array<int, mixed>> $variants
30+
*/
2831
function parseVariants(array $variants):string{
2932
$data = [];
3033

@@ -65,6 +68,11 @@ function parseVariants(array $variants):string{
6568
return implode("\n", $table);
6669
}
6770

71+
/**
72+
* @param array<int, array<int, mixed>> $results
73+
*
74+
* @return array{0: float, 1: int}
75+
*/
6876
function parseVariantResults(array $results):array{
6977
$iterations = count($results);
7078
$mem_peak = array_column($results, 'mem_peak');

benchmark/parse-result.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
require_once __DIR__.'/parse-common.php';
3030

3131
const SEPARATOR = '|';
32+
const BOOLEANS = ['has_baseline', 'env_php_xdebug', 'env_opcache_extension_loaded', 'env_opcache_enabled'];
33+
const INTEGERS = [
34+
'variant_index', 'variant_revs', 'variant_iterations', 'iteration_index', 'result_time_net',
35+
'result_time_revs', 'result_mem_peak', 'result_mem_real', 'result_mem_final',
36+
];
37+
const FLOATS = [
38+
'env_sampler_nothing', 'env_sampler_md5', 'env_sampler_file_rw', 'result_time_avg',
39+
'result_comp_z_value', 'result_comp_deviation',
40+
];
41+
const ARRAYS = ['subject_groups', 'variant_params'];
42+
3243

3344
function parseLine(string $line):array{
3445
return array_map(fn(string $str):string => trim($str, "\ \n\r\t\v\0\""), explode(SEPARATOR, $line));
@@ -42,22 +53,22 @@ function parseLine(string $line):array{
4253
foreach($parsed as $i => $result){
4354

4455
// booleans
45-
foreach(['has_baseline', 'env_php_xdebug', 'env_opcache_extension_loaded', 'env_opcache_enabled'] as $bool){
56+
foreach(BOOLEANS as $bool){
4657
$result[$bool] = (bool)$result[$bool];
4758
}
4859

4960
// integers
50-
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){
61+
foreach(INTEGERS as $int){
5162
$result[$int] = intval($result[$int]);
5263
}
5364

5465
// floats
55-
foreach(['env_sampler_nothing', 'env_sampler_md5', 'env_sampler_file_rw', 'result_time_avg', 'result_comp_z_value', 'result_comp_deviation'] as $float){
66+
foreach(FLOATS as $float){
5667
$result[$float] = floatval($result[$float]);
5768
}
5869

5970
// arrays
60-
foreach(['subject_groups', 'variant_params'] as $array){
71+
foreach(ARRAYS as $array){
6172
$val = trim($result[$array], '"[]');
6273

6374
if($val === ''){
@@ -111,21 +122,24 @@ function parseLine(string $line):array{
111122
continue;
112123
}
113124

125+
// phpcs:ignore
114126
$json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']][str_replace('subject_', '', $k)] = $v;
115127
}
116128

117129
// add variants
118130
if(str_starts_with($k, 'variant_')){
131+
// phpcs:ignore
119132
$json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']][str_replace('variant_', '', $k)] = $v;
120133
}
121134

122135
// add benchmark results per variant
123136
if(str_starts_with($k, 'result_')){
137+
// phpcs:ignore
124138
$json['benchmark'][$result['benchmark_name']]['subjects'][$result['subject_name']]['variants'][$result['variant_index']]['results'][$result['result_index']][str_replace('result_', '', $k)] = $v;
125139
}
126140

127141
}
128142

129143
}
130144

131-
file_put_contents(FILE.'.json', json_encode($json, (JSON_PRESERVE_ZERO_FRACTION|JSON_PRETTY_PRINT)));
145+
file_put_contents(FILE.'.json', json_encode($json, (JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT)));

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
55
<description>php-qrcode rules for phpcs</description>
66

7+
<file>benchmark</file>
78
<file>examples</file>
89
<file>src</file>
910
<file>tests</file>
@@ -209,6 +210,7 @@
209210

210211
<rule ref="PSR1.Files.SideEffects">
211212
<exclude-pattern>examples</exclude-pattern>
213+
<exclude-pattern>benchmark</exclude-pattern>
212214
</rule>
213215

214216

0 commit comments

Comments
 (0)