AclProviderBenchmarkTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Acl\Dbal;
  3. use Symfony\Component\Security\Acl\Dbal\AclProvider;
  4. use Symfony\Component\Security\Acl\Domain\PermissionGrantingStrategy;
  5. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  6. use Symfony\Component\Security\Acl\Dbal\Schema;
  7. use Doctrine\DBAL\DriverManager;
  8. class AclProviderBenchmarkTest extends \PHPUnit_Framework_TestCase
  9. {
  10. protected $con;
  11. protected $insertClassStmt;
  12. protected $insertSidStmt;
  13. protected $insertOidAncestorStmt;
  14. protected $insertOidStmt;
  15. protected $insertEntryStmt;
  16. public function testFindAcls()
  17. {
  18. // $this->generateTestData();
  19. // get some random test object identities from the database
  20. $oids = array();
  21. $stmt = $this->con->executeQuery("SELECT object_identifier, class_type FROM acl_object_identities o INNER JOIN acl_classes c ON c.id = o.class_id ORDER BY RAND() LIMIT 25");
  22. foreach ($stmt->fetchAll() as $oid) {
  23. $oids[] = new ObjectIdentity($oid['object_identifier'], $oid['class_type']);
  24. }
  25. $provider = $this->getProvider();
  26. $start = microtime(true);
  27. $provider->findAcls($oids);
  28. $time = microtime(true) - $start;
  29. echo "Total Time: ".$time."s\n";
  30. }
  31. protected function setUp()
  32. {
  33. // comment the following line, and run only this test, if you need to benchmark
  34. $this->markTestSkipped();
  35. $this->con = DriverManager::getConnection(array(
  36. 'driver' => 'pdo_mysql',
  37. 'host' => 'localhost',
  38. 'user' => 'root',
  39. 'dbname' => 'testdb',
  40. ));
  41. }
  42. protected function tearDown()
  43. {
  44. $this->con = null;
  45. }
  46. /**
  47. * This generates a huge amount of test data to be used mainly for benchmarking
  48. * purposes, not so much for testing. That's why it's not called by default.
  49. */
  50. protected function generateTestData()
  51. {
  52. $sm = $this->con->getSchemaManager();
  53. $sm->dropAndCreateDatabase('testdb');
  54. $this->con->exec("USE testdb");
  55. // import the schema
  56. $schema = new Schema($options = $this->getOptions());
  57. foreach ($schema->toSql($this->con->getDatabasePlatform()) as $sql) {
  58. $this->con->exec($sql);
  59. }
  60. // setup prepared statements
  61. $this->insertClassStmt = $this->con->prepare('INSERT INTO acl_classes (id, class_type) VALUES (?, ?)');
  62. $this->insertSidStmt = $this->con->prepare('INSERT INTO acl_security_identities (id, identifier, username) VALUES (?, ?, ?)');
  63. $this->insertOidStmt = $this->con->prepare('INSERT INTO acl_object_identities (id, class_id, object_identifier, parent_object_identity_id, entries_inheriting) VALUES (?, ?, ?, ?, ?)');
  64. $this->insertEntryStmt = $this->con->prepare('INSERT INTO acl_entries (id, class_id, object_identity_id, field_name, ace_order, security_identity_id, mask, granting, granting_strategy, audit_success, audit_failure) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)');
  65. $this->insertOidAncestorStmt = $this->con->prepare('INSERT INTO acl_object_identity_ancestors (object_identity_id, ancestor_id) VALUES (?, ?)');
  66. for ($i=0; $i<40000; $i++) {
  67. $this->generateAclHierarchy();
  68. }
  69. }
  70. protected function generateAclHierarchy()
  71. {
  72. $rootId = $this->generateAcl($this->chooseClassId(), null, array());
  73. $this->generateAclLevel(rand(1, 15), $rootId, array($rootId));
  74. }
  75. protected function generateAclLevel($depth, $parentId, $ancestors)
  76. {
  77. $level = count($ancestors);
  78. for ($i=0,$t=rand(1, 10); $i<$t; $i++) {
  79. $id = $this->generateAcl($this->chooseClassId(), $parentId, $ancestors);
  80. if ($level < $depth) {
  81. $this->generateAclLevel($depth, $id, array_merge($ancestors, array($id)));
  82. }
  83. }
  84. }
  85. protected function chooseClassId()
  86. {
  87. static $id = 1000;
  88. if ($id === 1000 || ($id < 1500 && rand(0, 1))) {
  89. $this->insertClassStmt->execute(array($id, $this->getRandomString(rand(20, 100), 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\\_')));
  90. $id += 1;
  91. return $id-1;
  92. }
  93. else {
  94. return rand(1000, $id-1);
  95. }
  96. }
  97. protected function generateAcl($classId, $parentId, $ancestors)
  98. {
  99. static $id = 1000;
  100. $this->insertOidStmt->execute(array(
  101. $id,
  102. $classId,
  103. $this->getRandomString(rand(20, 50)),
  104. $parentId,
  105. rand(0, 1),
  106. ));
  107. $this->insertOidAncestorStmt->execute(array($id, $id));
  108. foreach ($ancestors as $ancestor) {
  109. $this->insertOidAncestorStmt->execute(array($id, $ancestor));
  110. }
  111. $this->generateAces($classId, $id);
  112. $id += 1;
  113. return $id-1;
  114. }
  115. protected function chooseSid()
  116. {
  117. static $id = 1000;
  118. if ($id === 1000 || ($id < 11000 && rand(0, 1))) {
  119. $this->insertSidStmt->execute(array(
  120. $id,
  121. $this->getRandomString(rand(5, 30)),
  122. rand(0, 1)
  123. ));
  124. $id += 1;
  125. return $id-1;
  126. }
  127. else {
  128. return rand(1000, $id-1);
  129. }
  130. }
  131. protected function generateAces($classId, $objectId)
  132. {
  133. static $id = 1000;
  134. $sids = array();
  135. $fieldOrder = array();
  136. for ($i=0; $i<=30; $i++) {
  137. $fieldName = rand(0, 1) ? null : $this->getRandomString(rand(10, 20));
  138. do {
  139. $sid = $this->chooseSid();
  140. }
  141. while (array_key_exists($sid, $sids) && in_array($fieldName, $sids[$sid], true));
  142. $fieldOrder[$fieldName] = array_key_exists($fieldName, $fieldOrder) ? $fieldOrder[$fieldName]+1 : 0;
  143. if (!isset($sids[$sid])) {
  144. $sids[$sid] = array();
  145. }
  146. $sids[$sid][] = $fieldName;
  147. $strategy = rand(0, 2);
  148. if ($strategy === 0) {
  149. $strategy = PermissionGrantingStrategy::ALL;
  150. }
  151. else if ($strategy === 1) {
  152. $strategy = PermissionGrantingStrategy::ANY;
  153. }
  154. else {
  155. $strategy = PermissionGrantingStrategy::EQUAL;
  156. }
  157. // id, cid, oid, field, order, sid, mask, granting, strategy, a success, a failure
  158. $this->insertEntryStmt->execute(array(
  159. $id,
  160. $classId,
  161. rand(0, 5) ? $objectId : null,
  162. $fieldName,
  163. $fieldOrder[$fieldName],
  164. $sid,
  165. $this->generateMask(),
  166. rand(0, 1),
  167. $strategy,
  168. rand(0, 1),
  169. rand(0, 1),
  170. ));
  171. $id += 1;
  172. }
  173. }
  174. protected function generateMask()
  175. {
  176. $i = rand(1, 30);
  177. $mask = 0;
  178. while ($i <= 30) {
  179. $mask |= 1 << rand(0, 30);
  180. $i++;
  181. }
  182. return $mask;
  183. }
  184. protected function getRandomString($length, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
  185. {
  186. $s = '';
  187. $cLength = strlen($chars);
  188. while (strlen($s) < $length) {
  189. $s .= $chars[mt_rand(0, $cLength-1)];
  190. }
  191. return $s;
  192. }
  193. protected function getOptions()
  194. {
  195. return array(
  196. 'oid_table_name' => 'acl_object_identities',
  197. 'oid_ancestors_table_name' => 'acl_object_identity_ancestors',
  198. 'class_table_name' => 'acl_classes',
  199. 'sid_table_name' => 'acl_security_identities',
  200. 'entry_table_name' => 'acl_entries',
  201. );
  202. }
  203. protected function getStrategy()
  204. {
  205. return new PermissionGrantingStrategy();
  206. }
  207. protected function getProvider()
  208. {
  209. return new AclProvider($this->con, $this->getStrategy(), $this->getOptions());
  210. }
  211. }