createsim.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR);
  22. $shortoptions = 'b:g:j:n:t:u:w:x:z:';
  23. $longoptions = array(
  24. 'subscriptions=',
  25. 'groups=',
  26. 'joins=',
  27. 'notices=',
  28. 'tags=',
  29. 'users=',
  30. 'words=',
  31. 'prefix=',
  32. 'groupprefix=',
  33. // 'faves=',
  34. );
  35. $helptext = <<<END_OF_CREATESIM_HELP
  36. Creates a lot of test users and notices to (loosely) simulate a real server.
  37. -b --subscriptions Average subscriptions per user (default no. users/20)
  38. -g --groups Number of groups (default 20)
  39. -j --joins Number of groups per user (default 5)
  40. -n --notices Average notices per user (default 100)
  41. -t --tags Number of distinct hash tags (default 10000)
  42. -u --users Number of users (default 100)
  43. -w --words Words file (default '/usr/share/dict/words')
  44. -x --prefix User name prefix (default 'testuser')
  45. -z --groupprefix Group name prefix (default 'testgroup')
  46. END_OF_CREATESIM_HELP;
  47. require_once INSTALLDIR.'/scripts/commandline.inc';
  48. // XXX: make these command-line options
  49. function newUser($i)
  50. {
  51. global $userprefix;
  52. $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
  53. 'password' => sprintf('password%d', $i),
  54. 'fullname' => sprintf('Test User %d', $i)));
  55. if (!empty($user)) {
  56. $user->free();
  57. }
  58. }
  59. function newGroup($i, $j)
  60. {
  61. global $groupprefix;
  62. global $userprefix;
  63. // Pick a random user to be the admin
  64. $n = rand(0, max($j - 1, 0));
  65. $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
  66. $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
  67. 'local' => true,
  68. 'userid' => $user->id,
  69. 'fullname' => sprintf('Test Group %d', $i)));
  70. }
  71. function newNotice($i, $tagmax)
  72. {
  73. global $userprefix;
  74. $options = array('scope' => Notice::defaultScope());
  75. $n = rand(0, $i - 1);
  76. $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
  77. $is_reply = rand(0, 1);
  78. $content = testNoticeContent();
  79. if ($is_reply == 0) {
  80. $stream = new InboxNoticeStream($user->getProfile(), $user->getProfile());
  81. $notices = $stream->getNotices(0, 20);
  82. if ($notices->N > 0) {
  83. $nval = rand(0, $notices->N - 1);
  84. $notices->fetch(); // go to 0th
  85. for ($i = 0; $i < $nval; $i++) {
  86. $notices->fetch();
  87. }
  88. $options['reply_to'] = $notices->id;
  89. $dont_use_nickname = rand(0, 2);
  90. if ($dont_use_nickname != 0) {
  91. $rprofile = $notices->getProfile();
  92. $content = "@".$rprofile->nickname." ".$content;
  93. }
  94. $private_to_addressees = rand(0, 4);
  95. if ($private_to_addressees == 0) {
  96. $options['scope'] |= Notice::ADDRESSEE_SCOPE;
  97. }
  98. }
  99. } else {
  100. $is_directed = rand(0, 4);
  101. if ($is_directed == 0) {
  102. $subs = $user->getSubscribed(0, 100)->fetchAll();
  103. if (count($subs) > 0) {
  104. $seen = array();
  105. $f = rand(0, 9);
  106. if ($f <= 6) {
  107. $addrs = 1;
  108. } else if ($f <= 8) {
  109. $addrs = 2;
  110. } else {
  111. $addrs = 3;
  112. }
  113. for ($m = 0; $m < $addrs; $m++) {
  114. $x = rand(0, count($subs) - 1);
  115. if ($seen[$x]) {
  116. continue;
  117. }
  118. if ($subs[$x]->id == $user->id) {
  119. continue;
  120. }
  121. $seen[$x] = true;
  122. $rprofile = $subs[$x];
  123. $content = "@".$rprofile->nickname." ".$content;
  124. }
  125. $private_to_addressees = rand(0, 4);
  126. if ($private_to_addressees == 0) {
  127. $options['scope'] |= Notice::ADDRESSEE_SCOPE;
  128. }
  129. }
  130. }
  131. }
  132. $has_hash = rand(0, 2);
  133. if ($has_hash == 0) {
  134. $hashcount = rand(0, 2);
  135. for ($j = 0; $j < $hashcount; $j++) {
  136. $h = rand(0, $tagmax);
  137. $content .= " #tag{$h}";
  138. }
  139. }
  140. $in_group = rand(0, 5);
  141. if ($in_group == 0) {
  142. $groups = $user->getGroups();
  143. if ($groups instanceof User_group) {
  144. $gval = rand(0, $groups->N - 1);
  145. $groups->fetch(); // go to 0th
  146. for ($i = 0; $i < $gval; $i++) {
  147. $groups->fetch();
  148. }
  149. $options['groups'] = array($groups->id);
  150. $content = "!".$groups->nickname." ".$content;
  151. $private_to_group = rand(0, 2);
  152. if ($private_to_group == 0) {
  153. $options['scope'] |= Notice::GROUP_SCOPE;
  154. }
  155. }
  156. }
  157. $private_to_site = rand(0, 4);
  158. if ($private_to_site == 0) {
  159. $options['scope'] |= Notice::SITE_SCOPE;
  160. }
  161. $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
  162. }
  163. /* Plugins should be part of the simulation too!
  164. function newMessage($i)
  165. {
  166. global $userprefix;
  167. $n = rand(0, $i - 1);
  168. $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
  169. $content = testNoticeContent();
  170. $friends = $user->mutuallySubscribedUsers()->fetchAll();
  171. if (count($friends) == 0) {
  172. return;
  173. }
  174. $j = rand(0, count($friends) - 1);
  175. $other = $friends[$j];
  176. $message = Message::saveNew($user->id, $other->id, $content, 'createsim');
  177. }*/
  178. function newSub($i)
  179. {
  180. global $userprefix;
  181. $f = rand(0, $i - 1);
  182. $fromnick = sprintf('%s%d', $userprefix, $f);
  183. $from = User::getKV('nickname', $fromnick);
  184. if (empty($from)) {
  185. throw new Exception("Can't find user '$fromnick'.");
  186. }
  187. $t = rand(0, $i - 1);
  188. if ($t == $f) {
  189. $t++;
  190. if ($t > $i - 1) {
  191. $t = 0;
  192. }
  193. }
  194. $tunic = sprintf('%s%d', $userprefix, $t);
  195. $to = User::getKV('nickname', $tunic);
  196. if (!($to instanceof User)) {
  197. throw new Exception("Can't find user '$tunic'.");
  198. }
  199. Subscription::start($from->getProfile(), $to->getProfile());
  200. $from->free();
  201. $to->free();
  202. }
  203. function newJoin($u, $g)
  204. {
  205. global $userprefix;
  206. global $groupprefix;
  207. $userNumber = rand(0, $u - 1);
  208. $userNick = sprintf('%s%d', $userprefix, $userNumber);
  209. $user = User::getKV('nickname', $userNick);
  210. if (empty($user)) {
  211. throw new Exception("Can't find user '$fromnick'.");
  212. }
  213. $groupNumber = rand(0, $g - 1);
  214. $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
  215. $group = User_group::getKV('nickname', $groupNick);
  216. if (empty($group)) {
  217. throw new Exception("Can't find group '$groupNick'.");
  218. }
  219. if (!$user->isMember($group)) {
  220. $user->joinGroup($group);
  221. }
  222. }
  223. /* Plugins should be part of the simulation too!
  224. function newFave($u)
  225. {
  226. global $userprefix;
  227. global $groupprefix;
  228. $userNumber = rand(0, $u - 1);
  229. $userNick = sprintf('%s%d', $userprefix, $userNumber);
  230. $user = User::getKV('nickname', $userNick);
  231. if (empty($user)) {
  232. throw new Exception("Can't find user '$userNick'.");
  233. }
  234. // NB: it's OK to like your own stuff!
  235. $otherNumber = rand(0, $u - 1);
  236. $otherNick = sprintf('%s%d', $userprefix, $otherNumber);
  237. $other = User::getKV('nickname', $otherNick);
  238. if (empty($other)) {
  239. throw new Exception("Can't find user '$otherNick'.");
  240. }
  241. $notices = $other->getNotices()->fetchAll();
  242. if (count($notices) == 0) {
  243. return;
  244. }
  245. $idx = rand(0, count($notices) - 1);
  246. $notice = $notices[$idx];
  247. if ($user->hasFave($notice)) {
  248. return;
  249. }
  250. Fave::addNew($user->getProfile(), $notice);
  251. }*/
  252. function testNoticeContent()
  253. {
  254. global $words;
  255. if (is_null($words)) {
  256. return "test notice content";
  257. }
  258. $cnt = rand(3, 8);
  259. $ids = array_rand($words, $cnt);
  260. foreach ($ids as $id) {
  261. $parts[] = $words[$id];
  262. }
  263. $text = implode(' ', $parts);
  264. if (mb_strlen($text) > 80) {
  265. $text = substr($text, 0, 77) . "...";
  266. }
  267. return $text;
  268. }
  269. //function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax)
  270. function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
  271. {
  272. global $config;
  273. $config['site']['dupelimit'] = -1;
  274. $n = 0;
  275. $g = 0;
  276. // Make users first
  277. $preuser = min($usercount, 5);
  278. for ($j = 0; $j < $preuser; $j++) {
  279. printfv("$i Creating user $n\n");
  280. newUser($n);
  281. $n++;
  282. }
  283. $pregroup = min($groupcount, 3);
  284. for ($k = 0; $k < $pregroup; $k++) {
  285. printfv("$i Creating group $g\n");
  286. newGroup($g, $n);
  287. $g++;
  288. }
  289. // # registrations + # notices + # subs
  290. //$events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $favesavg + $messageavg));
  291. $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
  292. $events -= $preuser;
  293. $events -= $pregroup;
  294. $ut = $usercount;
  295. $gt = $ut + $groupcount;
  296. $nt = $gt + ($usercount * $noticeavg);
  297. $st = $nt + ($usercount * $subsavg);
  298. $jt = $st + ($usercount * $joinsavg);
  299. // $ft = $jt + ($usercount * $favesavg);
  300. // $mt = $ft + ($usercount * $messageavg);
  301. // printfv("$events events ($ut, $gt, $nt, $st, $jt, $ft, $mt)\n");
  302. printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
  303. for ($i = 0; $i < $events; $i++)
  304. {
  305. $e = rand(0, $events);
  306. if ($e >= 0 && $e <= $ut) {
  307. printfv("$i Creating user $n\n");
  308. newUser($n);
  309. $n++;
  310. } else if ($e > $ut && $e <= $gt) {
  311. printfv("$i Creating group $g\n");
  312. newGroup($g, $n);
  313. $g++;
  314. } else if ($e > $gt && $e <= $nt) {
  315. printfv("$i Making a new notice\n");
  316. newNotice($n, $tagmax);
  317. } else if ($e > $nt && $e <= $st) {
  318. printfv("$i Making a new subscription\n");
  319. newSub($n);
  320. } else if ($e > $st && $e <= $jt) {
  321. printfv("$i Making a new group join\n");
  322. newJoin($n, $g);
  323. /* } else if ($e > $jt && $e <= $ft) {
  324. printfv("$i Making a new fave\n");
  325. newFave($n);*/
  326. /* } else if ($e > $ft && $e <= $mt) {
  327. printfv("$i Making a new message\n");
  328. newMessage($n);*/
  329. } else {
  330. printfv("No event for $i!");
  331. }
  332. }
  333. }
  334. $defaultWordsfile = '/usr/share/dict/words';
  335. $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
  336. $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
  337. $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
  338. $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
  339. $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
  340. //$favesavg = (have_option('f', 'faves')) ? get_option_value('f', 'faves') : max($noticeavg/10, 5);
  341. //$messageavg = (have_option('m', 'messages')) ? get_option_value('m', 'messages') : max($noticeavg/10, 5);
  342. $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
  343. $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
  344. $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
  345. $wordsfile = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
  346. if (is_readable($wordsfile)) {
  347. $words = file($wordsfile);
  348. } else {
  349. if ($wordsfile != $defaultWordsfile) {
  350. // user specified words file couldn't be read
  351. throw new Exception("Couldn't read words file: {$wordsfile}.");
  352. }
  353. $words = null;
  354. }
  355. try {
  356. //main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax);
  357. main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
  358. } catch (Exception $e) {
  359. printfv("Got an exception: ".$e->getMessage());
  360. }