]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
add groups and joins 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     $n = rand(0, $i - 1);
71     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
72
73     $is_reply = rand(0, 4);
74
75     $content = 'Test notice content';
76
77     if ($is_reply == 0) {
78         $n = rand(0, $i - 1);
79         $content = "@$userprefix$n " . $content;
80     }
81
82     $has_hash = rand(0, 2);
83
84     if ($has_hash == 0) {
85         $hashcount = rand(0, 2);
86         for ($j = 0; $j < $hashcount; $j++) {
87             $h = rand(0, $tagmax);
88             $content .= " #tag{$h}";
89         }
90     }
91
92     $notice = Notice::saveNew($user->id, $content, 'system');
93
94     $user->free();
95     $notice->free();
96 }
97
98 function newSub($i)
99 {
100     global $userprefix;
101     $f = rand(0, $i - 1);
102
103     $fromnick = sprintf('%s%d', $userprefix, $f);
104
105     $from = User::staticGet('nickname', $fromnick);
106
107     if (empty($from)) {
108         throw new Exception("Can't find user '$fromnick'.");
109     }
110
111     $t = rand(0, $i - 1);
112
113     if ($t == $f) {
114         $t++;
115         if ($t > $i - 1) {
116             $t = 0;
117         }
118     }
119
120     $tunic = sprintf('%s%d', $userprefix, $t);
121
122     $to = User::staticGet('nickname', $tunic);
123
124     if (empty($to)) {
125         throw new Exception("Can't find user '$tunic'.");
126     }
127
128     subs_subscribe_to($from, $to);
129
130     $from->free();
131     $to->free();
132 }
133
134 function newJoin($u, $g)
135 {
136     global $userprefix;
137     global $groupprefix;
138
139     $userNumber = rand(0, $u - 1);
140
141     $userNick = sprintf('%s%d', $userprefix, $userNumber);
142
143     $user = User::staticGet('nickname', $userNick);
144
145     if (empty($user)) {
146         throw new Exception("Can't find user '$fromnick'.");
147     }
148
149     $groupNumber = rand(0, $g - 1);
150
151     $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);
152
153     $group = User_group::staticGet('nickname', $groupNick);
154
155     if (empty($group)) {
156         throw new Exception("Can't find group '$groupNick'.");
157     }
158
159     if (!$user->isMember($group)) {
160         $user->joinGroup($group);
161     }
162 }
163
164 function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
165 {
166     global $config;
167     $config['site']['dupelimit'] = -1;
168
169     $n = 1;
170     $g = 1;
171
172     newUser(0);
173     newGroup(0);
174
175     // # registrations + # notices + # subs
176
177     $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
178
179     $ut = $usercount;
180     $gt = $ut + $groupcount;
181     $nt = $gt + ($usercount * $noticeavg);
182     $st = $nt + ($usercount * $subsavg);
183     $jt = $st + ($usercount * $joinsavg);
184
185     for ($i = 0; $i < $events; $i++)
186     {
187         $e = rand(0, $events);
188
189         if ($e > 0 && $e <= $ut) {
190             printfv("Creating user $n\n");
191             newUser($n);
192             $n++;
193         } else if ($e > $ut && $e <= $gt) {
194             printfv("Creating group $g\n");
195             newGroup($g);
196             $g++;
197         } else if ($e > $gt && $e <= $nt) {
198             printfv("Making a new notice\n");
199             newNotice($n, $tagmax);
200         } else if ($e > $nt && $e <= $st) {
201             printfv("Making a new subscription\n");
202             newSub($n);
203         } else if ($e > $st && $e <= $jt) {
204             printfv("Making a new group join\n");
205             newJoin($n, $g);
206         }
207     }
208 }
209
210 $usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
211 $groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
212 $noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
213 $subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
214 $joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
215 $tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
216 $userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
217 $groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
218
219 main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);