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