-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark_serialization.php
95 lines (87 loc) · 2.6 KB
/
benchmark_serialization.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
include "Collection.php";
ini_set('memory_limit', -1);
// nested
$input = array();
for ($i = 0; $i < 1000; $i++) {
$input["k-$i"] = [$i];
$input["k-$i"]['k1'] =['a','b','c'];
$input["k-$i"]['k1']['k2'] =['a','b',10,20,30,true];
}
// simple
/*$input = array();
for ($i = 0; $i < 1000; $i++) {
$input["k-$i"] = ["k-$i",20];
}
*/
$instances=1000;
// php
$t1 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$ser=serialize($input);
}
$t2 = microtime(true);
$t3 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$b=unserialize($ser);
}
$t4 = microtime(true);
$table['serialize'] = $t2 - $t1;
$tableunser['unserialize'] = $t4 - $t3;
$tablesize['serialize'] = strlen($ser);
$tablecomp['unserialize'] = ($b===$input)?'yes':'no';
// igbinary
$t1 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$ser=igbinary_serialize($input);
}
$t2 = microtime(true);
$t3 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$b=igbinary_unserialize($ser);
}
$t4 = microtime(true);
$table['igbinary_serialize'] = $t2 - $t1;
$tableunser['igbinary_unserialize'] = $t4 - $t3;
$tablesize['igbinary_serialize'] = strlen($ser);
$tablecomp['igbinary_unserialize'] = ($b===$input)?'yes':'no';
$t1 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$ser=json_encode($input);
}
$t2 = microtime(true);
$t3 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$b=json_decode($ser,true);
}
$t4 = microtime(true);
$table['json_encode'] = $t2 - $t1;
$tableunser['json_decode'] = $t4 - $t3;
$tablesize['json_encode'] = strlen($ser);
$tablecomp['json_decode'] = ($b===$input)?'yes':'no';
// msgpack
$packer = new \MessagePack(false);
$t1 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$ser=$packer->pack($input);
}
$t2 = microtime(true);
$t3 = microtime(true);
for ($i = 0; $i < $instances; $i++) {
$b=$packer->unpack($ser);
}
$t4 = microtime(true);
$table['packer->pack (msgpack)'] = $t2 - $t1;
$tableunser['packer->unpack (msgpack)'] = $t4 - $t3;
$tablesize['packer->pack (msgpack)'] = strlen($ser);
$tablecomp['packer->pack (msgpack)'] = ($b===$input)?'yes':'no';
echo "<h3>Serialize: (in seconds less is better)</h3>";
echo \mapache_commons\Collection::generateTable($table,true,true);
echo "<h3>De-serialize: (in seconds less is better)</h3>";
echo \mapache_commons\Collection::generateTable($tableunser,true,true);
echo "<h3>Size: (in bytes less is better)</h3>";
echo \mapache_commons\Collection::generateTable($tablesize,true,true);
echo "<h3>Input is equals to output: (no means error)</h3>";
echo \mapache_commons\Collection::generateTable($tablecomp,true);
echo "<br>";
echo "<br>";