AclProviderBenchmarkTest.php 8.4 KB

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