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)
74 $options = array('scope' => common_config('notice', 'defaultscope'));
77 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
79 $is_reply = rand(0, 1);
81 $content = 'Test notice content';
84 common_set_user($user);
85 $stream = new InboxNoticeStream($user);
86 $notices = $stream->getNotices(0, 20, null, null);
87 if ($notices->N > 0) {
88 $nval = rand(0, $notices->N - 1);
89 $notices->fetch(); // go to 0th
90 for ($i = 0; $i < $nval; $i++) {
93 $options['reply_to'] = $notices->id;
94 $dont_use_nickname = rand(0, 2);
95 if ($dont_use_nickname != 0) {
96 $rprofile = $notices->getProfile();
97 $content = "@".$rprofile->nickname." ".$content;
99 $private_to_addressees = rand(0, 4);
100 if ($private_to_addressees == 0) {
101 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
106 $has_hash = rand(0, 2);
108 if ($has_hash == 0) {
109 $hashcount = rand(0, 2);
110 for ($j = 0; $j < $hashcount; $j++) {
111 $h = rand(0, $tagmax);
112 $content .= " #tag{$h}";
116 $in_group = rand(0, 5);
118 if ($in_group == 0) {
119 $groups = $user->getGroups();
120 if ($groups->N > 0) {
121 $gval = rand(0, $group->N - 1);
122 $groups->fetch(); // go to 0th
123 for ($i = 0; $i < $gval; $i++) {
126 $options['groups'] = array($groups->id);
127 $content = "!".$groups->nickname." ".$content;
128 $private_to_group = rand(0, 2);
129 if ($private_to_group == 0) {
130 $options['scope'] |= Notice::GROUP_SCOPE;
135 $private_to_site = rand(0, 4);
137 if ($private_to_site == 0) {
138 $options['scope'] |= Notice::SITE_SCOPE;
141 $notice = Notice::saveNew($user->id, $content, 'system', $options);
147 $f = rand(0, $i - 1);
149 $fromnick = sprintf('%s%d', $userprefix, $f);
151 $from = User::staticGet('nickname', $fromnick);
154 throw new Exception("Can't find user '$fromnick'.");
157 $t = rand(0, $i - 1);
166 $tunic = sprintf('%s%d', $userprefix, $t);
168 $to = User::staticGet('nickname', $tunic);
171 throw new Exception("Can't find user '$tunic'.");
174 subs_subscribe_to($from, $to);
180 function newJoin($u, $g)
185 $userNumber = rand(0, $u - 1);
187 $userNick = sprintf('%s%d', $userprefix, $userNumber);
189 $user = User::staticGet('nickname', $userNick);
192 throw new Exception("Can't find user '$fromnick'.");
195 $groupNumber = rand(0, $g - 1);
197 $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
199 $group = User_group::staticGet('nickname', $groupNick);
202 throw new Exception("Can't find group '$groupNick'.");
205 if (!$user->isMember($group)) {
206 $user->joinGroup($group);
210 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
213 $config['site']['dupelimit'] = -1;
221 // # registrations + # notices + # subs
223 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
226 $gt = $ut + $groupcount;
227 $nt = $gt + ($usercount * $noticeavg);
228 $st = $nt + ($usercount * $subsavg);
229 $jt = $st + ($usercount * $joinsavg);
231 printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
233 for ($i = 0; $i < $events; $i++)
235 $e = rand(0, $events);
237 if ($e >= 0 && $e <= $ut) {
238 printfv("$i Creating user $n\n");
241 } else if ($e > $ut && $e <= $gt) {
242 printfv("$i Creating group $g\n");
245 } else if ($e > $gt && $e <= $nt) {
246 printfv("$i Making a new notice\n");
247 newNotice($n, $tagmax);
248 } else if ($e > $nt && $e <= $st) {
249 printfv("$i Making a new subscription\n");
251 } else if ($e > $st && $e <= $jt) {
252 printfv("$i Making a new group join\n");
255 printfv("No event for $i!");
260 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
261 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
262 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
263 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
264 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
265 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
266 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
267 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
270 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
271 } catch (Exception $e) {
272 printfv("Got an exception: ".$e->getMessage());