PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/horde-3.3.13/scripts/share-stress-test.php

#
PHP | 174 lines | 137 code | 21 blank | 16 comment | 30 complexity | c697d9ccf9ed583a18c3dc04fe0e87e4 MD5 | raw file
Possible License(s): LGPL-2.0
  1. #!/usr/bin/env php
  2. <?php
  3. @define('AUTH_HANDLER', true);
  4. @define('HORDE_BASE', dirname(__FILE__) . '/..');
  5. // Do CLI checks and environment setup first.
  6. require_once HORDE_BASE . '/lib/core.php';
  7. require_once 'Horde/CLI.php';
  8. // Make sure no one runs this from the web.
  9. if (!Horde_CLI::runningFromCLI()) {
  10. exit("Must be run from the command line\n");
  11. }
  12. // Load the CLI environment - make sure there's no time limit, init some
  13. // variables, etc.
  14. Horde_CLI::init();
  15. // Include needed libraries.
  16. require_once HORDE_BASE . '/lib/base.php';
  17. require_once 'Horde/Perms.php';
  18. require_once 'Horde/Share.php';
  19. require_once 'Horde/String.php';
  20. require_once 'Benchmark/Timer.php';
  21. require_once 'Console/Getopt.php';
  22. $conf['log']['name'] = '/dev/null';
  23. /* Setting up command line. */
  24. $getopt = new Console_Getopt();
  25. $argv = $getopt->readPHPArgv();
  26. if (is_a($argv, 'PEAR_Error')) {
  27. echo $argv->getMessage() . "\n";
  28. exit(2);
  29. }
  30. $cmd = basename(array_shift($argv));
  31. $description = <<<EOD
  32. This script creates a large numbers of shares with varying permission in the specified share backend and then runs some benchmarks on common share actions.
  33. Example: $cmd -b sqlng -c pgsql
  34. -b, --backend The backend to test. Must be configured in the conf.php
  35. configuration file.
  36. -c, --configuration The configuration to use for the specified backend.
  37. --users Number of users to create (default: 10000).
  38. --groups Number of groups to assume (default: 10).
  39. --sharing-users Percentage of users actually sharing with others
  40. (default: 5).
  41. --max-users Maximum number of users to share with (default: 4).
  42. --max-groups Maximum number of groups to share with (default: 1).
  43. --runs Number of executed test calls (default: 500).
  44. EOD;
  45. $result = $getopt->getopt2(
  46. $argv, 'b:c:',
  47. array('backend=', 'configuration=', 'users=', 'groups=', 'sharing-users=',
  48. 'max-users=', 'max-groups=', 'runs='));
  49. if (is_a($result, 'PEAR_Error')) {
  50. echo $description . "\n" . $result->getMessage() . "\n";
  51. exit(2);
  52. }
  53. $options = array();
  54. foreach ($result[0] as $option) {
  55. $name = str_replace('--', '', $option[0]);
  56. if ($name == 'b') $name = 'backend';
  57. if ($name == 'c') $name = 'configuration';
  58. $options[$name] = $option[1];
  59. }
  60. if (!isset($options['users'])) $options['users'] = 10000;
  61. if (!isset($options['groups'])) $options['groups'] = 10;
  62. if (!isset($options['sharing-users'])) $options['sharing-users'] = 5;
  63. if (!isset($options['max-users'])) $options['max-users'] = 4;
  64. if (!isset($options['max-groups'])) $options['max-groups'] = 1;
  65. if (!isset($options['runs'])) $options['runs'] = 500;
  66. $choices = array('sql', 'sqlng', 'kolab', 'datatree');
  67. if (!isset($options['backend']) || !in_array($options['backend'], $choices)) {
  68. echo $description . "\n";
  69. echo '--backend must be one of ' . implode(', ', $choices) . "\n";
  70. exit(2);
  71. }
  72. if (!isset($options['configuration'])) {
  73. echo $description . "\n";
  74. echo 'Missing --configuration argument.' . "\n";
  75. exit(2);
  76. }
  77. /* Load configuration. */
  78. if (!file_exists(dirname(__FILE__) . '/share-stress-test-conf.php')) {
  79. echo "Configuration file share-stress-test-conf.php missing.\n";
  80. exit(1);
  81. }
  82. require dirname(__FILE__) . '/share-stress-test-conf.php';
  83. /* Create storage backend. */
  84. switch ($options['backend']) {
  85. case 'sql':
  86. case 'sqlng':
  87. $conf['sql'] = isset($conf['share']['sql'][$options['configuration']])
  88. ? $conf['share']['sql'][$options['configuration']]
  89. : array();
  90. echo "Creating tables...";
  91. require_once 'Horde/SQL/Manager.php';
  92. $manager = Horde_SQL_Manager::getInstance($conf['sql']);
  93. $manager->_writer->db->query('DROP TABLE IF EXISTS test_shares');
  94. $manager->_writer->db->query('DROP TABLE IF EXISTS test_shares_groups');
  95. $manager->_writer->db->query('DROP TABLE IF EXISTS test_shares_users');
  96. $result = $manager->updateSchema(dirname(__FILE__) . '/share-stress-test-' . $options['backend'] . '.xml');
  97. if (is_a($result, 'PEAR_Error')) {
  98. echo "\n" . $result->toString();
  99. exit(1);
  100. }
  101. echo "done\n";
  102. break;
  103. default:
  104. echo "Storage configuration for ${options['backend']} not implemented yet.\n";
  105. exit(1);
  106. }
  107. /* Create share backend. */
  108. $class = 'Horde_Share_' . implode('_', array_map(array('String', 'ucfirst'), explode('_', $options['backend'])));
  109. $shares = Horde_Share::singleton('test', $options['backend']);
  110. if ($options['backend'] == 'sqlng') {
  111. $shares->_table = 'test_shares';
  112. }
  113. /* Start timer. */
  114. $timer = new Benchmark_Timer();
  115. /* Create test shares. */
  116. echo "Creating ${options['users']} users.\n";
  117. $timer->start();
  118. $users = $groups = 0;
  119. for ($i = 0; $i < $options['users']; $i++) {
  120. /* Create share. */
  121. $share = $shares->newShare(sprintf('user%0' . strlen($options['users']) . 'd', $i),
  122. md5(uniqid(mt_rand(), true)));
  123. $shares->addShare($share);
  124. /* Add permissions with some probability. */
  125. if (rand(0, 100) <= $options['sharing-users']) {
  126. for ($j = 0, $r = rand(0, $options['max-users']); $j < $r; $j++) {
  127. $share->addUserPermission(sprintf('user%0' . strlen($options['users']) . 'd', rand(0, $options['users'] - 1)), PERMS_SHOW | PERMS_READ);
  128. $users++;
  129. }
  130. for ($j = 0, $r = rand(0, $options['max-groups']); $j < $r; $j++) {
  131. $share->addGroupPermission(sprintf('group%0' . strlen($options['groups']) . 'd', rand(0, $options['groups'] - 1)), PERMS_SHOW | PERMS_READ);
  132. $groups++;
  133. }
  134. }
  135. /* Progress marker. */
  136. if (!($i % ($options['users'] / 10))) {
  137. echo '.';
  138. }
  139. }
  140. $timer->stop();
  141. echo "\nTime spent: " . $timer->timeElapsed() . " seconds\n";
  142. echo "Sharing with $users users and $groups groups.\n";
  143. echo "\nExecuting ${options['runs']} listShares() calls.\n";
  144. $timer->start();
  145. for ($i = 0; $i < $options['runs']; $i++) {
  146. $shares->listShares(sprintf('user%0' . strlen($options['users']) . 'd', rand(0, $options['users'] - 1)));
  147. /* Progress marker. */
  148. if (!($i % ($options['runs'] / 10))) {
  149. echo '.';
  150. }
  151. }
  152. $timer->stop();
  153. echo "\nTime spent: " . $timer->timeElapsed() . " seconds\n";