- remove spurious php timeout
[plcapi.git] / test / benchmark.php
1 <?php
2 /**
3  * Benchamrking suite for the PHP-XMLRPC lib
4  * @author Gaetano Giunta
5  * @version $Id$
6  * @copyright (c) 2005-2009 G. Giunta
7  * @license code licensed under the BSD License: http://phpxmlrpc.sourceforge.net/license.txt
8  *
9  * @todo add a test for response ok in call testing?
10  **/
11
12         include(getcwd().'/parse_args.php');
13
14         require_once('xmlrpc.inc');
15
16         // Set up PHP structures to be used in many tests
17         $data1 = array(1, 1.0, 'hello world', true, '20051021T23:43:00', -1, 11.0, '~!@#$%^&*()_+|', false, '20051021T23:43:00');
18         $data2 = array('zero' => $data1, 'one' => $data1, 'two' => $data1, 'three' => $data1, 'four' => $data1, 'five' => $data1, 'six' => $data1, 'seven' => $data1, 'eight' => $data1, 'nine' => $data1);
19         $data = array($data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2, $data2);
20         $keys = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
21
22         $test_results=array();
23         $xd = extension_loaded('xdebug') && ini_get('xdebug.profiler_enable');
24         if ($xd)
25                 $num_tests = 1;
26         else
27                 $num_tests = 10;
28
29         $title = 'XML-RPC Benchmark Tests';
30
31         if(isset($_SERVER['REQUEST_METHOD']))
32         {
33                 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n<head>\n<title>$title</title>\n</head>\n<body>\n<h1>$title</h1>\n<pre>\n";
34         }
35         else
36         {
37                 echo "$title\n\n";
38         }
39
40         if(isset($_SERVER['REQUEST_METHOD']))
41         {
42                 echo "<h3>Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."</h3>\n";
43                 if ($xd) echo "<h4>XDEBUG profiling enabled: skipping remote tests. Trace file is: ".htmlspecialchars(xdebug_get_profiler_filename())."</h4>\n";
44                 flush();
45                 ob_flush();
46         }
47         else
48         {
49                 echo "Using lib version: $xmlrpcVersion on PHP version: ".phpversion()."\n";
50                 if ($xd) echo "XDEBUG profiling enabled: skipping remote tests\nTrace file is: ".xdebug_get_profiler_filename()."\n";
51         }
52
53         // test 'old style' data encoding vs. 'automatic style' encoding
54         begin_test('Data encoding (large array)', 'manual encoding');
55         for ($i = 0; $i < $num_tests; $i++)
56         {
57                 $vals = array();
58                 for ($j = 0; $j < 10; $j++)
59                 {
60                         $valarray = array();
61                         foreach ($data[$j] as $key => $val)
62                         {
63                                 $values = array();
64                                 $values[] = new xmlrpcval($val[0], 'int');
65                                 $values[] = new xmlrpcval($val[1], 'double');
66                                 $values[] = new xmlrpcval($val[2], 'string');
67                                 $values[] = new xmlrpcval($val[3], 'boolean');
68                                 $values[] = new xmlrpcval($val[4], 'dateTime.iso8601');
69                                 $values[] = new xmlrpcval($val[5], 'int');
70                                 $values[] = new xmlrpcval($val[6], 'double');
71                                 $values[] = new xmlrpcval($val[7], 'string');
72                                 $values[] = new xmlrpcval($val[8], 'boolean');
73                                 $values[] = new xmlrpcval($val[9], 'dateTime.iso8601');
74                                 $valarray[$key] = new xmlrpcval($values, 'array');
75                         }
76                         $vals[] = new xmlrpcval($valarray, 'struct');
77                 }
78                 $value = new xmlrpcval($vals, 'array');
79                 $out = $value->serialize();
80         }
81         end_test('Data encoding (large array)', 'manual encoding', $out);
82
83         begin_test('Data encoding (large array)', 'automatic encoding');
84         for ($i = 0; $i < $num_tests; $i++)
85         {
86                 $value =& php_xmlrpc_encode($data, array('auto_dates'));
87                 $out = $value->serialize();
88         }
89         end_test('Data encoding (large array)', 'automatic encoding', $out);
90
91         if (function_exists('xmlrpc_set_type'))
92         {
93         begin_test('Data encoding (large array)', 'xmlrpc-epi encoding');
94         for ($i = 0; $i < $num_tests; $i++)
95         {
96                 for ($j = 0; $j < 10; $j++)
97                         foreach ($keys as $k)
98                         {
99                                 xmlrpc_set_type($data[$j][$k][4], 'datetime');
100                                 xmlrpc_set_type($data[$j][$k][8], 'datetime');
101                         }
102                 $out = xmlrpc_encode($data);
103         }
104         end_test('Data encoding (large array)', 'xmlrpc-epi encoding', $out);
105         }
106
107         // test 'old style' data decoding vs. 'automatic style' decoding
108         $dummy = new xmlrpcmsg('');
109         $out = new xmlrpcresp($value);
110         $in = '<?xml version="1.0" ?>'."\n".$out->serialize();
111
112         begin_test('Data decoding (large array)', 'manual decoding');
113         for ($i = 0; $i < $num_tests; $i++)
114         {
115                 $response =& $dummy->ParseResponse($in, true);
116                 $value = $response->value();
117                 $result = array();
118                 for ($k = 0; $k < $value->arraysize(); $k++)
119                 {
120                         $val1 = $value->arraymem($k);
121                         $out = array();
122                         while (list($name, $val) = $val1->structeach())
123                         {
124                                 $out[$name] = array();
125                                 for ($j = 0; $j < $val->arraysize(); $j++)
126                                 {
127                                         $data = $val->arraymem($j);
128                                         $out[$name][] = $data->scalarval();
129                                 }
130                         } // while
131                         $result[] = $out;
132                 }
133         }
134         end_test('Data decoding (large array)', 'manual decoding', $result);
135
136         begin_test('Data decoding (large array)', 'automatic decoding');
137         for ($i = 0; $i < $num_tests; $i++)
138         {
139                 $response =& $dummy->ParseResponse($in, true, 'phpvals');
140                 $value = $response->value();
141         }
142         end_test('Data decoding (large array)', 'automatic decoding', $value);
143
144         if (function_exists('xmlrpc_decode'))
145         {
146         begin_test('Data decoding (large array)', 'xmlrpc-epi decoding');
147         for ($i = 0; $i < $num_tests; $i++)
148         {
149                 $response =& $dummy->ParseResponse($in, true, 'xml');
150                 $value = xmlrpc_decode($response->value());
151         }
152         end_test('Data decoding (large array)', 'xmlrpc-epi decoding', $value);
153         }
154
155         if (!$xd) {
156
157         /// test multicall vs. many calls vs. keep-alives
158         $value = php_xmlrpc_encode($data1, array('auto_dates'));
159         $msg = new xmlrpcmsg('interopEchoTests.echoValue', array($value));
160         $msgs=array();
161         for ($i = 0; $i < 25; $i++)
162                 $msgs[] = $msg;
163         $server = explode(':', $LOCALSERVER);
164         if(count($server) > 1)
165         {
166                 $c = new xmlrpc_client($URI, $server[0], $server[1]);
167         }
168         else
169         {
170                 $c = new xmlrpc_client($URI, $LOCALSERVER);
171         }
172         // do not interfere with http compression
173         $c->accepted_compression = array();
174         //$c->debug=true;
175
176         if (function_exists('gzinflate')) {
177                 $c->accepted_compression = null;
178         }
179         begin_test('Repeated send (small array)', 'http 10');
180         $response = array();
181         for ($i = 0; $i < 25; $i++)
182         {
183                 $resp =& $c->send($msg);
184                 $response[] = $resp->value();
185         }
186         end_test('Repeated send (small array)', 'http 10', $response);
187
188         if (function_exists('curl_init'))
189         {
190                 begin_test('Repeated send (small array)', 'http 11 w. keep-alive');
191                 $response = array();
192                 for ($i = 0; $i < 25; $i++)
193                 {
194                         $resp =& $c->send($msg, 10, 'http11');
195                         $response[] = $resp->value();
196                 }
197                 end_test('Repeated send (small array)', 'http 11 w. keep-alive', $response);
198
199                 $c->keepalive = false;
200                 begin_test('Repeated send (small array)', 'http 11');
201                 $response = array();
202                 for ($i = 0; $i < 25; $i++)
203                 {
204                         $resp =& $c->send($msg, 10, 'http11');
205                         $response[] = $resp->value();
206                 }
207                 end_test('Repeated send (small array)', 'http 11', $response);
208         }
209
210         begin_test('Repeated send (small array)', 'multicall');
211         $response =& $c->send($msgs);
212         foreach ($response as $key =>& $val)
213         {
214             $val = $val->value();
215         }
216         end_test('Repeated send (small array)', 'multicall', $response);
217
218         if (function_exists('gzinflate'))
219         {
220                 $c->accepted_compression = array('gzip');
221                 $c->request_compression = 'gzip';
222
223                 begin_test('Repeated send (small array)', 'http 10 w. compression');
224                 $response = array();
225                 for ($i = 0; $i < 25; $i++)
226                 {
227                         $resp =& $c->send($msg);
228                         $response[] = $resp->value();
229                 }
230                 end_test('Repeated send (small array)', 'http 10 w. compression', $response);
231
232         if (function_exists('curl_init'))
233         {
234             begin_test('Repeated send (small array)', 'http 11 w. keep-alive and compression');
235             $response = array();
236             for ($i = 0; $i < 25; $i++)
237             {
238                 $resp =& $c->send($msg, 10, 'http11');
239                 $response[] = $resp->value();
240             }
241             end_test('Repeated send (small array)', 'http 11 w. keep-alive and compression', $response);
242
243             $c->keepalive = false;
244             begin_test('Repeated send (small array)', 'http 11 w. compression');
245             $response = array();
246             for ($i = 0; $i < 25; $i++)
247             {
248                 $resp =& $c->send($msg, 10, 'http11');
249                 $response[] = $resp->value();
250             }
251             end_test('Repeated send (small array)', 'http 11 w. compression', $response);
252         }
253
254         begin_test('Repeated send (small array)', 'multicall w. compression');
255         $response =& $c->send($msgs);
256         foreach ($response as $key =>& $val)
257         {
258             $val = $val->value();
259         }
260         end_test('Repeated send (small array)', 'multicall w. compression', $response);
261         }
262
263         } // end of 'if no xdebug profiling'
264
265         function begin_test($test_name, $test_case)
266         {
267                 global $test_results;
268                 if (!isset($test_results[$test_name]))
269                         $test_results[$test_name]=array();
270                 $test_results[$test_name][$test_case] = array();
271                 $test_results[$test_name][$test_case]['time'] = microtime(true);
272         }
273
274         function end_test($test_name, $test_case, $test_result)
275         {
276                 global $test_results;
277                 $end = microtime(true);
278                 if (!isset($test_results[$test_name][$test_case]))
279                         trigger_error('ending test that was not sterted');
280                 $test_results[$test_name][$test_case]['time'] = $end - $test_results[$test_name][$test_case]['time'];
281                 $test_results[$test_name][$test_case]['result'] = $test_result;
282                 echo '.';
283                 flush();
284                 ob_flush();
285         }
286
287
288         echo "\n";
289         foreach($test_results as $test => $results)
290         {
291                 echo "\nTEST: $test\n";
292                 foreach ($results as $case => $data)
293                         echo "  $case: {$data['time']} secs - Output data CRC: ".crc32(serialize($data['result']))."\n";
294         }
295
296
297         if(isset($_SERVER['REQUEST_METHOD']))
298         {
299                 echo "\n</pre>\n</body>\n</html>\n";
300         }
301 ?>