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