output.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Tests the performance of the built-in output classes
  4. *
  5. * @created 16.10.2023
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2023 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Performance;
  11. use chillerlan\QRCode\{QRCode, QROptions};
  12. use chillerlan\QRCode\Common\{EccLevel, Mode, Version};
  13. use chillerlan\QRCode\Output\QROutputInterface;
  14. use chillerlan\QRCodeTest\QRMaxLengthTrait;
  15. use Generator;
  16. use function file_put_contents;
  17. use function json_encode;
  18. use function printf;
  19. use function sprintf;
  20. use function str_repeat;
  21. use function str_replace;
  22. use function substr;
  23. use const JSON_PRETTY_PRINT;
  24. require_once __DIR__.'/../../vendor/autoload.php';
  25. // excerpt from QRCodeReaderTestAbstract
  26. $generator = new class () {
  27. use QRMaxLengthTrait;
  28. public function dataProvider():Generator{
  29. $str = str_repeat('https://www.youtube.com/watch?v=dQw4w9WgXcQ ', 100);
  30. $eccLevel = new EccLevel(EccLevel::L);
  31. for($v = 5; $v <= 40; $v += 5){
  32. $version = new Version($v);
  33. foreach(QROutputInterface::MODES as $outputType => $FQN){
  34. $name = str_replace('chillerlan\\QRCode\\Output\\', '', $FQN);
  35. yield sprintf('version %2s: %-14s', $version, $name) => [
  36. $version->getVersionNumber(),
  37. $outputType,
  38. $FQN,
  39. substr($str, 0, self::getMaxLengthForMode(Mode::BYTE, $version, $eccLevel)),
  40. $name,
  41. ];
  42. }
  43. }
  44. }
  45. };
  46. $test = new PerformanceTest(100);
  47. $json = [];
  48. foreach($generator->dataProvider() as $key => [$version, $outputType, $FQN, $data, $name]){
  49. $options = new QROptions([
  50. 'version' => $version,
  51. 'outputType' => $outputType,
  52. 'connectPaths' => true,
  53. 'drawLightModules' => true,
  54. 'drawCircularModules' => true,
  55. 'gdImageUseUpscale' => false, // set to false to allow proper comparison
  56. ]);
  57. $qrcode = new QRCode($options);
  58. $qrcode->addByteSegment($data);
  59. $matrix = $qrcode->getQRMatrix();
  60. $test->run(fn() => (new $FQN($options, $matrix))->dump());
  61. printf("%s: %8.3fms\n", $key, $test->getResult());
  62. $json[$name][$version] = $test->getResult();
  63. }
  64. file_put_contents(__DIR__.'/performance_output.json', json_encode($json, JSON_PRETTY_PRINT));