Last active
October 16, 2021 03:19
-
-
Save spajak/d07a999deb0430e2b6b7e58fc44213d1 to your computer and use it in GitHub Desktop.
PHP serializers (native, json, igbinary, msgpack) performance and size comparison (for session like, small, arrays dataset)
This file contains 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 | |
define('ITERATIONS', 1000000); | |
# [igbinary serializer PHP extension](https://github.com/igbinary/igbinary) | |
if (!extension_loaded('igbinary')) { | |
throw new DomainException('Required PHP module "igbinary" is not loaded'); | |
} | |
# [MessagePack binary serializer PHP extension](https://github.com/msgpack/msgpack-php) | |
if (!extension_loaded('msgpack')) { | |
throw new DomainException('Required PHP module "msgpack" is not loaded'); | |
} | |
ini_set('igbinary.compact_strings', true); | |
function millitime() { | |
return (int) (microtime(true)*1000); | |
} | |
function timed(Closure $callable, $data) { | |
$s = millitime(); | |
for ($i = 0; $i < ITERATIONS; $i++) { $callable($data); } | |
return millitime() - $s; | |
} | |
function report(string $name, array $serializer, array $data, string $dataName) { | |
echo "------------------------------\n"; | |
echo sprintf("[%s] %s:\n", $dataName, $name); | |
echo "------------------------------\n"; | |
$value = $serializer[0]($data); | |
echo sprintf("Length: %s bytes\n", strlen($value)); | |
echo sprintf("Base64 length: %s bytes\n", strlen(base64_encode($value))); | |
$time1 = timed($serializer[0], $data); | |
echo sprintf("Time (serialize): %s ms\n", $time1); | |
$time2 = timed($serializer[1], $value); | |
echo sprintf("Time (unserialize): %s ms\n", $time2); | |
echo sprintf("Time (total): %s ms\n", $time1 + $time2); | |
if ($data !== $serializer[1]($value)) { | |
throw new RuntimeException( | |
sprintf('Unserialized data "%s" (by %s serializer) is not valid!', $dataName, $name) | |
); | |
} | |
} | |
$dataSet = json_decode(file_get_contents('x-dataset.json'), true, JSON_THROW_ON_ERROR); | |
$serializers = [ | |
"Native" => [ | |
function(array $data) { return serialize($data); }, | |
function(string $data) { return unserialize($data); } | |
], | |
"Json" => [ | |
function(array $data) { return json_encode($data); }, | |
function(string $data) { return json_decode($data, true); } | |
], | |
"Igbinary" => [ | |
function(array $data) { return igbinary_serialize($data); }, | |
function(string $data) { return igbinary_unserialize($data); } | |
], | |
"MessagePack" => [ | |
function(array $data) { return msgpack_pack($data); }, | |
function(string $data) { return msgpack_unpack($data); } | |
] | |
]; | |
foreach ($dataSet as $dataName => $data) { | |
foreach ($serializers as $name => $serializer) { | |
report($name, $serializer, $data, $dataName); | |
} | |
} |
This file contains 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
{ | |
"tiny": { | |
"foo": { | |
"user_id": 46337768, | |
"roles": ["user"] | |
} | |
}, | |
"small": { | |
"foo": { | |
"user_id": 8625806709, | |
"preferences": { | |
"likes_animals": true, | |
"favourite_color": "black", | |
"is_insane": false | |
}, | |
"roles": ["user", "batman"] | |
}, | |
"bar": { | |
"profession": "lifesaving", | |
"top_speed": 1239.3421 | |
} | |
}, | |
"medium": { | |
"foo": { | |
"user_id": 8625806709, | |
"preferences": { | |
"likes_animals": true, | |
"favourite_color": "black", | |
"counts": 7612 | |
}, | |
"profession": "lifesaving", | |
"top_speed": 1163.7821, | |
"token": "TL4cvaLajj7wFxqEMHLUDvGtAX9YZjcK.q3cejYbnj6ze", | |
"date": "Wed 17 Jul 2019 02:13:34 PM UTC", | |
"roles": ["user", "batman"], | |
"favourite_gods": "Apollo, Zeus, Chaos, Charon", | |
"ipv6": "2001:0db9:0000:0000:0000:8a2e:0370:7337" | |
}, | |
"bar": { | |
"days": ["Friday", "Thursday", "Sunday"], | |
"months": ["August", "September", "March", "April"], | |
"unicode": "ąęółżźćńśł", | |
"greek": "αβγεω" | |
} | |
}, | |
"big": { | |
"session": { | |
"user_id": 1844441354, | |
"full_name": "Edward Scissorhands", | |
"special_ability": "cutting", | |
"roles": ["user", "member", "star", "barber"], | |
"likes_children": true, | |
"is_bird": false, | |
"dislikes": ["liars", "lawyers"], | |
"has_parents": false, | |
"likes_birds": true, | |
"timestamp": 1563366166, | |
"token": "Z4fNqZeDML8v6CQvzVgrBNkavYMgeR5Q", | |
"weekdays": ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], | |
"months": "February, March, April, December, October, May, November", | |
"uuid": "af764211-32e8-439e-8810-5b706cc3c7dc", | |
"ipv6": "02a01:115f:0688:1610:0bce7:50da:dd70:905d" | |
}, | |
"other": { | |
"random": "CphdmDR78wDraQ46TFcPCadgWcukmPAz4JDZckWVsccsSR8Bpb2BW5uJ7MEjs6u6EWHtSfZahNcLvbXuXQFKwYjeBz9EvtmKfTPJLEJThygpzQunwYHDPCH22GwnKHWBcBbNRwjYTbYhRhfY7NtxfLq84K6D4S6S67wMtrpXsX696XXNB2buCbswL8KsQF9xpfatx9fNWWyDK7gcX4gb5jhtgTvWae6qkRGtQySpkfRVdJJMDQPjYWn6FGDGLkGW", | |
"fill_some_space": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce euismod convallis est sit amet tincidunt. Proin euismod auctor enim, ut accumsan odio aliquam nec. Cras pharetra turpis eget sollicitudin porta. Praesent et iaculis ipsum, sed mollis sapien. Donec condimentum, purus et volutpat interdum, enim nisi fringilla neque, nec eleifend dui lectus ac ante. Proin maximus finibus diam, eu porttitor mi ullamcorper sed. Cras ac pellentesque justo. Nullam eget mi mi. Morbi ac dui nec augue sollicitudin aliquam. Integer orci ipsum, vehicula eu nisl sit amet, efficitur tristique erat. Nullam eget cursus nulla, et auctor risus. Aliquam bibendum diam erat, et volutpat justo maximus ut. Aliquam eleifend ex sapien, sit amet tincidunt turpis tempor vitae." | |
}, | |
"deeper": { | |
"date": "Wed 17 Jul 2019 02:13:11 PM UTC", | |
"notes": "Pro verear perfecto at, sea an legere constituam. Aliquip forensibus ius ei, adhuc movet decore ut duo. Facer liber copiosae nec no. Errem persius has ea.", | |
"unicode": "ąęółżźćńśł-ĄĘÓŁŻŹĆŃŚŁ", | |
"greek": "αβγεω", | |
"numbers": [2342, 9821, 0, 100000, 99999, 66666, 12382] | |
} | |
}, | |
"repeatable": { | |
"foo": { | |
"session_id": "123e4567-e89b-12d3-a456-426655440000", | |
"interests": "Trains relativity; Special relativity; General relativity", | |
"ts": 1563366166, | |
"number": 876362, | |
"is_human": true, | |
"favourite_vegetables": ["meat", "spam", "spam", "spam", "chocolate"], | |
"date": "Wed 17 Jul 2019 02:14:51 PM UTC", | |
"ipv6": "2620:0000:0862:0ed1a:0000:0000::0000:0001", | |
"greek": "αβγεω" | |
}, | |
"more": [ | |
"value", "same-boring-value", "same-boring-value", "same-boring-value", "same-boring-value", "same-boring-value" | |
] | |
}, | |
"randoms": { | |
"integer": [808710896, 390766634, 723905356, 290738044, 852481758, 572710096], | |
"base64": [ | |
"2tt3JXnOrN+NqOQTlaV4n5r+yG29XRZXvvfvTCEW8IgtEmQkn0lfnRHhOAoeKEOGaFyrwR1M6oYPepVnTzSLYA==", | |
"AdVcfJzDE1HYMbsVNHnO3JYR+YBzzEVNiYRA5fS1IbIEONMh17O8DnyQHvYZ7m5qm3F34Cs4PQXvMufWpE73KQ==", | |
"Zkt1axzllX35nUKTwfL+qi4Q29RHrdMFfAzgtaMcTIN3m7WxBM4/23YgbpqbcDgyRb71FqzzB4KH/mFpobdgxQ==", | |
"rltI3Q29YoydB97sUe/77xzqv9c4kYREBAu28cbFdEtHWGaB4vxcZvfsC+79xaaRONddhdp1KUr+8zrpoGnWIw==", | |
"yKdcZoeGHubkZ+3Z3N8p+glUrIH8nicIvPjn4UGFMzEapmtHQa7RTXgnYHgmG/M6GyiJmnihV0OBGeSqv+psKw==", | |
"ecSrheqUfCUV5EU8aeUORUK8rSvoF6q2rQu6CJ3YREbuE2+s/xGzhDMOTXP7xdyR7xfJB+oXmFoOqDghyvn2dw==" | |
], | |
"hex": [ | |
"692553009ead3f627c41fa003d0dd90698d6a429430a01964730c22d31a80fa3", | |
"2910a547dcce783a7282dd1a20bde98408bebd2a374ec521603ed3fb8556d9f8", | |
"6d8a7bdac1340685aa037b82e536d218bf985933224e3191dcb2b68fbae34838", | |
"455fe3a53033f8122107a7b6e481ca237acfbe305b9c109c90ef499d04c0805a", | |
"699007480eae8f3e7ef3fcf887ba48295de2674b895c9c7e912b3dba3b9dfdd0", | |
"13e5c40f68015e0a09ce2889bfd634946a3f5b9ea9c14444fdd5dd01d1959e17" | |
] | |
} | |
} |
This file contains 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
------------------------------ | |
[tiny] Native: | |
------------------------------ | |
Length: 80 bytes | |
Base64 length: 108 bytes | |
Time (serialize): 520 ms | |
Time (unserialize): 1021 ms | |
Time (total): 1541 ms | |
------------------------------ | |
[tiny] Json: | |
------------------------------ | |
Length: 45 bytes | |
Base64 length: 60 bytes | |
Time (serialize): 423 ms | |
Time (unserialize): 1179 ms | |
Time (total): 1602 ms | |
------------------------------ | |
[tiny] Igbinary: | |
------------------------------ | |
Length: 44 bytes | |
Base64 length: 60 bytes | |
Time (serialize): 657 ms | |
Time (unserialize): 695 ms | |
Time (total): 1352 ms | |
------------------------------ | |
[tiny] MessagePack: | |
------------------------------ | |
Length: 31 bytes | |
Base64 length: 44 bytes | |
Time (serialize): 763 ms | |
Time (unserialize): 863 ms | |
Time (total): 1626 ms | |
------------------------------ | |
[small] Native: | |
------------------------------ | |
Length: 284 bytes | |
Base64 length: 380 bytes | |
Time (serialize): 2296 ms | |
Time (unserialize): 2022 ms | |
Time (total): 4318 ms | |
------------------------------ | |
[small] Json: | |
------------------------------ | |
Length: 193 bytes | |
Base64 length: 260 bytes | |
Time (serialize): 1985 ms | |
Time (unserialize): 2869 ms | |
Time (total): 4854 ms | |
------------------------------ | |
[small] Igbinary: | |
------------------------------ | |
Length: 176 bytes | |
Base64 length: 236 bytes | |
Time (serialize): 1136 ms | |
Time (unserialize): 1440 ms | |
Time (total): 2576 ms | |
------------------------------ | |
[small] MessagePack: | |
------------------------------ | |
Length: 149 bytes | |
Base64 length: 200 bytes | |
Time (serialize): 1212 ms | |
Time (unserialize): 1926 ms | |
Time (total): 3138 ms | |
------------------------------ | |
[medium] Native: | |
------------------------------ | |
Length: 744 bytes | |
Base64 length: 992 bytes | |
Time (serialize): 2978 ms | |
Time (unserialize): 3637 ms | |
Time (total): 6615 ms | |
------------------------------ | |
[medium] Json: | |
------------------------------ | |
Length: 582 bytes | |
Base64 length: 776 bytes | |
Time (serialize): 2954 ms | |
Time (unserialize): 6485 ms | |
Time (total): 9439 ms | |
------------------------------ | |
[medium] Igbinary: | |
------------------------------ | |
Length: 501 bytes | |
Base64 length: 668 bytes | |
Time (serialize): 2568 ms | |
Time (unserialize): 2792 ms | |
Time (total): 5360 ms | |
------------------------------ | |
[medium] MessagePack: | |
------------------------------ | |
Length: 439 bytes | |
Base64 length: 588 bytes | |
Time (serialize): 1851 ms | |
Time (unserialize): 3732 ms | |
Time (total): 5583 ms | |
------------------------------ | |
[big] Native: | |
------------------------------ | |
Length: 2301 bytes | |
Base64 length: 3068 bytes | |
Time (serialize): 3426 ms | |
Time (unserialize): 5685 ms | |
Time (total): 9111 ms | |
------------------------------ | |
[big] Json: | |
------------------------------ | |
Length: 2056 bytes | |
Base64 length: 2744 bytes | |
Time (serialize): 5062 ms | |
Time (unserialize): 12358 ms | |
Time (total): 17420 ms | |
------------------------------ | |
[big] Igbinary: | |
------------------------------ | |
Length: 1912 bytes | |
Base64 length: 2552 bytes | |
Time (serialize): 5972 ms | |
Time (unserialize): 4371 ms | |
Time (total): 10343 ms | |
------------------------------ | |
[big] MessagePack: | |
------------------------------ | |
Length: 1817 bytes | |
Base64 length: 2424 bytes | |
Time (serialize): 3195 ms | |
Time (unserialize): 5879 ms | |
Time (total): 9074 ms | |
------------------------------ | |
[repeatable] Native: | |
------------------------------ | |
Length: 660 bytes | |
Base64 length: 880 bytes | |
Time (serialize): 1567 ms | |
Time (unserialize): 2663 ms | |
Time (total): 4230 ms | |
------------------------------ | |
[repeatable] Json: | |
------------------------------ | |
Length: 496 bytes | |
Base64 length: 664 bytes | |
Time (serialize): 1571 ms | |
Time (unserialize): 4432 ms | |
Time (total): 6003 ms | |
------------------------------ | |
[repeatable] Igbinary: | |
------------------------------ | |
Length: 388 bytes | |
Base64 length: 520 bytes | |
Time (serialize): 2830 ms | |
Time (unserialize): 1861 ms | |
Time (total): 4691 ms | |
------------------------------ | |
[repeatable] MessagePack: | |
------------------------------ | |
Length: 410 bytes | |
Base64 length: 548 bytes | |
Time (serialize): 1361 ms | |
Time (unserialize): 2808 ms | |
Time (total): 4169 ms | |
------------------------------ | |
[randoms] Native: | |
------------------------------ | |
Length: 1213 bytes | |
Base64 length: 1620 bytes | |
Time (serialize): 1492 ms | |
Time (unserialize): 2351 ms | |
Time (total): 3843 ms | |
------------------------------ | |
[randoms] Json: | |
------------------------------ | |
Length: 1045 bytes | |
Base64 length: 1396 bytes | |
Time (serialize): 2525 ms | |
Time (unserialize): 5017 ms | |
Time (total): 7542 ms | |
------------------------------ | |
[randoms] Igbinary: | |
------------------------------ | |
Length: 1036 bytes | |
Base64 length: 1384 bytes | |
Time (serialize): 3834 ms | |
Time (unserialize): 1521 ms | |
Time (total): 5355 ms | |
------------------------------ | |
[randoms] MessagePack: | |
------------------------------ | |
Length: 989 bytes | |
Base64 length: 1320 bytes | |
Time (serialize): 1350 ms | |
Time (unserialize): 2030 ms | |
Time (total): 3380 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment