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')
36 -w --words Words file (default '/usr/share/dict/words')
38 END_OF_CREATESIM_HELP;
40 require_once INSTALLDIR.'/scripts/commandline.inc';
42 // XXX: make these command-line options
47 $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
48 'password' => sprintf('password%d', $i),
49 'fullname' => sprintf('Test User %d', $i)));
55 function newGroup($i, $j)
60 // Pick a random user to be the admin
62 $n = rand(0, max($j - 1, 0));
63 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
65 $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
67 'userid' => $user->id,
68 'fullname' => sprintf('Test Group %d', $i)));
71 function newNotice($i, $tagmax)
75 $options = array('scope' => common_config('notice', 'defaultscope'));
78 $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
80 $is_reply = rand(0, 1);
82 $content = testNoticeContent();
85 $stream = new InboxNoticeStream($user, $user->getProfile());
86 $notices = $stream->getNotices(0, 20);
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 testNoticeContent()
214 if (is_null($words)) {
215 return "test notice content";
220 $ids = array_rand($words, $cnt);
222 foreach ($ids as $id) {
223 $parts[] = $words[$id];
226 $text = implode(' ', $parts);
228 if (mb_strlen($text) > 80) {
229 $text = substr($text, 0, 77) . "...";
235 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
238 $config['site']['dupelimit'] = -1;
245 $preuser = min($usercount, 5);
247 for ($j = 0; $j < $preuser; $j++) {
248 printfv("$i Creating user $n\n");
253 $pregroup = min($groupcount, 3);
255 for ($k = 0; $k < $pregroup; $k++) {
256 printfv("$i Creating group $g\n");
261 // # registrations + # notices + # subs
263 $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
266 $events -= $pregroup;
269 $gt = $ut + $groupcount;
270 $nt = $gt + ($usercount * $noticeavg);
271 $st = $nt + ($usercount * $subsavg);
272 $jt = $st + ($usercount * $joinsavg);
274 printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
276 for ($i = 0; $i < $events; $i++)
278 $e = rand(0, $events);
280 if ($e >= 0 && $e <= $ut) {
281 printfv("$i Creating user $n\n");
284 } else if ($e > $ut && $e <= $gt) {
285 printfv("$i Creating group $g\n");
288 } else if ($e > $gt && $e <= $nt) {
289 printfv("$i Making a new notice\n");
290 newNotice($n, $tagmax);
291 } else if ($e > $nt && $e <= $st) {
292 printfv("$i Making a new subscription\n");
294 } else if ($e > $st && $e <= $jt) {
295 printfv("$i Making a new group join\n");
298 printfv("No event for $i!");
303 $usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
304 $groupcount = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
305 $noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
306 $subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
307 $joinsavg = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
308 $tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
309 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
310 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
311 $wordsfile = (have_option('w', 'words')) ? get_option_value('w', 'words') : '/usr/share/dict/words';
313 if (is_readable($wordsfile)) {
314 $words = file($wordsfile);
320 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
321 } catch (Exception $e) {
322 printfv("Got an exception: ".$e->getMessage());