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