PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/phpgacl/test_suite/random_acl_check.php

https://github.com/radicaldesigns/amp
PHP | 201 lines | 133 code | 49 blank | 19 comment | 9 complexity | c0d129155eabfc627c7dc2dbde4fc41e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Random ACL Check</title>
  5. </head>
  6. <body>
  7. <pre>
  8. <?php
  9. set_time_limit (6000);
  10. /*! function
  11. get time accurate to the nearest microsecond, used for script timing
  12. !*/
  13. function getmicrotime ()
  14. {
  15. list ($usec, $sec) = explode (' ', microtime ());
  16. return (float)$usec + (float)$sec;
  17. }
  18. /*! function
  19. a better array_rand, this one actualluy works on windows
  20. !*/
  21. function array_mt_rand ($array, $items)
  22. {
  23. $keys = array_keys ($array);
  24. $max = count ($keys) - 1;
  25. if ( $items == 1 )
  26. {
  27. return $keys[mt_rand (0, $max)];
  28. }
  29. $return = array ();
  30. for ( $i = 1; $i <= $items; $i++ )
  31. {
  32. $return[] = $keys[mt_rand (0, $max)];
  33. }
  34. return $return;
  35. }
  36. /*! function
  37. grab random objects from the database
  38. !*/
  39. function random_objects ($type, $limit = NULL)
  40. {
  41. $sql = 'SELECT id, section_value, value FROM ' . $GLOBALS['gacl_api']->_db_table_prefix . $type . ' ORDER BY RAND()';
  42. if ( is_scalar ($limit) )
  43. {
  44. $rs = $GLOBALS['gacl_api']->db->SelectLimit ($sql,$limit);
  45. }
  46. else
  47. {
  48. $rs = $GLOBALS['gacl_api']->db->Execute ($sql);
  49. }
  50. if ( !is_object ($rs) )
  51. {
  52. return FALSE;
  53. }
  54. $retarr = array ();
  55. while ( $row = $rs->FetchRow () )
  56. {
  57. $retarr[$row[0]] = array (
  58. $row[1],
  59. $row[2]
  60. );
  61. }
  62. return $retarr;
  63. }
  64. // require gacl
  65. require_once (dirname (__FILE__) . '/../admin/gacl_admin.inc.php');
  66. /*
  67. * Let's get ready to RUMBLE!!!
  68. */
  69. $scale = 100;
  70. echo '<b>Random ACL Check</b>' . "\n";
  71. echo ' Scale: ' . $scale . "\n\n";
  72. $overall_start = getmicrotime ();
  73. mt_srand ((double)microtime () *10000);
  74. echo "<b>Generating Test Data Set</b>\n";
  75. flush ();
  76. $start_time = getmicrotime ();
  77. $start = 1;
  78. $max = 5 * $scale;
  79. // $max = 1;
  80. $check = array ();
  81. $aco = random_objects ('aco', $max);
  82. $aro = random_objects ('aro', $max);
  83. $axo = random_objects ('axo', $max);
  84. for ( $i = $start; $i <= $max; $i++ )
  85. {
  86. $rand_aco_id = array_mt_rand ($aco, 1);
  87. $rand_aro_id = array_mt_rand ($aro, 1);
  88. $rand_axo_id = array_mt_rand ($axo, 1);
  89. // echo ' Rand ACO: '. $rand_aco_id .' ARO: '. $rand_aro_id . ' AXO: ' . $rand_axo_id . "\n";
  90. $check[$i] = array (
  91. 'aco' => $aco[$rand_aco_id],
  92. 'aro' => $aro[$rand_aro_id],
  93. 'axo' => $axo[$rand_axo_id]
  94. );
  95. }
  96. $elapsed = getmicrotime () - $start_time;
  97. echo "Done\n\n";
  98. echo ' Count: ' . $max . "\n";
  99. echo ' Time: ' . $elapsed . " s\n";
  100. echo ' Average: ' . $elapsed/$max . " s\n\n";
  101. echo "<b>Testing...</b>\n";
  102. flush ();
  103. $best = 99999;
  104. $worst = 0;
  105. $total = 0;
  106. $allowed = 0;
  107. $denied = 0;
  108. $allowed_time = 0;
  109. $denied_time = 0;
  110. foreach ( $check as $i => $data )
  111. {
  112. 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";
  113. $check_start = getmicrotime ();
  114. $allow = $gacl_api->acl_check ($data['aco'][0],$data['aco'][1],$data['aro'][0],$data['aro'][1],$data['axo'][0],$data['axo'][1]);
  115. $check_time = getmicrotime () - $check_start;
  116. if ( $allow ) {
  117. echo '<font color="#00ff00"> ' . $i . ". Access Granted</font>";
  118. $allowed++;
  119. $allowed_time += $check_time;
  120. } else {
  121. echo '<font color="#ff0000"> ' . $i . ". Access Denied</font>";
  122. $denied++;
  123. $denied_time += $check_time;
  124. }
  125. echo ' - ' . $check_time . " s\n";
  126. $best = min ($best, $check_time);
  127. $worst = max ($worst, $check_time);
  128. $total = $total + $check_time;
  129. }
  130. echo "Done\n";
  131. echo ' Count: ' . $max . "\n";
  132. echo ' Total: ' . $total . " s\n";
  133. echo ' Average: ' . $total/$max . " s\n\n";
  134. echo ' Allowed: ' . $allowed . "\n";
  135. echo ' Total: ' . $allowed_time . " s\n";
  136. echo ' Average: ' . $allowed_time/$allowed . " s\n\n";
  137. echo ' Denied: ' . $denied . "\n";
  138. echo ' Total: ' . $denied_time . " s\n";
  139. echo ' Average: ' . $denied_time/$denied . " s\n\n";
  140. echo ' Best: ' . $best . " s\n";
  141. echo ' Worst: ' . $worst . " s\n\n";
  142. // print_r ($gacl_api->db);
  143. $elapsed = getmicrotime () - $overall_start;
  144. echo '<b>All Finished</b>' . "\n";
  145. echo ' Total Time: ' . $elapsed . " s\n";
  146. /*
  147. * end of script
  148. */
  149. ?>
  150. </pre>
  151. </body>
  152. </html>