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 -n --notices Average notices per user (default 100)
44 -t --tags Number of distinct hash tags (default 10000)
45 -u --users Number of users (default 100)
46 -w --words Words file (default '/usr/share/dict/words')
47 -x --prefix User name prefix (default 'testuser')
48 -z --groupprefix Group name prefix (default 'testgroup')
50 END_OF_CREATESIM_HELP;
52 require_once INSTALLDIR.'/scripts/commandline.inc';
54 // XXX: make these command-line options
59 $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
60 'password' => sprintf('password%d', $i),
61 'fullname' => sprintf('Test User %d', $i)));
67 function newGroup($i, $j)
72 // Pick a random user to be the admin
74 $n = rand(0, max($j - 1, 0));
75 $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
77 $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
79 'userid' => $user->id,
80 'fullname' => sprintf('Test Group %d', $i)));
83 function newNotice($i, $tagmax)
87 $options = array('scope' => Notice::defaultScope());
90 $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
92 $is_reply = rand(0, 1);
94 $content = testNoticeContent();
97 $stream = new InboxNoticeStream($user->getProfile(), $user->getProfile());
98 $notices = $stream->getNotices(0, 20);
99 if ($notices->N > 0) {
100 $nval = rand(0, $notices->N - 1);
101 $notices->fetch(); // go to 0th
102 for ($i = 0; $i < $nval; $i++) {
105 $options['reply_to'] = $notices->id;
106 $dont_use_nickname = rand(0, 2);
107 if ($dont_use_nickname != 0) {
108 $rprofile = $notices->getProfile();
109 $content = "@".$rprofile->nickname." ".$content;
111 $private_to_addressees = rand(0, 4);
112 if ($private_to_addressees == 0) {
113 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
117 $is_directed = rand(0, 4);
119 if ($is_directed == 0) {
120 $subs = $user->getSubscribed(0, 100)->fetchAll();
121 if (count($subs) > 0) {
126 } else if ($f <= 8) {
131 for ($m = 0; $m < $addrs; $m++) {
132 $x = rand(0, count($subs) - 1);
136 if ($subs[$x]->id == $user->id) {
140 $rprofile = $subs[$x];
141 $content = "@".$rprofile->nickname." ".$content;
143 $private_to_addressees = rand(0, 4);
144 if ($private_to_addressees == 0) {
145 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
151 $has_hash = rand(0, 2);
153 if ($has_hash == 0) {
154 $hashcount = rand(0, 2);
155 for ($j = 0; $j < $hashcount; $j++) {
156 $h = rand(0, $tagmax);
157 $content .= " #tag{$h}";
161 $in_group = rand(0, 5);
163 if ($in_group == 0) {
164 $groups = $user->getGroups();
165 if ($groups instanceof User_group) {
166 $gval = rand(0, $groups->N - 1);
167 $groups->fetch(); // go to 0th
168 for ($i = 0; $i < $gval; $i++) {
171 $options['groups'] = array($groups->id);
172 $content = "!".$groups->nickname." ".$content;
173 $private_to_group = rand(0, 2);
174 if ($private_to_group == 0) {
175 $options['scope'] |= Notice::GROUP_SCOPE;
180 $private_to_site = rand(0, 4);
182 if ($private_to_site == 0) {
183 $options['scope'] |= Notice::SITE_SCOPE;
186 $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
189 function newMessage($i)
193 $n = rand(0, $i - 1);
194 $user = User::getKV('nickname', sprintf('%s%d', $userprefix, $n));
196 $content = testNoticeContent();
198 $friends = $user->mutuallySubscribedUsers()->fetchAll();
200 if (count($friends) == 0) {
204 $j = rand(0, count($friends) - 1);
206 $other = $friends[$j];
208 $message = Message::saveNew($user->id, $other->id, $content, 'createsim');
214 $f = rand(0, $i - 1);
216 $fromnick = sprintf('%s%d', $userprefix, $f);
218 $from = User::getKV('nickname', $fromnick);
221 throw new Exception("Can't find user '$fromnick'.");
224 $t = rand(0, $i - 1);
233 $tunic = sprintf('%s%d', $userprefix, $t);
235 $to = User::getKV('nickname', $tunic);
237 if (!($to instanceof User)) {
238 throw new Exception("Can't find user '$tunic'.");
241 Subscription::start($from->getProfile(), $to->getProfile());
247 function newJoin($u, $g)
252 $userNumber = rand(0, $u - 1);
254 $userNick = sprintf('%s%d', $userprefix, $userNumber);
256 $user = User::getKV('nickname', $userNick);
259 throw new Exception("Can't find user '$fromnick'.");
262 $groupNumber = rand(0, $g - 1);
264 $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
266 $group = User_group::getKV('nickname', $groupNick);
269 throw new Exception("Can't find group '$groupNick'.");
272 if (!$user->isMember($group)) {
273 $user->joinGroup($group);
277 /* Plugins should be part of the simulation too!
283 $userNumber = rand(0, $u - 1);
285 $userNick = sprintf('%s%d', $userprefix, $userNumber);
287 $user = User::getKV('nickname', $userNick);
290 throw new Exception("Can't find user '$userNick'.");
293 // NB: it's OK to like your own stuff!
295 $otherNumber = rand(0, $u - 1);
297 $otherNick = sprintf('%s%d', $userprefix, $otherNumber);
299 $other = User::getKV('nickname', $otherNick);
302 throw new Exception("Can't find user '$otherNick'.");
305 $notices = $other->getNotices()->fetchAll();
307 if (count($notices) == 0) {
311 $idx = rand(0, count($notices) - 1);
313 $notice = $notices[$idx];
315 if ($user->hasFave($notice)) {
319 Fave::addNew($user->getProfile(), $notice);
322 function testNoticeContent()
326 if (is_null($words)) {
327 return "test notice content";
332 $ids = array_rand($words, $cnt);
334 foreach ($ids as $id) {
335 $parts[] = $words[$id];
338 $text = implode(' ', $parts);
340 if (mb_strlen($text) > 80) {
341 $text = substr($text, 0, 77) . "...";
347 //function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax)
348 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $messageavg, $tagmax)
351 $config['site']['dupelimit'] = -1;
358 $preuser = min($usercount, 5);
360 for ($j = 0; $j < $preuser; $j++) {
361 printfv("$i Creating user $n\n");
366 $pregroup = min($groupcount, 3);
368 for ($k = 0; $k < $pregroup; $k++) {
369 printfv("$i Creating group $g\n");
374 // # registrations + # notices + # subs
376 //$events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $favesavg + $messageavg));
377 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg + $messageavg));
380 $events -= $pregroup;
383 $gt = $ut + $groupcount;
384 $nt = $gt + ($usercount * $noticeavg);
385 $st = $nt + ($usercount * $subsavg);
386 $jt = $st + ($usercount * $joinsavg);
387 // $ft = $jt + ($usercount * $favesavg);
388 $mt = $ft + ($usercount * $messageavg);
390 printfv("$events events ($ut, $gt, $nt, $st, $jt, $ft, $mt)\n");
392 for ($i = 0; $i < $events; $i++)
394 $e = rand(0, $events);
396 if ($e >= 0 && $e <= $ut) {
397 printfv("$i Creating user $n\n");
400 } else if ($e > $ut && $e <= $gt) {
401 printfv("$i Creating group $g\n");
404 } else if ($e > $gt && $e <= $nt) {
405 printfv("$i Making a new notice\n");
406 newNotice($n, $tagmax);
407 } else if ($e > $nt && $e <= $st) {
408 printfv("$i Making a new subscription\n");
410 } else if ($e > $st && $e <= $jt) {
411 printfv("$i Making a new group join\n");
413 /* } else if ($e > $jt && $e <= $ft) {
414 printfv("$i Making a new fave\n");
416 } else if ($e > $ft && $e <= $mt) {
417 printfv("$i Making a new message\n");
420 printfv("No event for $i!");
425 $defaultWordsfile = '/usr/share/dict/words';
427 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
428 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
429 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
430 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
431 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
432 //$favesavg = (have_option('f', 'faves')) ? get_option_value('f', 'faves') : max($noticeavg/10, 5);
433 $messageavg = (have_option('m', 'messages')) ? get_option_value('m', 'messages') : max($noticeavg/10, 5);
434 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
435 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
436 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
437 $wordsfile = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
439 if (is_readable($wordsfile)) {
440 $words = file($wordsfile);
442 if ($wordsfile != $defaultWordsfile) {
443 // user specified words file couldn't be read
444 throw new Exception("Couldn't read words file: {$wordsfile}.");
450 //main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $favesavg, $messageavg, $tagmax);
451 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $messageavg, $tagmax);
452 } catch (Exception $e) {
453 printfv("Got an exception: ".$e->getMessage());