forked from msgpack/msgpack-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue138.php
More file actions
71 lines (61 loc) · 2.42 KB
/
issue138.php
File metadata and controls
71 lines (61 loc) · 2.42 KB
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
61
62
63
64
65
66
67
68
69
70
<?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);
}
}