QRDataBenchmark.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * Class QRDataBenchmark
  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\Common\BitBuffer;
  13. use chillerlan\QRCode\Data\QRData;
  14. use PhpBench\Attributes\{BeforeMethods, Subject};
  15. /**
  16. * Tests the QRMatrix write performance
  17. */
  18. final class QRDataBenchmark extends BenchmarkAbstract{
  19. private QRData $qrData;
  20. private BitBuffer $bitBuffer;
  21. public function initOptions():void{
  22. $options = [
  23. 'version' => $this->version->getVersionNumber(),
  24. 'eccLevel' => $this->eccLevel->getLevel(),
  25. ];
  26. $this->initQROptions($options);
  27. }
  28. public function initQRData():void{
  29. $this->qrData = new QRData($this->options, [new $this->modeFQCN($this->testData)]);
  30. }
  31. public function initBitBuffer():void{
  32. $this->bitBuffer = $this->qrData->getBitBuffer();
  33. $this->bitBuffer->read(4); // read data mode indicator
  34. }
  35. /**
  36. * Tests the performance of QRData invovcation, includes QRData::writeBitBuffer()
  37. */
  38. #[Subject]
  39. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions'])]
  40. public function invocation():void{
  41. new QRData($this->options, [new $this->modeFQCN($this->testData)]);
  42. }
  43. /**
  44. * Tests the performance of QRData::writeMatrix(), includes QRMatrix::writeCodewords() and the ReedSolomonEncoder
  45. */
  46. #[Subject]
  47. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions', 'initQRData'])]
  48. public function writeMatrix():void{
  49. $this->qrData->writeMatrix();
  50. }
  51. /**
  52. * Tests the performance of QRDataModeInterface::decodeSegment()
  53. *
  54. * we need to clone the BitBuffer instance here because its internal state is modified during decoding
  55. */
  56. #[Subject]
  57. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions', 'initQRData', 'initBitBuffer'])]
  58. public function decodeSegment():void{
  59. /** @noinspection PhpUndefinedMethodInspection */
  60. $this->modeFQCN::decodeSegment(clone $this->bitBuffer, $this->version->getVersionNumber());
  61. }
  62. }