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 = 'u:n:b:g:j:t:x:z:';
24 $longoptions = array('users=', 'notices=', 'subscriptions=', 'groups=', 'joins=', 'tags=', 'prefix=');
26 $helptext = <<<END_OF_CREATESIM_HELP
27 Creates a lot of test users and notices to (loosely) simulate a real server.
29 -u --users Number of users (default 100)
30 -n --notices Average notices per user (default 100)
31 -b --subscriptions Average subscriptions per user (default no. users/20)
32 -g --groups Number of groups (default 20)
33 -j --joins Number of groups per user (default 5)
34 -t --tags Number of distinct hash tags (default 10000)
35 -x --prefix User name prefix (default 'testuser')
37 END_OF_CREATESIM_HELP;
39 require_once INSTALLDIR.'/scripts/commandline.inc';
41 // XXX: make these command-line options
46 $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
47 'password' => sprintf('password%d', $i),
48 'fullname' => sprintf('Test User %d', $i)));
54 function newGroup($i, $j)
59 // Pick a random user to be the admin
61 $n = rand(0, max($j - 1, 0));
62 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
64 $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
66 'userid' => $user->id,
67 'fullname' => sprintf('Test Group %d', $i)));
70 function newNotice($i, $tagmax)
77 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
79 $is_reply = rand(0, 4);
81 $content = 'Test notice content';
84 $r = rand(0, max($i - 1, 0));
85 $rnick = sprintf('%s%d', $userprefix, $r);
86 $ruser = User::staticGet('nickname', $rnick);
87 common_set_user($user);
88 $notices = $ruser->getNotices(0, 5);
89 if ($notices->N > 0) {
90 $nval = rand(0, $notices->N);
91 $notices->fetch(); // go to 0th
92 for ($i = 0; $i < $nval; $i++) {
95 $options['reply_to'] = $notices->id;
97 $content = "@$rnick " . $content;
100 $has_hash = rand(0, 2);
102 if ($has_hash == 0) {
103 $hashcount = rand(0, 2);
104 for ($j = 0; $j < $hashcount; $j++) {
105 $h = rand(0, $tagmax);
106 $content .= " #tag{$h}";
110 $in_group = rand(0, 5);
112 if ($in_group == 0) {
113 $groups = $user->getGroups();
114 if ($groups->N > 0) {
115 $gval = rand(0, $group->N);
116 $groups->fetch(); // go to 0th
117 for ($i = 0; $i < $gval; $i++) {
120 $options['groups'] = array($groups->id);
121 $content = "!".$groups->nickname." ".$content;
125 $notice = Notice::saveNew($user->id, $content, 'system', $options);
131 $f = rand(0, $i - 1);
133 $fromnick = sprintf('%s%d', $userprefix, $f);
135 $from = User::staticGet('nickname', $fromnick);
138 throw new Exception("Can't find user '$fromnick'.");
141 $t = rand(0, $i - 1);
150 $tunic = sprintf('%s%d', $userprefix, $t);
152 $to = User::staticGet('nickname', $tunic);
155 throw new Exception("Can't find user '$tunic'.");
158 subs_subscribe_to($from, $to);
164 function newJoin($u, $g)
169 $userNumber = rand(0, $u - 1);
171 $userNick = sprintf('%s%d', $userprefix, $userNumber);
173 $user = User::staticGet('nickname', $userNick);
176 throw new Exception("Can't find user '$fromnick'.");
179 $groupNumber = rand(0, $g - 1);
181 $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
183 $group = User_group::staticGet('nickname', $groupNick);
186 throw new Exception("Can't find group '$groupNick'.");
189 if (!$user->isMember($group)) {
190 $user->joinGroup($group);
194 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
197 $config['site']['dupelimit'] = -1;
205 // # registrations + # notices + # subs
207 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
210 $gt = $ut + $groupcount;
211 $nt = $gt + ($usercount * $noticeavg);
212 $st = $nt + ($usercount * $subsavg);
213 $jt = $st + ($usercount * $joinsavg);
215 printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
217 for ($i = 0; $i < $events; $i++)
219 $e = rand(0, $events);
221 if ($e >= 0 && $e <= $ut) {
222 printfv("$i Creating user $n\n");
225 } else if ($e > $ut && $e <= $gt) {
226 printfv("$i Creating group $g\n");
229 } else if ($e > $gt && $e <= $nt) {
230 printfv("$i Making a new notice\n");
231 newNotice($n, $tagmax);
232 } else if ($e > $nt && $e <= $st) {
233 printfv("$i Making a new subscription\n");
235 } else if ($e > $st && $e <= $jt) {
236 printfv("$i Making a new group join\n");
239 printfv("No event for $i!");
244 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
245 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
246 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
247 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
248 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
249 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
250 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
251 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
254 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
255 } catch (Exception $e) {
256 printfv("Got an exception: ".$e->getMessage());