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:';
36 $helptext = <<<END_OF_CREATESIM_HELP
37 Creates a lot of test users and notices to (loosely) simulate a real server.
39 -b --subscriptions Average subscriptions per user (default no. users/20)
40 -g --groups Number of groups (default 20)
41 -j --joins Number of groups per user (default 5)
42 -n --notices Average notices per user (default 100)
43 -t --tags Number of distinct hash tags (default 10000)
44 -u --users Number of users (default 100)
45 -w --words Words file (default '/usr/share/dict/words')
46 -x --prefix User name prefix (default 'testuser')
47 -z --groupprefix Group name prefix (default 'testgroup')
49 END_OF_CREATESIM_HELP;
51 require_once INSTALLDIR.'/scripts/commandline.inc';
53 // XXX: make these command-line options
58 $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
59 'password' => sprintf('password%d', $i),
60 'fullname' => sprintf('Test User %d', $i)));
66 function newGroup($i, $j)
71 // Pick a random user to be the admin
73 $n = rand(0, max($j - 1, 0));
74 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
76 $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
78 'userid' => $user->id,
79 'fullname' => sprintf('Test Group %d', $i)));
82 function newNotice($i, $tagmax)
86 $options = array('scope' => Notice::defaultScope());
89 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
91 $is_reply = rand(0, 1);
93 $content = testNoticeContent();
96 $stream = new InboxNoticeStream($user, $user->getProfile());
97 $notices = $stream->getNotices(0, 20);
98 if ($notices->N > 0) {
99 $nval = rand(0, $notices->N - 1);
100 $notices->fetch(); // go to 0th
101 for ($i = 0; $i < $nval; $i++) {
104 $options['reply_to'] = $notices->id;
105 $dont_use_nickname = rand(0, 2);
106 if ($dont_use_nickname != 0) {
107 $rprofile = $notices->getProfile();
108 $content = "@".$rprofile->nickname." ".$content;
110 $private_to_addressees = rand(0, 4);
111 if ($private_to_addressees == 0) {
112 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
117 $has_hash = rand(0, 2);
119 if ($has_hash == 0) {
120 $hashcount = rand(0, 2);
121 for ($j = 0; $j < $hashcount; $j++) {
122 $h = rand(0, $tagmax);
123 $content .= " #tag{$h}";
127 $in_group = rand(0, 5);
129 if ($in_group == 0) {
130 $groups = $user->getGroups();
131 if ($groups->N > 0) {
132 $gval = rand(0, $groups->N - 1);
133 $groups->fetch(); // go to 0th
134 for ($i = 0; $i < $gval; $i++) {
137 $options['groups'] = array($groups->id);
138 $content = "!".$groups->nickname." ".$content;
139 $private_to_group = rand(0, 2);
140 if ($private_to_group == 0) {
141 $options['scope'] |= Notice::GROUP_SCOPE;
146 $private_to_site = rand(0, 4);
148 if ($private_to_site == 0) {
149 $options['scope'] |= Notice::SITE_SCOPE;
152 $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
158 $f = rand(0, $i - 1);
160 $fromnick = sprintf('%s%d', $userprefix, $f);
162 $from = User::staticGet('nickname', $fromnick);
165 throw new Exception("Can't find user '$fromnick'.");
168 $t = rand(0, $i - 1);
177 $tunic = sprintf('%s%d', $userprefix, $t);
179 $to = User::staticGet('nickname', $tunic);
182 throw new Exception("Can't find user '$tunic'.");
185 subs_subscribe_to($from, $to);
191 function newJoin($u, $g)
196 $userNumber = rand(0, $u - 1);
198 $userNick = sprintf('%s%d', $userprefix, $userNumber);
200 $user = User::staticGet('nickname', $userNick);
203 throw new Exception("Can't find user '$fromnick'.");
206 $groupNumber = rand(0, $g - 1);
208 $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
210 $group = User_group::staticGet('nickname', $groupNick);
213 throw new Exception("Can't find group '$groupNick'.");
216 if (!$user->isMember($group)) {
217 $user->joinGroup($group);
221 function testNoticeContent()
225 if (is_null($words)) {
226 return "test notice content";
231 $ids = array_rand($words, $cnt);
233 foreach ($ids as $id) {
234 $parts[] = $words[$id];
237 $text = implode(' ', $parts);
239 if (mb_strlen($text) > 80) {
240 $text = substr($text, 0, 77) . "...";
246 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
249 $config['site']['dupelimit'] = -1;
256 $preuser = min($usercount, 5);
258 for ($j = 0; $j < $preuser; $j++) {
259 printfv("$i Creating user $n\n");
264 $pregroup = min($groupcount, 3);
266 for ($k = 0; $k < $pregroup; $k++) {
267 printfv("$i Creating group $g\n");
272 // # registrations + # notices + # subs
274 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
277 $events -= $pregroup;
280 $gt = $ut + $groupcount;
281 $nt = $gt + ($usercount * $noticeavg);
282 $st = $nt + ($usercount * $subsavg);
283 $jt = $st + ($usercount * $joinsavg);
285 printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
287 for ($i = 0; $i < $events; $i++)
289 $e = rand(0, $events);
291 if ($e >= 0 && $e <= $ut) {
292 printfv("$i Creating user $n\n");
295 } else if ($e > $ut && $e <= $gt) {
296 printfv("$i Creating group $g\n");
299 } else if ($e > $gt && $e <= $nt) {
300 printfv("$i Making a new notice\n");
301 newNotice($n, $tagmax);
302 } else if ($e > $nt && $e <= $st) {
303 printfv("$i Making a new subscription\n");
305 } else if ($e > $st && $e <= $jt) {
306 printfv("$i Making a new group join\n");
309 printfv("No event for $i!");
314 $defaultWordsfile = '/usr/share/dict/words';
316 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
317 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
318 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
319 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
320 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
321 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
322 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
323 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
324 $wordsfile = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
326 if (is_readable($wordsfile)) {
327 $words = file($wordsfile);
329 if ($wordsfile != $defaultWordsfile) {
330 // user specified words file couldn't be read
331 throw new Exception("Couldn't read words file: {$wordsfile}.");
337 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
338 } catch (Exception $e) {
339 printfv("Got an exception: ".$e->getMessage());