]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
All groups start with a random admin
[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
37 END_OF_CREATESIM_HELP;
38
39 require_once INSTALLDIR.'/scripts/commandline.inc';
40
41 // XXX: make these command-line options
42
43 function newUser($i)
44 {
45     global $userprefix;
46     $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
47                                  'password' => sprintf('password%d', $i),
48                                  'fullname' => sprintf('Test User %d', $i)));
49     if (!empty($user)) {
50         $user->free();
51     }
52 }
53
54 function newGroup($i, $j)
55 {
56     global $groupprefix;
57     global $userprefix;
58
59     // Pick a random user to be the admin
60
61     $n = rand(0, max($j - 1, 0));
62     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
63
64     $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
65                                         'local'    => true,
66                                         'userid'   => $user->id,
67                                         'fullname' => sprintf('Test Group %d', $i)));
68 }
69
70 function newNotice($i, $tagmax)
71 {
72     global $userprefix;
73
74     $options = array();
75
76     $n = rand(0, $i - 1);
77     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
78
79     $is_reply = rand(0, 4);
80
81     $content = 'Test notice content';
82
83     if ($is_reply == 0) {
84         $r = rand(0, max($i - 1, 0));
85         $rnick = sprintf('%s%d', $userprefix, $r);
86         $ruser = User::staticGet('nickname', $rnick);
87         common_set_user($user);
88         $notices = $ruser->getNotices(0, 5);
89         if ($notices->N > 0) {
90             $nval = rand(0, $notices->N);
91             $notices->fetch(); // go to 0th
92             for ($i = 0; $i < $nval; $i++) {
93                 $notices->fetch();
94             }
95             $options['reply_to'] = $notices->id;
96         }
97         $content = "@$rnick " . $content;
98     }
99
100     $has_hash = rand(0, 2);
101
102     if ($has_hash == 0) {
103         $hashcount = rand(0, 2);
104         for ($j = 0; $j < $hashcount; $j++) {
105             $h = rand(0, $tagmax);
106             $content .= " #tag{$h}";
107         }
108     }
109
110     $in_group = rand(0, 5);
111
112     if ($in_group == 0) {
113         $groups = $user->getGroups();
114         if ($groups->N > 0) {
115             $gval = rand(0, $group->N);
116             $groups->fetch(); // go to 0th
117             for ($i = 0; $i < $gval; $i++) {
118                 $groups->fetch();
119             }
120             $options['groups'] = array($groups->id);
121             $content = "!".$groups->nickname." ".$content;
122         }
123     }
124
125     $notice = Notice::saveNew($user->id, $content, 'system', $options);
126 }
127
128 function newSub($i)
129 {
130     global $userprefix;
131     $f = rand(0, $i - 1);
132
133     $fromnick = sprintf('%s%d', $userprefix, $f);
134
135     $from = User::staticGet('nickname', $fromnick);
136
137     if (empty($from)) {
138         throw new Exception("Can't find user '$fromnick'.");
139     }
140
141     $t = rand(0, $i - 1);
142
143     if ($t == $f) {
144         $t++;
145         if ($t > $i - 1) {
146             $t = 0;
147         }
148     }
149
150     $tunic = sprintf('%s%d', $userprefix, $t);
151
152     $to = User::staticGet('nickname', $tunic);
153
154     if (empty($to)) {
155         throw new Exception("Can't find user '$tunic'.");
156     }
157
158     subs_subscribe_to($from, $to);
159
160     $from->free();
161     $to->free();
162 }
163
164 function newJoin($u, $g)
165 {
166     global $userprefix;
167     global $groupprefix;
168
169     $userNumber = rand(0, $u - 1);
170
171     $userNick = sprintf('%s%d', $userprefix, $userNumber);
172
173     $user = User::staticGet('nickname', $userNick);
174
175     if (empty($user)) {
176         throw new Exception("Can't find user '$fromnick'.");
177     }
178
179     $groupNumber = rand(0, $g - 1);
180
181     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
182
183     $group = User_group::staticGet('nickname', $groupNick);
184
185     if (empty($group)) {
186         throw new Exception("Can't find group '$groupNick'.");
187     }
188
189     if (!$user->isMember($group)) {
190         $user->joinGroup($group);
191     }
192 }
193
194 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
195 {
196     global $config;
197     $config['site']['dupelimit'] = -1;
198
199     $n = 1;
200     $g = 1;
201
202     newUser(0);
203     newGroup(0, $n);
204
205     // # registrations + # notices + # subs
206
207     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
208
209     $ut = $usercount;
210     $gt = $ut + $groupcount;
211     $nt = $gt + ($usercount * $noticeavg);
212     $st = $nt + ($usercount * $subsavg);
213     $jt = $st + ($usercount * $joinsavg);
214
215     printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
216
217     for ($i = 0; $i < $events; $i++)
218     {
219         $e = rand(0, $events);
220
221         if ($e >= 0 && $e <= $ut) {
222             printfv("$i Creating user $n\n");
223             newUser($n);
224             $n++;
225         } else if ($e > $ut && $e <= $gt) {
226             printfv("$i Creating group $g\n");
227             newGroup($g, $n);
228             $g++;
229         } else if ($e > $gt && $e <= $nt) {
230             printfv("$i Making a new notice\n");
231             newNotice($n, $tagmax);
232         } else if ($e > $nt && $e <= $st) {
233             printfv("$i Making a new subscription\n");
234             newSub($n);
235         } else if ($e > $st && $e <= $jt) {
236             printfv("$i Making a new group join\n");
237             newJoin($n, $g);
238         } else {
239             printfv("No event for $i!");
240         }
241     }
242 }
243
244 $usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
245 $groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
246 $noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
247 $subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
248 $joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
249 $tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
250 $userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
251 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
252
253 try {
254     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
255 } catch (Exception $e) {
256     printfv("Got an exception: ".$e->getMessage());
257 }