|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests the QRMatrix write performance |
| 4 | + * |
| 5 | + * @created 16.10.2023 |
| 6 | + * @author smiley <[email protected]> |
| 7 | + * @copyright 2023 smiley |
| 8 | + * @license MIT |
| 9 | + */ |
| 10 | + |
| 11 | +namespace chillerlan\QRCodeTest\Performance; |
| 12 | + |
| 13 | +use chillerlan\QRCode\Common\{EccLevel, Mode, Version}; |
| 14 | +use chillerlan\QRCode\Data\QRData; |
| 15 | +use chillerlan\QRCode\QROptions; |
| 16 | +use chillerlan\QRCodeTest\QRMaxLengthTrait; |
| 17 | +use Generator; |
| 18 | +use function file_put_contents; |
| 19 | +use function json_encode; |
| 20 | +use function printf; |
| 21 | +use function sprintf; |
| 22 | +use function str_repeat; |
| 23 | +use function str_replace; |
| 24 | +use const JSON_PRETTY_PRINT; |
| 25 | + |
| 26 | +require_once __DIR__.'/../../vendor/autoload.php'; |
| 27 | + |
| 28 | +// excerpt from QRCodeReaderTestAbstract |
| 29 | +$generator = new class () { |
| 30 | + use QRMaxLengthTrait; |
| 31 | + |
| 32 | + public function dataProvider():Generator{ |
| 33 | + |
| 34 | + $dataModeData = [ |
| 35 | + Mode::NUMBER => str_repeat('0123456789', 750), |
| 36 | + Mode::ALPHANUM => str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 $%*+-./:', 100), |
| 37 | + Mode::KANJI => str_repeat('漂う花の香り', 350), |
| 38 | + Mode::HANZI => str_repeat('无可奈何燃花作香', 250), |
| 39 | + Mode::BYTE => str_repeat('https://www.youtube.com/watch?v=dQw4w9WgXcQ ', 100), |
| 40 | + ]; |
| 41 | + |
| 42 | + foreach(Mode::INTERFACES as $dataMode => $dataModeInterface){ |
| 43 | + $dataModeName = str_replace('chillerlan\\QRCode\\Data\\', '', $dataModeInterface); |
| 44 | + |
| 45 | + for($v = 1; $v <= 40; $v++){ |
| 46 | + $version = new Version($v); |
| 47 | + |
| 48 | + foreach([EccLevel::L, EccLevel::M, EccLevel::Q, EccLevel::H] as $ecc){ |
| 49 | + $eccLevel = new EccLevel($ecc); |
| 50 | + |
| 51 | + yield sprintf('version %2s%s (%s)', $version, $eccLevel, $dataModeName) => [ |
| 52 | + $version->getVersionNumber(), |
| 53 | + $eccLevel, |
| 54 | + $dataModeInterface, |
| 55 | + $dataModeName, |
| 56 | + mb_substr($dataModeData[$dataMode], 0, self::getMaxLengthForMode($dataMode, $version, $eccLevel)), |
| 57 | + ]; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +}; |
| 65 | + |
| 66 | +$test = new PerformanceTest(100); |
| 67 | +$json = []; |
| 68 | + |
| 69 | +foreach($generator->dataProvider() as $key => [$version, $eccLevel, $dataModeInterface, $dataModeName, $data]){ |
| 70 | + // invovcation tests the performance of QRData::writeBitBuffer() |
| 71 | + $test->run(fn() => new QRData(new QROptions(['version' => $version, 'eccLevel' => $eccLevel->getLevel()]), [new ($dataModeInterface)($data)])); |
| 72 | + |
| 73 | + printf('%s encode: % 6.3fms', $key, $test->getResult()); |
| 74 | + $json[$dataModeName][(string)$eccLevel]['encode'][$version] = $test->getResult(); |
| 75 | + |
| 76 | + // writeMatrix includes QRMatrix::writeCodewords() and the ReedSolomonEncoder |
| 77 | + $qrdata = new QRData(new QROptions(['version' => $version, 'eccLevel' => $eccLevel->getLevel()]), [new ($dataModeInterface)($data)]); |
| 78 | + $test->run(fn() => $qrdata->writeMatrix()); |
| 79 | + |
| 80 | + printf(', write matrix: % 6.3fms', $test->getResult()); |
| 81 | + $json[$dataModeName][(string)$eccLevel]['write'][$version] = $test->getResult(); |
| 82 | + |
| 83 | + $bitBuffer = $qrdata->getBitBuffer(); |
| 84 | + $bitBuffer->read(4); // read data mode indicator |
| 85 | + |
| 86 | + $test->run(fn() => $dataModeInterface::decodeSegment(clone $bitBuffer, $version)); |
| 87 | + |
| 88 | + printf(", decode: % 6.3fms\n", $test->getResult()); |
| 89 | + $json[$dataModeName][(string)$eccLevel]['decode'][$version] = $test->getResult(); |
| 90 | +} |
| 91 | + |
| 92 | +file_put_contents(__DIR__.'/performance_qrdata.json', json_encode($json, JSON_PRETTY_PRINT)); |
0 commit comments