forked from phpredis/phpredis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.php
More file actions
141 lines (112 loc) · 3 KB
/
tests.php
File metadata and controls
141 lines (112 loc) · 3 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
if (!extension_loaded('igbinary')) {
dl('igbinary.' . PHP_SHLIB_SUFFIX);
}
function test_speed_print($testname, $php, $igbinary) {
sort($php);
sort($igbinary);
reset($php);
reset($igbinary);
$phptime = ($php[1] + $php[2] + $php[3]) / 3;
$igbinarytime = ($igbinary[1] + $igbinary[2] + $igbinary[3]) / 3;
$phpstr = sprintf('%.3f', $php[0]);
$igbinarystr = sprintf('%.3f', $igbinary[0]);
for ($i = 1; $i < 5; ++$i) {
$phpstr .= sprintf(' %.3f', $php[$i]);
$igbinarystr .= sprintf(' %.3f', $igbinary[$i]);
}
echo $testname, ":\n";
printf(" php : %6.3f [%s]\n", $phptime, $phpstr);
printf(" igbinary: %6.3f %6.02f%% [%s]\n", $igbinarytime, $igbinarytime * 100 / $phptime, $igbinarystr);
}
function test_speed_serialize_php($data, $loops) {
$start = microtime(true);
for ($i = 0; $i < $loops; ++$i) {
$tmp = serialize($data);
}
$end = microtime(true);
return $end - $start;
}
function test_speed_serialize_igbinary($data, $loops) {
$start = microtime(true);
for ($i = 0; $i < $loops; ++$i) {
$tmp = igbinary_serialize($data);
}
$end = microtime(true);
return $end - $start;
}
function test_speed_serialize($testname, $data, $loops) {
$php = array();
$igbinary = array();
for ($i = 0; $i < 5; ++$i) {
$php[] = test_speed_serialize_php($data, $loops);
$igbinary[] = test_speed_serialize_igbinary($data, $loops);
}
test_speed_print($testname, $php, $igbinary);
}
function test_speed_unserialize_php($data, $loops) {
$serdata = serialize($data);
$start = microtime(true);
for ($i = 0; $i < $loops; ++$i) {
$tmp = unserialize($serdata);
}
$end = microtime(true);
return $end - $start;
}
function test_speed_unserialize_igbinary($data, $loops) {
$serdata = igbinary_serialize($data);
$start = microtime(true);
for ($i = 0; $i < $loops; ++$i) {
$tmp = igbinary_unserialize($serdata);
}
$end = microtime(true);
return $end - $start;
}
function test_speed_unserialize($testname, $data, $loops) {
$php = array();
$igbinary = array();
for ($i = 0; $i < 5; ++$i) {
$php[] = test_speed_unserialize_php($data, $loops);
$igbinary[] = test_speed_unserialize_igbinary($data, $loops);
}
test_speed_print($testname, $php, $igbinary);
}
function test_space($testname, $data) {
$phplen = strlen(serialize($data));
$igbinarylen = strlen(igbinary_serialize($data));
printf(" php : %5u\n", $phplen);
printf(" igbinary: %5u (%.02f%%)\n", $igbinarylen, $igbinarylen * 100 / $phplen);
}
function usage() {
global $argv;
die('Usage: php '.$argv[0].' space|serialize|unserialize [data] [loops]'."\n");
}
if (count($argv) < 3) {
usage();
}
$t = $argv[1];
$i = $argv[2];
$loops = 100000;
if (count($argv) > 3) {
$loops = (int) $argv[3];
if ($loops < 100 || $loops > 1000000) {
$loops = 100000;
}
}
if ((@include($i . '.php')) != 1) {
usage();
}
switch ($t) {
case 'space':
test_space($i, $data);
break;
case 'serialize':
test_speed_serialize($i, $data, $loops);
break;
case 'unserialize':
test_speed_unserialize($i, $data, $loops);
break;
default:
usage();
}
?>