-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcompressed_string.php
52 lines (42 loc) · 1.78 KB
/
compressed_string.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
<?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\Text;
use App\MessagePack\TextExtension;
use MessagePack\BufferUnpacker;
use MessagePack\Packer;
require __DIR__.'/autoload.php';
$extension = new TextExtension(3);
$packer = new Packer(null, [$extension]);
$unpacker = new BufferUnpacker('', null, [$extension]);
$longString = <<<STR
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
STR;
$packedString = $packer->pack($longString);
$packedCompressedString = $packer->pack(new Text($longString));
$unpackedString = $unpacker->reset($packedString)->unpack();
$unpackedCompressedString = $unpacker->reset($packedCompressedString)->unpack();
if (($unpackedString !== $longString) || ($unpackedCompressedString !== $longString)) {
exit(1);
}
printf("Packed string size: %dB\n", strlen($packedString));
printf("Packed text size: %dB\n", strlen($packedCompressedString));
printf("Space saved: %dB\n", strlen($packedString) - strlen($packedCompressedString));
printf("Percentage saved: %d%%\n", round(1 - strlen($packedCompressedString) / strlen($packedString), 2) * 100);
/* OUTPUT
Packed string size: 448B
Packed text size: 291B
Space saved: 157B
Percentage saved: 35%
*/