]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
Merge commit 'refs/merge-requests/165' of git://gitorious.org/statusnet/mainline...
[quix0rs-gnu-social.git] / scripts / createsim.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23 $shortoptions = 'b:g:j:n:t:u:w:x:z:';
24 $longoptions = array(
25     'subscriptions=',
26     'groups=',
27     'joins=',
28     'notices=',
29     'tags=',
30     'users=',
31     'words=',
32     'prefix=',
33     'groupprefix'
34 );
35
36 $helptext = <<<END_OF_CREATESIM_HELP
37 Creates a lot of test users and notices to (loosely) simulate a real server.
38
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')
48
49 END_OF_CREATESIM_HELP;
50
51 require_once INSTALLDIR.'/scripts/commandline.inc';
52
53 // XXX: make these command-line options
54
55 function newUser($i)
56 {
57     global $userprefix;
58     $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
59                                  'password' => sprintf('password%d', $i),
60                                  'fullname' => sprintf('Test User %d', $i)));
61     if (!empty($user)) {
62         $user->free();
63     }
64 }
65
66 function newGroup($i, $j)
67 {
68     global $groupprefix;
69     global $userprefix;
70
71     // Pick a random user to be the admin
72
73     $n = rand(0, max($j - 1, 0));
74     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
75
76     $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
77                                         'local'    => true,
78                                         'userid'   => $user->id,
79                                         'fullname' => sprintf('Test Group %d', $i)));
80 }
81
82 function newNotice($i, $tagmax)
83 {
84     global $userprefix;
85
86     $options = array('scope' => Notice::defaultScope());
87
88     $n = rand(0, $i - 1);
89     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
90
91     $is_reply = rand(0, 1);
92
93     $content = testNoticeContent();
94
95     if ($is_reply == 0) {
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++) {
102                 $notices->fetch();
103             }
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;
109             }
110             $private_to_addressees = rand(0, 4);
111             if ($private_to_addressees == 0) {
112                 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
113             }
114         }
115     }
116
117     $has_hash = rand(0, 2);
118
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}";
124         }
125     }
126
127     $in_group = rand(0, 5);
128
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++) {
135                 $groups->fetch();
136             }
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;
142             }
143         }
144     }
145
146     $private_to_site = rand(0, 4);
147
148     if ($private_to_site == 0) {
149         $options['scope'] |= Notice::SITE_SCOPE;
150     }
151
152     $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
153 }
154
155 function newSub($i)
156 {
157     global $userprefix;
158     $f = rand(0, $i - 1);
159
160     $fromnick = sprintf('%s%d', $userprefix, $f);
161
162     $from = User::staticGet('nickname', $fromnick);
163
164     if (empty($from)) {
165         throw new Exception("Can't find user '$fromnick'.");
166     }
167
168     $t = rand(0, $i - 1);
169
170     if ($t == $f) {
171         $t++;
172         if ($t > $i - 1) {
173             $t = 0;
174         }
175     }
176
177     $tunic = sprintf('%s%d', $userprefix, $t);
178
179     $to = User::staticGet('nickname', $tunic);
180
181     if (empty($to)) {
182         throw new Exception("Can't find user '$tunic'.");
183     }
184
185     subs_subscribe_to($from, $to);
186
187     $from->free();
188     $to->free();
189 }
190
191 function newJoin($u, $g)
192 {
193     global $userprefix;
194     global $groupprefix;
195
196     $userNumber = rand(0, $u - 1);
197
198     $userNick = sprintf('%s%d', $userprefix, $userNumber);
199
200     $user = User::staticGet('nickname', $userNick);
201
202     if (empty($user)) {
203         throw new Exception("Can't find user '$fromnick'.");
204     }
205
206     $groupNumber = rand(0, $g - 1);
207
208     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
209
210     $group = User_group::staticGet('nickname', $groupNick);
211
212     if (empty($group)) {
213         throw new Exception("Can't find group '$groupNick'.");
214     }
215
216     if (!$user->isMember($group)) {
217         $user->joinGroup($group);
218     }
219 }
220
221 function testNoticeContent()
222 {
223     global $words;
224
225     if (is_null($words)) {
226         return "test notice content";
227     }
228
229     $cnt = rand(3, 8);
230
231     $ids = array_rand($words, $cnt);
232
233     foreach ($ids as $id) {
234         $parts[] = $words[$id];
235     }
236
237     $text = implode(' ', $parts);
238
239     if (mb_strlen($text) > 80) {
240         $text = substr($text, 0, 77) . "...";
241     }
242
243     return $text;
244 }
245
246 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
247 {
248     global $config;
249     $config['site']['dupelimit'] = -1;
250
251     $n = 0;
252     $g = 0;
253
254     // Make users first
255
256     $preuser = min($usercount, 5);
257
258     for ($j = 0; $j < $preuser; $j++) {
259         printfv("$i Creating user $n\n");
260         newUser($n);
261         $n++;
262     }
263
264     $pregroup = min($groupcount, 3);
265
266     for ($k = 0; $k < $pregroup; $k++) {
267         printfv("$i Creating group $g\n");
268         newGroup($g, $n);
269         $g++;
270     }
271
272     // # registrations + # notices + # subs
273
274     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
275
276     $events -= $preuser;
277     $events -= $pregroup;
278
279     $ut = $usercount;
280     $gt = $ut + $groupcount;
281     $nt = $gt + ($usercount * $noticeavg);
282     $st = $nt + ($usercount * $subsavg);
283     $jt = $st + ($usercount * $joinsavg);
284
285     printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
286
287     for ($i = 0; $i < $events; $i++)
288     {
289         $e = rand(0, $events);
290
291         if ($e >= 0 && $e <= $ut) {
292             printfv("$i Creating user $n\n");
293             newUser($n);
294             $n++;
295         } else if ($e > $ut && $e <= $gt) {
296             printfv("$i Creating group $g\n");
297             newGroup($g, $n);
298             $g++;
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");
304             newSub($n);
305         } else if ($e > $st && $e <= $jt) {
306             printfv("$i Making a new group join\n");
307             newJoin($n, $g);
308         } else {
309             printfv("No event for $i!");
310         }
311     }
312 }
313
314 $defaultWordsfile = '/usr/share/dict/words';
315
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;
325
326 if (is_readable($wordsfile)) {
327     $words = file($wordsfile);
328 } else {
329    if ($wordsfile != $defaultWordsfile) {
330       // user specified words file couldn't be read
331       throw new Exception("Couldn't read words file: {$wordsfile}.");
332     }
333     $words = null;
334 }
335
336 try {
337     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
338 } catch (Exception $e) {
339     printfv("Got an exception: ".$e->getMessage());
340 }