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