|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class BenchmarkAbstract |
| 4 | + * |
| 5 | + * @created 23.04.2024 |
| 6 | + * @author smiley <[email protected]> |
| 7 | + * @copyright 2024 smiley |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace chillerlan\QRCodeBenchmark; |
| 13 | + |
| 14 | +use chillerlan\QRCode\{QRCode, QROptions}; |
| 15 | +use chillerlan\QRCode\Common\{EccLevel, Mode, Version}; |
| 16 | +use chillerlan\QRCode\Data\QRMatrix; |
| 17 | +use chillerlan\QRCodeTest\QRMaxLengthTrait; |
| 18 | +use PhpBench\Attributes\{Iterations, ParamProviders, Revs, Warmup}; |
| 19 | +use Generator, RuntimeException; |
| 20 | +use function extension_loaded, is_dir, mb_substr, mkdir, sprintf, str_repeat, str_replace; |
| 21 | + |
| 22 | +/** |
| 23 | + * The abstract benchmark with common methods |
| 24 | + */ |
| 25 | +#[Iterations(3)] |
| 26 | +#[Warmup(3)] |
| 27 | +#[Revs(100)] |
| 28 | +#[ParamProviders(['versionProvider', 'eccLevelProvider', 'dataModeProvider'])] |
| 29 | +abstract class BenchmarkAbstract{ |
| 30 | + use QRMaxLengthTrait; |
| 31 | + |
| 32 | + protected const BUILDDIR = __DIR__.'/../.build/phpbench/'; |
| 33 | + protected const ECC_LEVELS = [EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H]; |
| 34 | + protected const DATAMODES = Mode::INTERFACES; |
| 35 | + |
| 36 | + protected array $dataModeData; |
| 37 | + protected string $testData; |
| 38 | + protected QROptions $options; |
| 39 | + protected QRMatrix $matrix; |
| 40 | + |
| 41 | + // properties from data providers |
| 42 | + protected Version $version; |
| 43 | + protected EccLevel $eccLevel; |
| 44 | + protected int $mode; |
| 45 | + protected string $modeFQCN; |
| 46 | + |
| 47 | + /** |
| 48 | + * |
| 49 | + */ |
| 50 | + public function __construct(){ |
| 51 | + |
| 52 | + foreach(['gd', 'imagick'] as $ext){ |
| 53 | + if(!extension_loaded($ext)){ |
| 54 | + throw new RuntimeException(sprintf('ext-%s not loaded', $ext)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if(!is_dir(self::BUILDDIR)){ |
| 59 | + mkdir(directory: self::BUILDDIR, recursive: true); |
| 60 | + } |
| 61 | + |
| 62 | + $this->dataModeData = $this->generateDataModeData(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Generates test data strings for each mode |
| 67 | + */ |
| 68 | + protected function generateDataModeData():array{ |
| 69 | + return [ |
| 70 | + Mode::NUMBER => str_repeat('0123456789', 750), |
| 71 | + Mode::ALPHANUM => str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 100), |
| 72 | + Mode::KANJI => str_repeat('漂う花の香り', 350), |
| 73 | + Mode::HANZI => str_repeat('无可奈何燃花作香', 250), |
| 74 | + Mode::BYTE => str_repeat('https://www.youtube.com/watch?v=dQw4w9WgXcQ ', 100), |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Generates a test max-length data string for the given version, ecc level and data mode |
| 80 | + */ |
| 81 | + protected function getData(Version $version, EccLevel $eccLevel, int $mode):string{ |
| 82 | + $maxLength = self::getMaxLengthForMode($mode, $version, $eccLevel); |
| 83 | + |
| 84 | + if($mode === Mode::KANJI || $mode === Mode::HANZI){ |
| 85 | + return mb_substr($this->dataModeData[$mode], 0, $maxLength); |
| 86 | + } |
| 87 | + |
| 88 | + return mb_substr($this->dataModeData[$mode], 0, $maxLength, '8bit'); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Initializes a QROptions instance and assigns it to its temp property |
| 93 | + */ |
| 94 | + protected function initQROptions(array $options):void{ |
| 95 | + $this->options = new QROptions($options); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Initializes a QRMatrix instance and assigns it to its temp property |
| 100 | + */ |
| 101 | + public function initMatrix():void{ |
| 102 | + $this->matrix = (new QRCode($this->options)) |
| 103 | + ->addByteSegment($this->testData) |
| 104 | + ->getQRMatrix() |
| 105 | + ; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Generates a test data string and assigns it to its temp property |
| 110 | + */ |
| 111 | + public function generateTestData():void{ |
| 112 | + $this->testData = $this->getData($this->version, $this->eccLevel, $this->mode); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Assigns the parameter array from the providers to properties and enforces the types |
| 117 | + */ |
| 118 | + public function assignParams(array $params):void{ |
| 119 | + foreach($params as $k => $v){ |
| 120 | + $this->{$k} = $v; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public function versionProvider():Generator{ |
| 125 | + for($v = 1; $v <= 40; $v++){ |
| 126 | + $version = new Version($v); |
| 127 | + |
| 128 | + yield (string)$version => ['version' => $version]; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public function eccLevelProvider():Generator{ |
| 133 | + foreach(static::ECC_LEVELS as $ecc){ |
| 134 | + $eccLevel = new EccLevel($ecc); |
| 135 | + |
| 136 | + yield (string)$eccLevel => ['eccLevel' => $eccLevel]; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public function dataModeProvider():Generator{ |
| 141 | + foreach(static::DATAMODES as $mode => $modeFQCN){ |
| 142 | + $name = str_replace('chillerlan\\QRCode\\Data\\', '', $modeFQCN); |
| 143 | + |
| 144 | + yield $name => ['mode' => $mode, 'modeFQCN' => $modeFQCN]; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments