4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
23 $shortoptions = 'b:g:j:n:t:u:w:x:z:';
37 $helptext = <<<END_OF_CREATESIM_HELP
38 Creates a lot of test users and notices to (loosely) simulate a real server.
40 -b --subscriptions Average subscriptions per user (default no. users/20)
41 -g --groups Number of groups (default 20)
42 -j --joins Number of groups per user (default 5)
43 -f --faves Number of faves per user (default notices/10)
44 -n --notices Average notices per user (default 100)
45 -t --tags Number of distinct hash tags (default 10000)
46 -u --users Number of users (default 100)
47 -w --words Words file (default '/usr/share/dict/words')
48 -x --prefix User name prefix (default 'testuser')
49 -z --groupprefix Group name prefix (default 'testgroup')
51 END_OF_CREATESIM_HELP;
53 require_once INSTALLDIR.'/scripts/commandline.inc';
55 // XXX: make these command-line options
60 $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
61 'password' => sprintf('password%d', $i),
62 'fullname' => sprintf('Test User %d', $i)));
68 function newGroup($i, $j)
73 // Pick a random user to be the admin
75 $n = rand(0, max($j - 1, 0));
76 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
78 $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
80 'userid' => $user->id,
81 'fullname' => sprintf('Test Group %d', $i)));
84 function newNotice($i, $tagmax)
88 $options = array('scope' => Notice::defaultScope());
91 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
93 $is_reply = rand(0, 1);
95 $content = testNoticeContent();
98 $stream = new InboxNoticeStream($user, $user->getProfile());
99 $notices = $stream->getNotices(0, 20);
100 if ($notices->N > 0) {
101 $nval = rand(0, $notices->N - 1);
102 $notices->fetch(); // go to 0th
103 for ($i = 0; $i < $nval; $i++) {
106 $options['reply_to'] = $notices->id;
107 $dont_use_nickname = rand(0, 2);
108 if ($dont_use_nickname != 0) {
109 $rprofile = $notices->getProfile();
110 $content = "@".$rprofile->nickname." ".$content;
112 $private_to_addressees = rand(0, 4);
113 if ($private_to_addressees == 0) {
114 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
119 $has_hash = rand(0, 2);
121 if ($has_hash == 0) {
122 $hashcount = rand(0, 2);
123 for ($j = 0; $j < $hashcount; $j++) {
124 $h = rand(0, $tagmax);
125 $content .= " #tag{$h}";
129 $in_group = rand(0, 5);
131 if ($in_group == 0) {
132 $groups = $user->getGroups();
133 if ($groups->N > 0) {
134 $gval = rand(0, $groups->N - 1);
135 $groups->fetch(); // go to 0th
136 for ($i = 0; $i < $gval; $i++) {
139 $options['groups'] = array($groups->id);
140 $content = "!".$groups->nickname." ".$content;
141 $private_to_group = rand(0, 2);
142 if ($private_to_group == 0) {
143 $options['scope'] |= Notice::GROUP_SCOPE;
148 $private_to_site = rand(0, 4);
150 if ($private_to_site == 0) {
151 $options['scope'] |= Notice::SITE_SCOPE;
154 $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
160 $f = rand(0, $i - 1);
162 $fromnick = sprintf('%s%d', $userprefix, $f);
164 $from = User::staticGet('nickname', $fromnick);
167 throw new Exception("Can't find user '$fromnick'.");
170 $t = rand(0, $i - 1);
179 $tunic = sprintf('%s%d', $userprefix, $t);
181 $to = User::staticGet('nickname', $tunic);
184 throw new Exception("Can't find user '$tunic'.");
187 subs_subscribe_to($from, $to);
193 function newJoin($u, $g)
198 $userNumber = rand(0, $u - 1);
200 $userNick = sprintf('%s%d', $userprefix, $userNumber);
202 $user = User::staticGet('nickname', $userNick);
205 throw new Exception("Can't find user '$fromnick'.");
208 $groupNumber = rand(0, $g - 1);
210 $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
212 $group = User_group::staticGet('nickname', $groupNick);
215 throw new Exception("Can't find group '$groupNick'.");
218 if (!$user->isMember($group)) {
219 $user->joinGroup($group);
228 $userNumber = rand(0, $u - 1);
230 $userNick = sprintf('%s%d', $userprefix, $userNumber);
232 $user = User::staticGet('nickname', $userNick);
235 throw new Exception("Can't find user '$userNick'.");
238 // NB: it's OK to like your own stuff!
240 $otherNumber = rand(0, $u - 1);
242 $otherNick = sprintf('%s%d', $userprefix, $otherNumber);
244 $other = User::staticGet('nickname', $otherNick);
247 throw new Exception("Can't find user '$otherNick'.");
250 $notices = $other->getNotices()->fetchAll();
252 if (count($notices) == 0) {
256 $idx = rand(0, count($notices) - 1);
258 $notice = $notices[$idx];
260 if ($user->hasFave($notice)) {
264 Fave::addNew($user->getProfile(), $notice);
267 function testNoticeContent()
271 if (is_null($words)) {
272 return "test notice content";
277 $ids = array_rand($words, $cnt);
279 foreach ($ids as $id) {
280 $parts[] = $words[$id];
283 $text = implode(' ', $parts);
285 if (mb_strlen($text) > 80) {
286 $text = substr($text, 0, 77) . "...";
292 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $tagmax)
295 $config['site']['dupelimit'] = -1;
302 $preuser = min($usercount, 5);
304 for ($j = 0; $j < $preuser; $j++) {
305 printfv("$i Creating user $n\n");
310 $pregroup = min($groupcount, 3);
312 for ($k = 0; $k < $pregroup; $k++) {
313 printfv("$i Creating group $g\n");
318 // # registrations + # notices + # subs
320 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $favesavg));
323 $events -= $pregroup;
326 $gt = $ut + $groupcount;
327 $nt = $gt + ($usercount * $noticeavg);
328 $st = $nt + ($usercount * $subsavg);
329 $jt = $st + ($usercount * $joinsavg);
330 $ft = $jt + ($usercount * $favesavg);
332 printfv("$events events ($ut, $gt, $nt, $st, $jt, $ft)\n");
334 for ($i = 0; $i < $events; $i++)
336 $e = rand(0, $events);
338 if ($e >= 0 && $e <= $ut) {
339 printfv("$i Creating user $n\n");
342 } else if ($e > $ut && $e <= $gt) {
343 printfv("$i Creating group $g\n");
346 } else if ($e > $gt && $e <= $nt) {
347 printfv("$i Making a new notice\n");
348 newNotice($n, $tagmax);
349 } else if ($e > $nt && $e <= $st) {
350 printfv("$i Making a new subscription\n");
352 } else if ($e > $st && $e <= $jt) {
353 printfv("$i Making a new group join\n");
355 } else if ($e > $jt && $e <= $ft) {
356 printfv("$i Making a new fave\n");
359 printfv("No event for $i!");
364 $defaultWordsfile = '/usr/share/dict/words';
366 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
367 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
368 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
369 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
370 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
371 $favesavg = (have_option('f', 'faves')) ? get_option_value('f', 'faves') : max($noticeavg/10, 5);
372 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
373 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
374 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
375 $wordsfile = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
377 if (is_readable($wordsfile)) {
378 $words = file($wordsfile);
380 if ($wordsfile != $defaultWordsfile) {
381 // user specified words file couldn't be read
382 throw new Exception("Couldn't read words file: {$wordsfile}.");
388 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $tagmax);
389 } catch (Exception $e) {
390 printfv("Got an exception: ".$e->getMessage());