AclProviderBenchmarkTest.php 8.3 KB

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