001: <?php
002:
003: header ('Content-Type: text/html; charset=utf-8');
004:
005: ?>
006:
007: <html>
008: <head>
009: <style type="text/css">
010: body
011: {
012: padding : 16px;
013: margin : 0px;
014: }
015:
016: table
017: {
018: margin : auto;
019: border-collapse : collapse;
020: border : 1px solid gray;
021: color : #FFFFFF;
022: font : normal normal normal 10px verdana, arial, helvetica, sans-serif;
023: }
024:
025: td
026: {
027: padding : 0.4em;
028: color : #363636;
029: text-align : center;
030: vertical-align : top;
031: }
032:
033: td:nth-child(1)
034: {
035: padding : 0.4em 0.8em;
036: background : #C2D3D4;
037: text-align : left;
038: }
039:
040: th
041: {
042: padding : 0.6em 1.2em;
043: background : #5C443A;
044: color : #FFFFFF;
045: text-align : center;
046: text-transform : uppercase;
047: vertical-align : top;
048: }
049:
050: tr
051: {
052: background : #D3E4E5;
053: border : 1px dotted gray;
054: }
055:
056: tr:nth-child(2n)
057: {
058: background : #FFFFFF;
059: }
060:
061: tr:hover
062: {
063: background : #99BCBF;
064: border : 1px solid #03476F;
065: color : #000000;
066: }
067: </style>
068: </head>
069: <body>
070: <table>
071: <tr>
072: <th>Test name</th>
073: <th>Loop #1</th>
074: <th>Loop #2</th>
075: <th>Loop #3</th>
076: <th>Average</th>
077: </tr>
078:
079: <?php
080:
081: function bench_perform ($name, $callback, $params = array ())
082: {
083: echo '
084: <tr>
085: <td>' . htmlentities ($name, ENT_COMPAT, 'utf-8') . '</td>';
086:
087: $failed = false;
088: $sum = 0;
089:
090: for ($i = 0; $i < 3; ++$i)
091: {
092: $time = microtime (true);
093:
094: if (call_user_func_array ($callback, $params))
095: {
096: $diff = microtime (true) - $time;
097: $sum += $diff;
098:
099: echo '
100: <td>' . round ($diff * 1000) . ' ms</td>';
101: }
102: else
103: {
104: $failed = true;
105:
106: echo '
107: <td>failed</td>';
108: }
109: }
110:
111: if ($failed)
112: echo '
113: <td>failed</td>
114: </tr>';
115: else
116: echo '
117: <td>' . round ($sum * 1000 / 3) . ' ms</td>
118: </tr>';
119: }
120:
121: function bench_skip ($name)
122: {
123: echo '
124: <tr>
125: <td>' . htmlentities ($name, ENT_COMPAT, 'utf-8') . '</td>
126: <td>skipped</td>
127: <td>skipped</td>
128: <td>skipped</td>
129: <td>skipped</td>
130: </tr>';
131: }
132:
133: function test_proc_loop ($loops)
134: {
135: for ($i = 0; $i < $loops; )
136: ++$i;
137:
138: return true;
139: }
140:
141: function test_disk_o ($loops, $size)
142: {
143: $buffer = str_repeat ('$', $size);
144:
145: for ($i = 0; $i < $loops; ++$i)
146: {
147: $f = fopen ('dummy', 'wb');
148:
149: fwrite ($f, $buffer);
150: fclose ($f);
151: }
152:
153: unlink ('dummy');
154:
155: return true;
156: }
157:
158: function test_disk_r ($loops, $size)
159: {
160: $f = fopen ('dummy', 'wb');
161:
162: fwrite ($f, str_repeat ('$', $size));
163: fclose ($f);
164:
165: for ($i = 0; $i < $loops; ++$i)
166: {
167: $f = fopen ('dummy', 'rb');
168:
169: fread ($f, $size);
170: fclose ($f);
171: }
172:
173: unlink ('dummy');
174:
175: return true;
176: }
177:
178: function test_disk_w ($loops, $size)
179: {
180: $buffer = str_repeat ('$', $size);
181:
182: for ($i = 0; $i < $loops; ++$i)
183: {
184: $f = fopen ('dummy', 'wb');
185:
186: fwrite ($f, $buffer);
187: fclose ($f);
188:
189: unlink ('dummy');
190: }
191:
192: return true;
193: }
194:
195: function test_mem_alloc ($loops, $size)
196: {
197: for ($i = 0; $i < $loops; ++$i)
198: {
199: $buffer = str_repeat ('$', $size);
200:
201: unset ($buffer);
202: }
203:
204: return true;
205: }
206:
207: function test_net_dl ($loops, $size)
208: {
209: for ($i = 0; $i < $loops; ++$i)
210: {
211: $ch = curl_init ('http://test-debit.free.fr/image.iso');
212:
213: if ($ch === false)
214: return false;
215:
216: curl_setopt ($ch, CURLOPT_RANGE, '0-' . ($size - 1));
217: curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
218:
219: if (curl_exec ($ch) === false)
220: return false;
221:
222: curl_close ($ch);
223: }
224:
225: return true;
226: }
227:
228: function test_net_ul ($loops, $size)
229: {
230: $buffer = str_repeat ('$', $size);
231:
232: for ($i = 0; $i < $loops; ++$i)
233: {
234: $ch = curl_init ('http://test-debit.free.fr');
235:
236: if ($ch === false)
237: return false;
238:
239: curl_setopt ($ch, CURLOPT_POST, 1);
240: curl_setopt ($ch, CURLOPT_POSTFIELDS, array ('buffer' => $buffer));
241: curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
242:
243: if (curl_exec ($ch) === false)
244: return false;
245:
246: curl_close ($ch);
247: }
248:
249: return true;
250: }
251:
252: bench_perform ('Memory : allocate and release 1 MB, 100 times', 'test_mem_alloc', array (100, 1024 * 1024));
253: bench_perform ('Memory : allocate and release 8 MB, 10 times', 'test_mem_alloc', array (10, 8 * 1024 * 1024));
254:
255: if (function_exists ('curl_init'))
256: {
257: bench_perform ('Network : download 100 B, 10 times', 'test_net_dl', array (10, 100));
258: bench_perform ('Network : download 10 KB, 5 times', 'test_net_dl', array (5, 10 * 1024));
259: bench_perform ('Network : upload 100 B, 10 times', 'test_net_ul', array (10, 100));
260: bench_perform ('Network : upload 10 KB, 5 times', 'test_net_ul', array (5, 10 * 1024));
261: }
262: else
263: bench_skip ('Network : cannot perform, cURL not available');
264:
265: bench_perform ('Processor : empty loop, 10^5 iterations', 'test_proc_loop', array (100000));
266: bench_perform ('Processor : empty loop, 10^6 iterations', 'test_proc_loop', array (1000000));
267:
268: bench_perform ('Storage : read 100 B from file, 20 times', 'test_disk_r', array (20, 100));
269: bench_perform ('Storage : read 10 KB from file, 10 times', 'test_disk_r', array (10, 10 * 1024));
270: bench_perform ('Storage : read 1 MB from file, 5 times', 'test_disk_r', array (5, 1024 * 1024));
271: bench_perform ('Storage : write 100 B without flush, 20 times', 'test_disk_w', array (20, 100));
272: bench_perform ('Storage : write 10 KB without flush, 10 times', 'test_disk_w', array (10, 10 * 1024));
273: bench_perform ('Storage : write 1 MB without flush, 5 times', 'test_disk_w', array (5, 1024 * 1024));
274: bench_perform ('Storage : write 100 B with flush, 20 times', 'test_disk_o', array (20, 100));
275: bench_perform ('Storage : write 10 KB with flush, 10 times', 'test_disk_o', array (10, 10 * 1024));
276: bench_perform ('Storage : write 1 MB with flush, 5 times', 'test_disk_o', array (5, 1024 * 1024));
277:
278: ?>
279:
280: </table>
281: </body>
282: </html>