Last active
March 30, 2020 09:01
-
-
Save rybakit/8f1cf53e4a673443d84e to your computer and use it in GitHub Desktop.
pure msgpack.php vs json extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\MessagePack\PackedMap; | |
use App\MessagePack\PackedMapTransformer; | |
use MessagePack\BufferUnpacker; | |
use MessagePack\Packer; | |
use MessagePack\PackOptions; | |
require __DIR__.'/vendor/autoload.php'; | |
const ITERATIONS = 1000; | |
$packer = new Packer(PackOptions::FORCE_STR); | |
$unpacker = new BufferUnpacker(); | |
$item = [ | |
'str' => 'bar', | |
'arr' => array_fill(0, 100, 42), | |
'map' => ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', 'e' => 'e'], | |
'bool' => false, | |
'int' => 65535, | |
'float' => 65535.42, | |
]; | |
$data = []; | |
for ($i = 100; $i; --$i) { | |
$data[] = $item; | |
} | |
$time = microtime(true); | |
for ($i = ITERATIONS; $i; --$i) { | |
$packed = \msgpack_pack($data); | |
$raw = \msgpack_unpack($packed); | |
} | |
printf("pecl msgpack: %.4f sec, %d bytes\n", microtime(true) - $time, strlen($packed)); | |
$time = microtime(true); | |
for ($i = ITERATIONS; $i; --$i) { | |
$packed = \json_encode($data); | |
$raw = \json_decode($packed, true); | |
} | |
printf("pecl json: %.4f sec, %d bytes\n", microtime(true) - $time, strlen($packed)); | |
$time = microtime(true); | |
for ($i = ITERATIONS; $i; --$i) { | |
$packed = $packer->pack($data); | |
$raw = $unpacker->reset($packed)->unpack(); | |
} | |
printf("pure msgpack: %.4f sec, %d bytes\n", microtime(true) - $time, strlen($packed)); | |
require __DIR__.'/examples/MessagePack/PackedMap.php'; | |
require __DIR__.'/examples/MessagePack/PackedMapTransformer.php'; | |
$transformer = new PackedMapTransformer(3); | |
$schema = [ | |
'str' => 'str', | |
'arr' => 'array', | |
'map' => 'map', | |
'bool' => 'bool', | |
'int' => 'int', | |
'float' => 'float', | |
]; | |
$packer->registerTransformer($transformer); | |
$unpacker->registerTransformer($transformer); | |
$time = microtime(true); | |
for ($i = ITERATIONS; $i; --$i) { | |
$packed = $packer->pack(new PackedMap($data, $schema)); | |
$raw = $unpacker->reset($packed)->unpack(); | |
} | |
printf("pure msgpack packed: %.4f sec, %d bytes\n", microtime(true) - $time, strlen($packed)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ php -n -d extension=json.so -dextension=msgpack.so -dzend_extension=opcache.so -dopcache.enable_cli=1 bench_json_vs_msgpack.php | |
pecl msgpack: 0.4628 sec, 16903 bytes | |
pecl json: 1.0338 sec, 41201 bytes | |
pure msgpack: 2.6338 sec, 16903 bytes | |
pure msgpack packed: 2.4968 sec, 14164 bytes | |
$ php -v | |
PHP 7.2.7 (cli) (built: Jun 19 2018 14:40:10) ( NTS ) | |
Copyright (c) 1997-2018 The PHP Group | |
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies | |
with Zend OPcache v7.2.7, Copyright (c) 1999-2018, by Zend Technologies | |
$ php -r 'echo (new ReflectionExtension("json"))->getVersion(), "\n";' | |
1.6.0 | |
$ php -r 'echo (new ReflectionExtension("msgpack"))->getVersion(), "\n";' | |
2.0.2 | |
$ git rev-parse HEAD | |
b8a51fcbafe10d766f207bd2210e4c3a325ba337 | |
$ git describe --tags | |
v0.5.3-3-gb8a51fc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment