ref: 32551106495dffe0af58d34173a5c3f0c242f2c5
converter/server/vendor/rybakit/msgpack/examples/packed_map.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<?php /* * This file is part of the rybakit/msgpack.php package. * * (c) Eugene Leonovich <gen.work@gmail.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ use App\MessagePack\PackedMap; use App\MessagePack\PackedMapTransformer; use MessagePack\BufferUnpacker; use MessagePack\Packer; require __DIR__.'/autoload.php'; $schema = [ 'id' => 'int', 'first_name' => 'str', 'last_name' => 'str', ]; $profiles = []; for ($i = 0; $i < 1000; ++$i) { $profiles[] = [ 'id' => $i, 'first_name' => sprintf('first_name_%03s', $i), 'last_name' => sprintf('last_name_%03s', $i), ]; } $transformer = new PackedMapTransformer(3); $packer = new Packer(); $packer->registerTransformer($transformer); $unpacker = new BufferUnpacker(); $unpacker->registerTransformer($transformer); $packedMap = $packer->pack($profiles); $packedPackedMap = $packer->pack(new PackedMap($profiles, $schema)); $unpackedMap = $unpacker->reset($packedMap)->unpack(); $unpackedPackedMap = $unpacker->reset($packedPackedMap)->unpack(); if (($unpackedMap !== $profiles) || ($unpackedPackedMap !== $profiles)) { exit(1); } printf("Map size: %dB\n", strlen($packedMap)); printf("PackedMap size: %dB\n", strlen($packedPackedMap)); printf("Space savings: %.2F%%\n", 1 - strlen($packedPackedMap) / strlen($packedMap)); /* OUTPUT Map size: 56619B PackedMap size: 31660B Space savings: 0.44% */ |