AclProviderBenchmarkTest.php 8.1 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. } else {
  101. return rand(1000, $id-1);
  102. }
  103. }
  104. protected function generateAcl($classId, $parentId, $ancestors)
  105. {
  106. static $id = 1000;
  107. $this->insertOidStmt->execute(array(
  108. $id,
  109. $classId,
  110. $this->getRandomString(rand(20, 50)),
  111. $parentId,
  112. rand(0, 1),
  113. ));
  114. $this->insertOidAncestorStmt->execute(array($id, $id));
  115. foreach ($ancestors as $ancestor) {
  116. $this->insertOidAncestorStmt->execute(array($id, $ancestor));
  117. }
  118. $this->generateAces($classId, $id);
  119. $id += 1;
  120. return $id-1;
  121. }
  122. protected function chooseSid()
  123. {
  124. static $id = 1000;
  125. if ($id === 1000 || ($id < 11000 && rand(0, 1))) {
  126. $this->insertSidStmt->execute(array(
  127. $id,
  128. $this->getRandomString(rand(5, 30)),
  129. rand(0, 1)
  130. ));
  131. $id += 1;
  132. return $id-1;
  133. } else {
  134. return rand(1000, $id-1);
  135. }
  136. }
  137. protected function generateAces($classId, $objectId)
  138. {
  139. static $id = 1000;
  140. $sids = array();
  141. $fieldOrder = array();
  142. for ($i=0; $i<=30; $i++) {
  143. $fieldName = rand(0, 1) ? null : $this->getRandomString(rand(10, 20));
  144. do {
  145. $sid = $this->chooseSid();
  146. }
  147. while (array_key_exists($sid, $sids) && in_array($fieldName, $sids[$sid], true));
  148. $fieldOrder[$fieldName] = array_key_exists($fieldName, $fieldOrder) ? $fieldOrder[$fieldName]+1 : 0;
  149. if (!isset($sids[$sid])) {
  150. $sids[$sid] = array();
  151. }
  152. $sids[$sid][] = $fieldName;
  153. $strategy = rand(0, 2);
  154. if ($strategy === 0) {
  155. $strategy = PermissionGrantingStrategy::ALL;
  156. } else if ($strategy === 1) {
  157. $strategy = PermissionGrantingStrategy::ANY;
  158. } else {
  159. $strategy = PermissionGrantingStrategy::EQUAL;
  160. }
  161. // id, cid, oid, field, order, sid, mask, granting, strategy, a success, a failure
  162. $this->insertEntryStmt->execute(array(
  163. $id,
  164. $classId,
  165. rand(0, 5) ? $objectId : null,
  166. $fieldName,
  167. $fieldOrder[$fieldName],
  168. $sid,
  169. $this->generateMask(),
  170. rand(0, 1),
  171. $strategy,
  172. rand(0, 1),
  173. rand(0, 1),
  174. ));
  175. $id += 1;
  176. }
  177. }
  178. protected function generateMask()
  179. {
  180. $i = rand(1, 30);
  181. $mask = 0;
  182. while ($i <= 30) {
  183. $mask |= 1 << rand(0, 30);
  184. $i++;
  185. }
  186. return $mask;
  187. }
  188. protected function getRandomString($length, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
  189. {
  190. $s = '';
  191. $cLength = strlen($chars);
  192. while (strlen($s) < $length) {
  193. $s .= $chars[mt_rand(0, $cLength-1)];
  194. }
  195. return $s;
  196. }
  197. protected function getOptions()
  198. {
  199. return array(
  200. 'oid_table_name' => 'acl_object_identities',
  201. 'oid_ancestors_table_name' => 'acl_object_identity_ancestors',
  202. 'class_table_name' => 'acl_classes',
  203. 'sid_table_name' => 'acl_security_identities',
  204. 'entry_table_name' => 'acl_entries',
  205. );
  206. }
  207. protected function getStrategy()
  208. {
  209. return new PermissionGrantingStrategy();
  210. }
  211. protected function getProvider()
  212. {
  213. return new AclProvider($this->con, $this->getStrategy(), $this->getOptions());
  214. }
  215. }