QRDataBenchmark.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /** @phan-suppress-next-line PhanNoopNew */
  42. new QRData($this->options, [new $this->modeFQCN($this->testData)]);
  43. }
  44. /**
  45. * Tests the performance of QRData::writeMatrix(), includes QRMatrix::writeCodewords() and the ReedSolomonEncoder
  46. */
  47. #[Subject]
  48. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions', 'initQRData'])]
  49. public function writeMatrix():void{
  50. $this->qrData->writeMatrix();
  51. }
  52. /**
  53. * Tests the performance of QRDataModeInterface::decodeSegment()
  54. *
  55. * we need to clone the BitBuffer instance here because its internal state is modified during decoding
  56. */
  57. #[Subject]
  58. #[BeforeMethods(['assignParams', 'generateTestData', 'initOptions', 'initQRData', 'initBitBuffer'])]
  59. public function decodeSegment():void{
  60. /** @noinspection PhpUndefinedMethodInspection */
  61. $this->modeFQCN::decodeSegment(clone $this->bitBuffer, $this->version->getVersionNumber());
  62. }
  63. }