BenchmarkAbstract.php 3.9 KB

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