]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
Merge branch '1.0.x' into limitdist2
[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('scope' => common_config('notice', 'defaultscope'));
75
76     $n = rand(0, $i - 1);
77     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
78
79     $is_reply = rand(0, 1);
80
81     $content = 'Test notice content';
82
83     if ($is_reply == 0) {
84         common_set_user($user);
85         $notices = $user->noticesWithFriends(0, 20);
86         if ($notices->N > 0) {
87             $nval = rand(0, $notices->N - 1);
88             $notices->fetch(); // go to 0th
89             for ($i = 0; $i < $nval; $i++) {
90                 $notices->fetch();
91             }
92             $options['reply_to'] = $notices->id;
93             $dont_use_nickname = rand(0, 2);
94             if ($dont_use_nickname != 0) {
95                 $rprofile = $notices->getProfile();
96                 $content = "@".$rprofile->nickname." ".$content;
97             }
98             $private_to_addressees = rand(0, 4);
99             if ($private_to_addressees == 0) {
100                 $options['scope'] |= Notice::ADDRESSEE_SCOPE;
101             }
102         }
103     }
104
105     $has_hash = rand(0, 2);
106
107     if ($has_hash == 0) {
108         $hashcount = rand(0, 2);
109         for ($j = 0; $j < $hashcount; $j++) {
110             $h = rand(0, $tagmax);
111             $content .= " #tag{$h}";
112         }
113     }
114
115     $in_group = rand(0, 5);
116
117     if ($in_group == 0) {
118         $groups = $user->getGroups();
119         if ($groups->N > 0) {
120             $gval = rand(0, $group->N - 1);
121             $groups->fetch(); // go to 0th
122             for ($i = 0; $i < $gval; $i++) {
123                 $groups->fetch();
124             }
125             $options['groups'] = array($groups->id);
126             $content = "!".$groups->nickname." ".$content;
127             $private_to_group = rand(0, 2);
128             if ($private_to_group == 0) {
129                 $options['scope'] |= Notice::GROUP_SCOPE;
130             }
131         }
132     }
133
134     $private_to_site = rand(0, 4);
135
136     if ($private_to_site == 0) {
137         $options['scope'] |= Notice::SITE_SCOPE;
138     }
139
140     $notice = Notice::saveNew($user->id, $content, 'system', $options);
141 }
142
143 function newSub($i)
144 {
145     global $userprefix;
146     $f = rand(0, $i - 1);
147
148     $fromnick = sprintf('%s%d', $userprefix, $f);
149
150     $from = User::staticGet('nickname', $fromnick);
151
152     if (empty($from)) {
153         throw new Exception("Can't find user '$fromnick'.");
154     }
155
156     $t = rand(0, $i - 1);
157
158     if ($t == $f) {
159         $t++;
160         if ($t > $i - 1) {
161             $t = 0;
162         }
163     }
164
165     $tunic = sprintf('%s%d', $userprefix, $t);
166
167     $to = User::staticGet('nickname', $tunic);
168
169     if (empty($to)) {
170         throw new Exception("Can't find user '$tunic'.");
171     }
172
173     subs_subscribe_to($from, $to);
174
175     $from->free();
176     $to->free();
177 }
178
179 function newJoin($u, $g)
180 {
181     global $userprefix;
182     global $groupprefix;
183
184     $userNumber = rand(0, $u - 1);
185
186     $userNick = sprintf('%s%d', $userprefix, $userNumber);
187
188     $user = User::staticGet('nickname', $userNick);
189
190     if (empty($user)) {
191         throw new Exception("Can't find user '$fromnick'.");
192     }
193
194     $groupNumber = rand(0, $g - 1);
195
196     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
197
198     $group = User_group::staticGet('nickname', $groupNick);
199
200     if (empty($group)) {
201         throw new Exception("Can't find group '$groupNick'.");
202     }
203
204     if (!$user->isMember($group)) {
205         $user->joinGroup($group);
206     }
207 }
208
209 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
210 {
211     global $config;
212     $config['site']['dupelimit'] = -1;
213
214     $n = 1;
215     $g = 1;
216
217     newUser(0);
218     newGroup(0, $n);
219
220     // # registrations + # notices + # subs
221
222     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
223
224     $ut = $usercount;
225     $gt = $ut + $groupcount;
226     $nt = $gt + ($usercount * $noticeavg);
227     $st = $nt + ($usercount * $subsavg);
228     $jt = $st + ($usercount * $joinsavg);
229
230     printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
231
232     for ($i = 0; $i < $events; $i++)
233     {
234         $e = rand(0, $events);
235
236         if ($e >= 0 && $e <= $ut) {
237             printfv("$i Creating user $n\n");
238             newUser($n);
239             $n++;
240         } else if ($e > $ut && $e <= $gt) {
241             printfv("$i Creating group $g\n");
242             newGroup($g, $n);
243             $g++;
244         } else if ($e > $gt && $e <= $nt) {
245             printfv("$i Making a new notice\n");
246             newNotice($n, $tagmax);
247         } else if ($e > $nt && $e <= $st) {
248             printfv("$i Making a new subscription\n");
249             newSub($n);
250         } else if ($e > $st && $e <= $jt) {
251             printfv("$i Making a new group join\n");
252             newJoin($n, $g);
253         } else {
254             printfv("No event for $i!");
255         }
256     }
257 }
258
259 $usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
260 $groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
261 $noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
262 $subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
263 $joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
264 $tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
265 $userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
266 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
267
268 try {
269     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
270 } catch (Exception $e) {
271     printfv("Got an exception: ".$e->getMessage());
272 }