DecoderBenchmark.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Class DecoderBenchmark
  4. *
  5. * @created 26.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\Common\{GDLuminanceSource, IMagickLuminanceSource, Mode};
  13. use chillerlan\QRCode\Data\Byte;
  14. use chillerlan\QRCode\Decoder\{Decoder, DecoderResult};
  15. use chillerlan\QRCode\Output\QRGdImagePNG;
  16. use chillerlan\QRCode\QRCodeException;
  17. use PhpBench\Attributes\{AfterMethods, BeforeMethods, Subject};
  18. use RuntimeException;
  19. /**
  20. * Tests the performance of the QR Code reader/decoder
  21. */
  22. final class DecoderBenchmark extends BenchmarkAbstract{
  23. protected const DATAMODES = [Mode::BYTE => Byte::class];
  24. private string $imageBlob;
  25. private DecoderResult $result;
  26. public function initOptions():void{
  27. $options = [
  28. 'version' => $this->version->getVersionNumber(),
  29. 'eccLevel' => $this->eccLevel->getLevel(),
  30. 'scale' => 2,
  31. 'imageTransparent' => false,
  32. 'outputBase64' => false,
  33. ];
  34. $this->initQROptions($options);
  35. }
  36. public function generateImageBlob():void{
  37. $this->imageBlob = (new QRGdImagePNG($this->options, $this->matrix))->dump();
  38. }
  39. public function checkReaderResult():void{
  40. if($this->result->data !== $this->testData){
  41. throw new RuntimeException('invalid reader result');
  42. }
  43. }
  44. /**
  45. * Tests the performance of the GD reader
  46. */
  47. #[Subject]
  48. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions', 'initMatrix', 'generateImageBlob'])]
  49. #[AfterMethods(['checkReaderResult'])]
  50. public function GDLuminanceSource():void{
  51. // in rare cases the reader will be unable to detect and throw,
  52. // but we don't want the performance test to yell about it
  53. // @see QRCodeReaderTestAbstract::testReadData()
  54. try{
  55. $this->result = (new Decoder($this->options))
  56. ->decode(GDLuminanceSource::fromBlob($this->imageBlob, $this->options));
  57. }
  58. catch(QRCodeException){
  59. // noop
  60. }
  61. }
  62. /**
  63. * Tests the performance of the ImageMagick reader
  64. */
  65. #[Subject]
  66. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions', 'initMatrix', 'generateImageBlob'])]
  67. #[AfterMethods(['checkReaderResult'])]
  68. public function IMagickLuminanceSource():void{
  69. $this->options->readerUseImagickIfAvailable = true;
  70. try{
  71. $this->result = (new Decoder($this->options))
  72. ->decode(IMagickLuminanceSource::fromBlob($this->imageBlob, $this->options));
  73. }
  74. catch(QRCodeException){
  75. // noop
  76. }
  77. }
  78. }