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