output.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 $FQN){
  34. $name = str_replace('chillerlan\\QRCode\\Output\\', '', $FQN);
  35. yield sprintf('version %2s: %-14s', $version, $name) => [
  36. $version->getVersionNumber(),
  37. $FQN,
  38. substr($str, 0, self::getMaxLengthForMode(Mode::BYTE, $version, $eccLevel)),
  39. $name,
  40. ];
  41. }
  42. }
  43. }
  44. };
  45. $test = new PerformanceTest(100);
  46. $json = [];
  47. foreach($generator->dataProvider() as $key => [$version, $FQN, $data, $name]){
  48. $options = new QROptions([
  49. 'version' => $version,
  50. 'outputInterface' => $FQN,
  51. 'connectPaths' => true,
  52. 'drawLightModules' => true,
  53. 'drawCircularModules' => true,
  54. 'gdImageUseUpscale' => false, // set to false to allow proper comparison
  55. ]);
  56. $qrcode = new QRCode($options);
  57. $qrcode->addByteSegment($data);
  58. $matrix = $qrcode->getQRMatrix();
  59. $test->run(fn() => (new $FQN($options, $matrix))->dump());
  60. printf("%s: %8.3fms\n", $key, $test->getResult());
  61. $json[$name][$version] = $test->getResult();
  62. }
  63. file_put_contents(__DIR__.'/performance_output.json', json_encode($json, JSON_PRETTY_PRINT));