AclProviderBenchmarkTest.php 8.2 KB

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