]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
Merge branch '1.0.x' into prefillbookmark
[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 = 'u:n:b:g:j:t:x:z:';
24 $longoptions = array('users=', 'notices=', 'subscriptions=', 'groups=', 'joins=', 'tags=', 'prefix=');
25
26 $helptext = <<<END_OF_CREATESIM_HELP
27 Creates a lot of test users and notices to (loosely) simulate a real server.
28
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')
37
38 END_OF_CREATESIM_HELP;
39
40 require_once INSTALLDIR.'/scripts/commandline.inc';
41
42 // XXX: make these command-line options
43
44 function newUser($i)
45 {
46     global $userprefix;
47     $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
48                                  'password' => sprintf('password%d', $i),
49                                  'fullname' => sprintf('Test User %d', $i)));
50     if (!empty($user)) {
51         $user->free();
52     }
53 }
54
55 function newGroup($i, $j)
56 {
57     global $groupprefix;
58     global $userprefix;
59
60     // Pick a random user to be the admin
61
62     $n = rand(0, max($j - 1, 0));
63     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
64
65     $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
66                                         'local'    => true,
67                                         'userid'   => $user->id,
68                                         'fullname' => sprintf('Test Group %d', $i)));
69 }
70
71 function newNotice($i, $tagmax)
72 {
73     global $userprefix;
74
75     $options = array('scope' => common_config('notice', 'defaultscope'));
76
77     $n = rand(0, $i - 1);
78     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
79
80     $is_reply = rand(0, 1);
81
82     $content = testNoticeContent();
83
84     if ($is_reply == 0) {
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++) {
91                 $notices->fetch();
92             }
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;
98             }
99             $private_to_addressees = rand(0, 4);
100             if ($private_to_addressees == 0) {
101                 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
102             }
103         }
104     }
105
106     $has_hash = rand(0, 2);
107
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}";
113         }
114     }
115
116     $in_group = rand(0, 5);
117
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++) {
124                 $groups->fetch();
125             }
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;
131             }
132         }
133     }
134
135     $private_to_site = rand(0, 4);
136
137     if ($private_to_site == 0) {
138         $options['scope'] |= Notice::SITE_SCOPE;
139     }
140
141     $notice = Notice::saveNew($user->id, $content, 'system', $options);
142 }
143
144 function newSub($i)
145 {
146     global $userprefix;
147     $f = rand(0, $i - 1);
148
149     $fromnick = sprintf('%s%d', $userprefix, $f);
150
151     $from = User::staticGet('nickname', $fromnick);
152
153     if (empty($from)) {
154         throw new Exception("Can't find user '$fromnick'.");
155     }
156
157     $t = rand(0, $i - 1);
158
159     if ($t == $f) {
160         $t++;
161         if ($t > $i - 1) {
162             $t = 0;
163         }
164     }
165
166     $tunic = sprintf('%s%d', $userprefix, $t);
167
168     $to = User::staticGet('nickname', $tunic);
169
170     if (empty($to)) {
171         throw new Exception("Can't find user '$tunic'.");
172     }
173
174     subs_subscribe_to($from, $to);
175
176     $from->free();
177     $to->free();
178 }
179
180 function newJoin($u, $g)
181 {
182     global $userprefix;
183     global $groupprefix;
184
185     $userNumber = rand(0, $u - 1);
186
187     $userNick = sprintf('%s%d', $userprefix, $userNumber);
188
189     $user = User::staticGet('nickname', $userNick);
190
191     if (empty($user)) {
192         throw new Exception("Can't find user '$fromnick'.");
193     }
194
195     $groupNumber = rand(0, $g - 1);
196
197     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
198
199     $group = User_group::staticGet('nickname', $groupNick);
200
201     if (empty($group)) {
202         throw new Exception("Can't find group '$groupNick'.");
203     }
204
205     if (!$user->isMember($group)) {
206         $user->joinGroup($group);
207     }
208 }
209
210 function testNoticeContent()
211 {
212     global $words;
213     
214     if (is_null($words)) {
215         return "test notice content";
216     }
217
218     $cnt = rand(3, 8);
219
220     $ids = array_rand($words, $cnt);
221
222     foreach ($ids as $id) {
223         $parts[] = $words[$id];
224     }
225
226     $text = implode(' ', $parts);
227     
228     if (mb_strlen($text) > 80) {
229         $text = substr($text, 0, 77) . "...";
230     }
231     
232     return $text;
233 }
234
235 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
236 {
237     global $config;
238     $config['site']['dupelimit'] = -1;
239
240     $n = 0;
241     $g = 0;
242
243     // Make users first
244
245     $preuser = min($usercount, 5);
246
247     for ($j = 0; $j < $preuser; $j++) {
248         printfv("$i Creating user $n\n");
249         newUser($n);
250         $n++;
251     }
252
253     $pregroup = min($groupcount, 3);
254
255     for ($k = 0; $k < $pregroup; $k++) {
256         printfv("$i Creating group $g\n");
257         newGroup($g, $n);
258         $g++;
259     }
260
261     // # registrations + # notices + # subs
262
263     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
264
265     $events -= $preuser;
266     $events -= $pregroup;
267
268     $ut = $usercount;
269     $gt = $ut + $groupcount;
270     $nt = $gt + ($usercount * $noticeavg);
271     $st = $nt + ($usercount * $subsavg);
272     $jt = $st + ($usercount * $joinsavg);
273
274     printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
275
276     for ($i = 0; $i < $events; $i++)
277     {
278         $e = rand(0, $events);
279
280         if ($e >= 0 && $e <= $ut) {
281             printfv("$i Creating user $n\n");
282             newUser($n);
283             $n++;
284         } else if ($e > $ut && $e <= $gt) {
285             printfv("$i Creating group $g\n");
286             newGroup($g, $n);
287             $g++;
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");
293             newSub($n);
294         } else if ($e > $st && $e <= $jt) {
295             printfv("$i Making a new group join\n");
296             newJoin($n, $g);
297         } else {
298             printfv("No event for $i!");
299         }
300     }
301 }
302
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';
312
313 if (is_readable($wordsfile)) {
314     $words = file($wordsfile);
315 } else {
316     $words = null;
317 }
318
319 try {
320     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
321 } catch (Exception $e) {
322     printfv("Got an exception: ".$e->getMessage());
323 }