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)));
58 $user = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
60 'fullname' => sprintf('Test Group %d', $i)));
66 function newNotice($i, $tagmax)
73 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
75 $is_reply = rand(0, 4);
77 $content = 'Test notice content';
80 $r = rand(0, max($i - 1, 0));
81 $rnick = sprintf('%s%d', $userprefix, $r);
82 $ruser = User::staticGet('nickname', $rnick);
83 common_set_user($user);
84 $notices = $ruser->getNotices(0, 5);
85 if ($notices->N > 0) {
86 $nval = rand(0, $notices->N);
87 $notices->fetch(); // go to 0th
88 for ($i = 0; $i < $nval; $i++) {
91 $options['reply_to'] = $notices->id;
93 $content = "@$rnick " . $content;
96 $has_hash = rand(0, 2);
99 $hashcount = rand(0, 2);
100 for ($j = 0; $j < $hashcount; $j++) {
101 $h = rand(0, $tagmax);
102 $content .= " #tag{$h}";
106 $notice = Notice::saveNew($user->id, $content, 'system', $options);
112 $f = rand(0, $i - 1);
114 $fromnick = sprintf('%s%d', $userprefix, $f);
116 $from = User::staticGet('nickname', $fromnick);
119 throw new Exception("Can't find user '$fromnick'.");
122 $t = rand(0, $i - 1);
131 $tunic = sprintf('%s%d', $userprefix, $t);
133 $to = User::staticGet('nickname', $tunic);
136 throw new Exception("Can't find user '$tunic'.");
139 subs_subscribe_to($from, $to);
145 function newJoin($u, $g)
150 $userNumber = rand(0, $u - 1);
152 $userNick = sprintf('%s%d', $userprefix, $userNumber);
154 $user = User::staticGet('nickname', $userNick);
157 throw new Exception("Can't find user '$fromnick'.");
160 $groupNumber = rand(0, $g - 1);
162 $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
164 $group = User_group::staticGet('nickname', $groupNick);
167 throw new Exception("Can't find group '$groupNick'.");
170 if (!$user->isMember($group)) {
171 $user->joinGroup($group);
175 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
178 $config['site']['dupelimit'] = -1;
186 // # registrations + # notices + # subs
188 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
191 $gt = $ut + $groupcount;
192 $nt = $gt + ($usercount * $noticeavg);
193 $st = $nt + ($usercount * $subsavg);
194 $jt = $st + ($usercount * $joinsavg);
196 printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
198 for ($i = 0; $i < $events; $i++)
200 $e = rand(0, $events);
202 if ($e > 0 && $e <= $ut) {
203 printfv("$i Creating user $n\n");
206 } else if ($e > $ut && $e <= $gt) {
207 printfv("$i Creating group $g\n");
210 } else if ($e > $gt && $e <= $nt) {
211 printfv("$i Making a new notice\n");
212 newNotice($n, $tagmax);
213 } else if ($e > $nt && $e <= $st) {
214 printfv("$i Making a new subscription\n");
216 } else if ($e > $st && $e <= $jt) {
217 printfv("$i Making a new group join\n");
220 printfv("No event for $i!");
225 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
226 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
227 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
228 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
229 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
230 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
231 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
232 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
235 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
236 } catch (Exception $e) {
237 printfv("Got an exception: ".$e->getMessage());