-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-vs-serialize.php
More file actions
141 lines (112 loc) · 4.54 KB
/
json-vs-serialize.php
File metadata and controls
141 lines (112 loc) · 4.54 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
BASED ON A SCRIPT FROM http://www.shozab.com/php-serialization-vs-json-encoding-for-an-array/
Removed HTML, added cli colouring and 150k suite
running with php 5.6 docker
$ docker run -it --rm --name=json-vs-serialize -v "/home/kuba/www/tests":/usr/src/myapp -w /usr/src/myapp php:5.6.30-alpine php json-vs-serialize.php
running with php7 installed
$ php json-vs-serialize.php
add a tmp directory inside or change the location
*/
define('GREEN', "\033[0;32m");
define('WHITE', "\033[0m");
define('BLUE', "\033[0;34m");
define('RED', "\033[0;31m");
ini_set('max_execution_time', 0);
ini_set('memory_limit', '2G');
function start_timer()
{
global $time_start;
return $time_start = microtime(true);
}
function end_timer()
{
global $time_start;
$totaltime = microtime(true) - $time_start;
echo 'Process completed in ' . $totaltime*1000 . ' ms'."\n";
return $totaltime;
}
function get_random_string($valid_chars, $length)
{
$random_string = "";
$num_valid_chars = strlen($valid_chars);
for ($i = 0; $i < $length; $i++){
$random_pick = mt_rand(1, $num_valid_chars);
$random_char = $valid_chars[$random_pick-1];
$random_string .= $random_char;
}
return $random_string;
}
function save_csv($data)
{
$csvstring = implode(array_keys($data[0]),',')."\n";;
foreach($data as $v)
{
$csvstring .= implode($v,',')."\n";
}
file_put_contents('test_'.time().'.csv',$csvstring);
}
function runtest($datasize)
{
$stats_row = array();
echo BLUE."Making Test Data of size $datasize".WHITE."\n";
$array = array();
for($i=0; $i<$datasize; $i++)
{
$array[] = array('id'=>$i,
'text'=>get_random_string('abcdefghi',16)
);
}
$stats_row['datasize'] = $datasize;
start_timer();
echo GREEN.' Encoding in Json'.WHITE.": ";
$jsonencodeddata = json_encode($array);
$stats_row['encode_json'] = end_timer();
$f = 'tmp/'.$datasize.'_json.dat';
file_put_contents($f,$jsonencodeddata);
$stats_row['json_size(MB)'] = filesize($f)/1048576;
start_timer();
echo GREEN.' Decoding from Json'.WHITE.": ";
$jsondecodeddata = json_decode($jsonencodeddata);
$stats_row['decode_json'] = end_timer();
start_timer();
echo GREEN.' Serialization of data'.WHITE.": ";
$serializeddata = serialize($array);
$stats_row['serialize'] = end_timer();
$f = 'tmp/'.$datasize.'_serialize.dat';
file_put_contents($f,$serializeddata);
$stats_row['serialize_size(MB)'] = filesize($f)/1048576;
start_timer();
echo GREEN.' Unserialization of data'.WHITE.": ";
$unserializeddata = unserialize($serializeddata);
$stats_row['unserialize'] = end_timer();
start_timer();
echo GREEN.' Packing with msgpack'.WHITE.": ";
$msgpackeddata = msgpack_pack($array);
$stats_row['msgpack_pack'] = end_timer();
$f = 'tmp/'.$datasize.'_msgpack.dat';
file_put_contents($f,$msgpackeddata);
$stats_row['msgpack_size(MB)'] = filesize($f)/1048576;
start_timer();
echo GREEN.' Unpacking from msgpack'.WHITE.": ";
$msgunpackeddata = msgpack_unpack($msgpackeddata);
$stats_row['msgpack_unpack'] = end_timer();
return $stats_row;
}
$stats = array();
$files = glob('tmp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
for($i=1000; $i<50000; $i+=1000)
{
$row = runtest($i);
echo "\n".RED."json encoded: ".WHITE.round($row['json_size(MB)'],2)." ".RED."serialized: ".WHITE.round($row['serialize_size(MB)'], 2)." ".RED."ratio: ".WHITE.round(($row['serialize_size(MB)'] / $row['json_size(MB)'])*100, 2)."%";
echo "\n".RED."json encoded: ".WHITE.round($row['json_size(MB)'],2)." ".RED."packed: ".WHITE.round($row['msgpack_size(MB)'], 2)." ".RED."ratio: ".WHITE.round(($row['msgpack_size(MB)'] / $row['json_size(MB)'])*100, 2)."%";
echo "\n".GREEN."----------------------------------------".WHITE."\n";
}
$row = runtest(150000);
echo "\n".RED."json encoded: ".WHITE.round($row['json_size(MB)'],2)." ".RED."serialized: ".WHITE.round($row['serialize_size(MB)'],2)." ".RED."ratio: ".WHITE.round(($row['serialize_size(MB)'] / $row['json_size(MB)'])*100, 2)."%";
echo "\n".RED."json encoded: ".WHITE.round($row['json_size(MB)'],2)." ".RED."packed: ".WHITE.round($row['msgpack_size(MB)'], 2)." ".RED."ratio: ".WHITE.round(($row['msgpack_size(MB)'] / $row['json_size(MB)'])*100, 2)."%";
echo "\n".GREEN."----------------------------------------".WHITE."\n";