-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpTest.php
176 lines (159 loc) · 5.44 KB
/
phpTest.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
// memcached -m 64 -s /tmp/m.sock -a 0777 -p 0 -u memcache
// memcached -m 64 -l 127.0.0.1 -p 11211 -u memcache
set_time_limit(3600);
error_reporting(E_ALL);
ini_set("display_errors", 1);
ini_set("apc.enable_cli", 1);
mysqli_report(MYSQLI_REPORT_STRICT | MYSQLI_REPORT_ERROR);
$data = array( // test are range from 10-128,000 bytes
"1111111110",
str_repeat("1111111110", 10),
str_repeat("1111111110", 30),
str_repeat("1111111110", 50),
str_repeat("1111111110", 100),
str_repeat("1111111110", 200),
str_repeat("1111111110", 400),
str_repeat("1111111110", 800),
str_repeat("1111111110", 1600),
str_repeat("1111111110", 3200),
str_repeat("1111111110", 6400),
str_repeat("1111111110", 12800),
array(0=>"1111111110", "id2"=>"hello world", "id3"=>"foo bar", "id4"=>42)
);
echo '<hr><b>'."shm\n".'</b>';
$t = array();
foreach ($data as $key=>$val) $t[] = shm(4000+$key, $val);
echo stats($t);
echo '<hr><b>'."apc\n".'</b>';
$t = array();
foreach ($data as $key=>$val) $t[] = apc((string)$key, $val);
echo stats($t);
echo '<hr><b>'."memcached\n".'</b>';
$t = array();
$m = new Memcached();
$m->addServer("127.0.0.1", 11211);
foreach ($data as $key=>$val) $t[] = memcached($m, (string)$key, $val);
echo stats($t);
//not in memcached 1.x
echo '<hr><b>'."memcached socket\n</b>";
$t = array();
$m = new Memcached();
$m->addServer("unix:///tmp/m.sock", 0);
foreach ($data as $key=>$val) $t[] = memcached($m, (string)$key, $val);
echo stats($t);
/**/
echo '<hr><b>'."mysql myisam\n".'</b>';
$t = array();
$m = new mysqli("127.0.0.1", "root", "root", "test");
mysqli_query($m, "drop table if exists test.cache");
mysqli_query($m, "create table test.cache (id int primary key, data mediumtext) engine=myisam");
foreach ($data as $key=>$val) $t[] = mysql_cache($m, $key, $val);
echo stats($t);
echo '<hr><b>'."mysql memory\n".'<b>';
$t = array();
mysqli_query($m, "drop table if exists test.cache");
mysqli_query($m, "create table test.cache (id int primary key, data varchar(65500)) engine=memory");
foreach ($data as $key=>$val) $t[] = mysql_cache($m, $key, $val);
echo stats($t);
echo '<hr><b>'."file cache\n".'<b>';
$t = array();
foreach ($data as $key=>$val) $t[] = file_cache((string)$key, $val);
echo stats($t);
echo '<hr><b>'."php file cache\n".'<b>';
$t = array();
foreach ($data as $key=>$val) $t[] = php_cache((string)$key, $val);
echo stats($t);
function stats($t) {
return "\nTotal: ".number_format(array_sum($t), 3).", ".
"Avg: ".number_format(array_sum($t) / count($t), 3)."\n\n";
}
function format($num) {
return number_format($num, 3);
}
function shm($id, $data) {
if (is_array($data)) {
$arr = true;
$data = serialize($data);
} else $arr = false;
$len = strlen($data);
$shm_id = shmop_open($id, "c", 0644, $len);
shmop_write($shm_id, $data, 0);
$start = microtime(true);
if ($arr) {
for ($i=0; $i<100000; $i++) $v = unserialize(shmop_read($shm_id, 0, $len));
} else {
for ($i=0; $i<100000; $i++) $v = shmop_read($shm_id, 0, $len);
}
echo format($end = microtime(true)-$start)." ";
shmop_close($shm_id);
assert(substr(is_array($v) ? $v[0] : $v, 0, 10)=="1111111110");
return $end;
}
function apc($id, $data) {
apc_store($id, $data);
$start = microtime(true);
for ($i=0; $i<100000; $i++) $v = apc_fetch($id);
echo format($end = microtime(true)-$start)." ";
assert(substr(is_array($v) ? $v[0] : $v, 0, 10)=="1111111110");
return $end;
}
function memcache($m, $id, $data) {
memcache_set($m, $id, $data);
$start = microtime(true);
for ($i=0; $i<100000; $i++) $v = memcache_get($m, $id);
echo format($end = microtime(true)-$start)." ";
assert(substr(is_array($v) ? $v[0] : $v, 0, 10)=="1111111110");
return $end;
}
function memcached($m, $id, $data) {
$m->set($id, $data);
$start = microtime(true);
for ($i=0; $i<100000; $i++) $v = $m->get($id);
echo format($end = microtime(true)-$start)." ";
assert(substr(is_array($v) ? $v[0] : $v, 0, 10)=="1111111110");
return $end;
}
/* you can uncomment with your server configuration
*/
function mysql_cache($m, $id, $data) {
$d = is_array($data) ? serialize($data) : $data;
mysqli_query($m, "insert into test.cache values (".$id.", '".$d."')");
$start = microtime(true);
if (is_array($data)) {
for ($i=0; $i<100000; $i++) {
$v = mysqli_query($m, "SELECT data FROM test.cache WHERE id=".$id)->fetch_row();
$v = unserialize($v[0]);
}
} else {
for ($i=0; $i<100000; $i++) {
$v = mysqli_query($m, "SELECT data FROM test.cache WHERE id=".$id)->fetch_row();
}
}
echo format($end = microtime(true)-$start)." ";
assert(substr($v[0], 0, 10)=="1111111110");
return $end;
}
function file_cache($id, $data) {
file_put_contents($id, is_array($data) ? serialize($data) : $data);
$start = microtime(true);
if (is_array($data)) {
for ($i=0; $i<100000; $i++) $v = unserialize(file_get_contents($id));
} else {
for ($i=0; $i<100000; $i++) $v = file_get_contents($id);
}
echo format($end = microtime(true)-$start)." ";
assert(substr(is_array($v) ? $v[0] : $v, 0, 10)=="1111111110");
return $end;
}
function php_cache($id, $data) {
$id .= ".php";
$data = is_array($data) ? var_export($data, 1) : "'".$data."'";
file_put_contents($id, "<?php\n\$v=".$data.";");
touch($id, time()-10); // needed for APC's file update protection
$start = microtime(true);
for ($i=0; $i<100000; $i++) include($id);
echo format($end = microtime(true)-$start)." ";
assert(substr(is_array($v) ? $v[0] : $v, 0, 10)=="1111111110");
return $end;
}