Add icd9 and minor fixes to the following:
[openemr.git] / gacl / test_suite / stress_test.php
blob3707dea066bbefe95917ff451cf8f9b94ab36846
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <html>
3 <head>
4 <title>Stress Test</title>
5 </head>
6 <body>
7 <pre>
8 <?php
10 /*! var
11 test scale
13 This script will create:
14 $scale * 10 ACOs
15 $scale * 10 ARO groups
16 $scale * 1000 AROs
17 $scale * 10 AXO groups
18 $scale * 1000 AXOs
19 $scale * 10 ACLs
21 1 normal ~5 seconds
22 10 heavy ~1 minute
23 100 crazy ~1 hour
24 !*/
25 $scale = 10;
27 set_time_limit (6000);
29 /*! function
30 get time accurate to the nearest microsecond, used for script timing
31 !*/
32 function getmicrotime ()
34 list ($usec, $sec) = explode (' ', microtime ());
35 return (float)$usec + (float)$sec;
38 /*! function
39 a better array_rand, this one actually works on windows
40 !*/
41 function array_mt_rand ($array, $items)
43 $keys = array_keys ($array);
44 $max = count ($keys) - 1;
46 if ( $items == 1 )
48 return $keys[mt_rand (0, $max)];
51 $return = array ();
53 for ( $i = 1; $i <= $items; $i++ )
55 $return[] = $keys[mt_rand (0, $max)];
58 return $return;
61 // require gacl
62 require_once (dirname (__FILE__) . '/../admin/gacl_admin.inc.php');
65 * Let's get ready to RUMBLE!!!
68 echo '<b>Stress Test</b>' . "\n";
69 echo ' Scale: ' . $scale . "\n\n";
71 $overall_start = getmicrotime ();
73 mt_srand ((double)microtime () *10000);
75 $gacl_api->add_object_section ('System', 'system', 0, 0, 'ACO');
77 echo "<b>Create ACOs</b>\n";
78 flush ();
81 $start_time = getmicrotime ();
83 $start = 1;
84 $max = 10 * $scale;
85 for ( $i = $start; $i <= $max; $i++ )
87 if ( $gacl_api->add_object ('system', 'ACO: ' . $i, $i, 10, 0, 'ACO') == FALSE )
89 echo " Error creating ACO: $i.\n";
90 echo ' ' . $gacl_api->_debug_msg . "\n";
94 $elapsed = getmicrotime () - $start_time;
96 echo "Done\n";
97 echo ' Count: ' . $max . "\n";
98 echo ' Time: ' . $elapsed . " s\n";
99 echo ' Average: ' . $elapsed/$max . " s\n\n";
103 $gacl_api->add_object_section ('Users', 'users', 0, 0, 'ARO');
105 echo "<b>Create many ARO Groups.</b>\n";
106 flush ();
108 $start_time = getmicrotime ();
110 $query = 'SELECT id FROM '.$gacl_api->_db_table_prefix.'aro_groups';
111 $ids = $gacl_api->db->GetCol($query);
113 // print_r ($ids);
115 $start = 1;
116 $max = 10 * $scale;
118 // function add_group ($name, $parent_id=0, $group_type='ARO') {
119 for ( $i = $start; $i <= $max; $i++ )
121 // Find a random parent
122 if ( !empty ($ids) ) {
123 $parent_id = $ids[array_mt_rand ($ids, 1)];
124 } else {
125 $parent_id = 0;
128 $result = $gacl_api->add_group ('aro_group'.$i,'ARO Group: '. $i, $parent_id, 'ARO');
130 if ( $result == FALSE )
132 echo " Error creating ARO Group: $i.\n";
133 echo ' ' . $gacl_api->_debug_msg . "\n";
135 else
137 $ids[] = $result;
141 $elapsed = getmicrotime () - $start_time;
143 echo "Done\n";
144 echo ' Count: ' . $max . "\n";
145 echo ' Time: ' . $elapsed . " s\n";
146 echo ' Average: ' . $elapsed/$max . " s\n\n";
150 echo "<b>Create AROs & assign to ARO Groups</b>\n";
151 flush ();
153 $start_time = getmicrotime ();
155 $start = 1;
156 $max = 1000 * $scale;
158 $groups = array_keys ($gacl_api->format_groups ($gacl_api->sort_groups ('ARO'), 'ARRAY'));
159 $randmax = count ($groups) - 1;
161 for ( $i = $start; $i <= $max; $i++ )
163 if ( $gacl_api->add_object ('users', 'ARO: '. $i, $i, 10, 0, 'ARO') == FALSE )
165 echo " Error creating ARO: $i.<br />\n";
166 echo ' ' . $gacl_api->_debug_msg . "\n";
168 else
170 // Assign to random groups.
171 $rand_key = $groups[mt_rand (0, $randmax)];
172 $gacl_api->add_group_object ($rand_key, 'users', $i, 'ARO');
176 $elapsed = getmicrotime () - $start_time;
178 echo "Done\n";
179 echo ' Count: ' . $max . "\n";
180 echo ' Time: ' . $elapsed . " s\n";
181 echo ' Average: ' . $elapsed/$max . " s\n\n";
186 $gacl_api->add_object_section ('Users', 'users', 0, 0, 'AXO');
188 echo "<b>Create many AXO Groups.</b>\n";
189 flush ();
191 $start_time = getmicrotime ();
193 $query = 'SELECT id FROM '.$gacl_api->_db_table_prefix.'axo_groups';
194 $ids = $gacl_api->db->GetCol($query);
196 $start = 1;
197 $max = 10 * $scale;
199 // function add_group ($name, $parent_id=0, $group_type='ARO') {
200 for ( $i = $start; $i <= $max; $i++ )
202 // Find a random parent
203 if ( !empty ($ids) ) {
204 $parent_id = $ids[array_mt_rand ($ids, 1)];
205 } else {
206 $parent_id = 0;
209 $result = $gacl_api->add_group ('axo_group'.$i,'AXO Group: '. $i, $parent_id, 'AXO');
210 if ( $result == FALSE )
212 echo " Error creating AXO Group: $i.\n";
213 echo ' ' . $gacl_api->_debug_msg . "\n";
215 else
217 $ids[] = $result;
221 $elapsed = getmicrotime () - $start_time;
223 echo "Done\n";
224 echo ' Count: ' . $max . "\n";
225 echo ' Time: ' . $elapsed . " s\n";
226 echo ' Average: ' . $elapsed/$max . " s\n\n";
230 echo "<b>Create AXOs & assign to AXO Groups</b>\n";
231 flush ();
233 $start_time = getmicrotime ();
235 $start = 1;
236 $max = 1000 * $scale;
238 // $groups = array_keys ($gacl_api->format_groups ($gacl_api->sort_groups ('AXO'), 'ARRAY'));
239 $rand_max = count ($groups) - 1;
241 for ( $i = $start; $i <= $max; $i++ )
243 if ( $gacl_api->add_object ('users', 'AXO: ' . $i, $i, 10, 0, 'AXO') == FALSE )
245 echo " Error creating ARO: $i.<br />\n";
246 echo ' ' . $gacl_api->_debug_msg . "\n";
248 else
250 // Assign to random groups.
251 $rand_key = $groups[mt_rand (0, $rand_max)];
252 $gacl_api->add_group_object ($rand_key, 'users', $i, 'AXO');
256 $elapsed = getmicrotime () - $start_time;
258 echo "Done\n";
259 echo ' Count: ' . $max . "\n";
260 echo ' Time: ' . $elapsed . " s\n";
261 echo ' Average: ' . $elapsed/$max . " s\n\n";
265 echo "<b>Generate random ACLs now.</b>\n";
266 flush ();
268 $start_time = getmicrotime ();
270 $start = 1;
271 $max = 10 * $scale;
273 $aco_list = $gacl_api->get_object ('system', 1, 'ACO');
275 $query = 'SELECT id, name FROM '.$gacl_api->_db_table_prefix.''. $gacl_.'aro_groups ORDER BY parent_id DESC LIMIT 100';
276 $rs = $gacl_api->db->Execute($query);
277 $aro_groups = $rs->GetAssoc();
279 $query = 'SELECT id, name FROM '.$gacl_api->_db_table_prefix.'axo_groups ORDER BY parent_id DESC LIMIT 100';
280 $rs = $gacl_api->db->Execute($query);
281 $axo_groups = $rs->GetAssoc();
283 // $aro_groups = $gacl_api->format_groups ($gacl_api->sort_groups ('ARO'), 'ARRAY');
285 print_r ($aro_groups);
287 // $axo_groups = $gacl_api->format_groups ($gacl_api->sort_groups ('AXO'), 'ARRAY');
289 print_r ($axo_groups);
291 for ( $i = $start; $i <= $max; $i++ )
293 $rand_aco_key = array_mt_rand ($aco_list, mt_rand (2, 10));
294 $rand_aro_key = array_mt_rand ($aro_groups, mt_rand(2,10));
295 $rand_axo_key = array_mt_rand ($axo_groups, mt_rand(2,10));
297 $aco_array = array ();
299 foreach ( $rand_aco_key as $aco_key )
301 $aco_data = $gacl_api->get_object_data ($aco_list[$aco_key], 'ACO');
302 $aco_array[$aco_data[0][0]][] = $aco_data[0][1];
305 // Randomly create ACLs with AXOs assigned to them.
306 // if ($i % 2 == 0) {
307 $axo_array = $rand_axo_key;
308 // }
310 if ( $gacl_api->add_acl ($aco_array, NULL, $rand_aro_key, NULL, $axo_array) == FALSE )
312 echo " Error creating ACL: $i.\n";
313 echo ' ' . $gacl_api->_debug_msg . "\n";
314 // print_r (array_slice ($gacl_api->_debug_msg, -2));
317 unset ($axo_array);
320 $elapsed = getmicrotime () - $start_time;
322 echo "Done\n";
323 echo ' Count: ' . $max . "\n";
324 echo ' Time: ' . $elapsed . " s\n";
325 echo ' Average: ' . $elapsed/$max . " s\n\n";
328 echo "<b>Generating Test Data Set</b>\n";
329 flush ();
331 $start_time = getmicrotime ();
333 $start = 1;
334 $max = 5 * $scale;
335 // $max = 1;
337 $check = array ();
339 for ( $i = $start; $i <= $max; $i++ )
341 $rand_aco_key = mt_rand (10,10 * $scale);
342 $rand_aro_key = mt_rand (10,1000 * $scale);
343 $rand_axo_key = mt_rand (10,1000 * $scale);
345 // echo ' Rand ACO: '. $rand_aco_key .' ARO: '. $rand_aro_key . ' AXO: ' . $rand_axo_key . "\n";
347 $aco_data = &$gacl_api->get_object_data ($rand_aco_key, 'ACO');
348 $aro_data = &$gacl_api->get_object_data ($rand_aro_key, 'ARO');
349 $axo_data = &$gacl_api->get_object_data ($rand_axo_key, 'AXO');
351 $check[$i] = array (
352 'aco' => $aco_data[0],
353 'aro' => $aro_data[0],
354 'axo' => $axo_data[0]
358 $elapsed = getmicrotime () - $start_time;
360 echo "Done\n\n";
361 echo ' Count: ' . $max . "\n";
362 echo ' Time: ' . $elapsed . " s\n";
363 echo ' Average: ' . $elapsed/$max . " s\n\n";
365 echo "<b>Testing...</b>\n";
366 flush ();
368 $best = 99999;
369 $worst = 0;
370 $total = 0;
372 foreach ( $check as $i => $data )
374 echo ' Trying: ACO Section: '. $data['aco'][0] .' Value: '. $data['aco'][1] .' ARO Section: '. $data['aro'][0] .' Value: '. $data['aro'][1] . ' ARO Section: '. $data['axo'][0] .' Value: '. $data['axo'][1] . "\n";
376 $check_start = getmicrotime ();
378 $allow = $gacl_api->acl_check ($data['aco'][0],$data['aco'][1],$data['aro'][0],$data['aro'][1],$data['axo'][0],$data['axo'][1]);
380 $check_time = getmicrotime () - $check_start;
382 if ( $allow ) {
383 echo '<font color="#00ff00"> ' . $i . ". Access Granted!</font>";
384 } else {
385 echo '<font color="#ff0000"> ' . $i . ". Access Denied!</font>";
388 echo ' - ' . $check_time . " s\n";
390 $best = min ($best, $check_time);
391 $worst = max ($worst, $check_time);
392 $total = $total + $check_time;
395 echo "Done\n";
396 echo ' Count: ' . $max . "\n\n";
398 echo ' Total: ' . $total . " s\n";
399 echo ' Average: ' . $total/$max . " s\n\n";
401 echo ' Best: ' . $best . " s\n";
402 echo ' Worst: ' . $worst . " s\n\n";
404 // print_r ($gacl_api->db);
407 $elapsed = getmicrotime () - $overall_start;
409 echo '<b>All Finished</b>' . "\n";
410 echo ' Total Time: ' . $elapsed . " s\n";
413 * end of script
417 </pre>
418 </body>
419 </html>