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