]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/createsim.php
change Laconica and Control Yourself to StatusNet in PHP files
[quix0rs-gnu-social.git] / scripts / createsim.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a 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:t:x:';
24 $longoptions = array('users=', 'notices=', 'subscriptions=', '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     -t --tags          Number of distinct hash tags (default 10000)
33     -x --prefix        User name prefix (default 'testuser')
34
35 END_OF_CREATESIM_HELP;
36
37 require_once INSTALLDIR.'/scripts/commandline.inc';
38
39 // XXX: make these command-line options
40
41 function newUser($i)
42 {
43     global $userprefix;
44     User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
45                          'password' => sprintf('password%d', $i),
46                          'fullname' => sprintf('Test User %d', $i)));
47 }
48
49 function newNotice($i, $tagmax)
50 {
51     global $userprefix;
52
53     $n = rand(0, $i - 1);
54     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
55
56     $is_reply = rand(0, 4);
57
58     $content = 'Test notice content';
59
60     if ($is_reply == 0) {
61         $n = rand(0, $i - 1);
62         $content = "@$userprefix$n " . $content;
63     }
64
65     $has_hash = rand(0, 2);
66
67     if ($has_hash == 0) {
68         $hashcount = rand(0, 2);
69         for ($j = 0; $j < $hashcount; $j++) {
70             $h = rand(0, $tagmax);
71             $content .= " #tag{$h}";
72         }
73     }
74
75     $notice = Notice::saveNew($user->id, $content, 'system');
76 }
77
78 function newSub($i)
79 {
80     global $userprefix;
81     $f = rand(0, $i - 1);
82
83     $fromnick = sprintf('%s%d', $userprefix, $f);
84
85     $from = User::staticGet('nickname', $fromnick);
86
87     if (empty($from)) {
88         throw new Exception("Can't find user '$fromnick'.");
89     }
90
91     $t = rand(0, $i - 1);
92
93     if ($t == $f) {
94         $t++;
95         if ($t > $i - 1) {
96             $t = 0;
97         }
98     }
99
100     $tunic = sprintf('%s%d', $userprefix, $t);
101
102     $to = User::staticGet('nickname', $tunic);
103
104     if (empty($from)) {
105         throw new Exception("Can't find user '$tunic'.");
106     }
107
108     subs_subscribe_to($from, $to);
109 }
110
111 function main($usercount, $noticeavg, $subsavg, $tagmax)
112 {
113     $n = 1;
114
115     newUser(0);
116
117     // # registrations + # notices + # subs
118
119     $events = $usercount + ($usercount * ($noticeavg + $subsavg));
120
121     for ($i = 0; $i < $events; $i++)
122     {
123         $e = rand(0, 1 + $noticeavg + $subsavg);
124
125         if ($e == 0) {
126             newUser($n);
127             $n++;
128         } else if ($e < $noticeavg + 1) {
129             newNotice($n, $tagmax);
130         } else {
131             newSub($n);
132         }
133     }
134 }
135
136 $usercount  = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
137 $noticeavg  = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
138 $subsavg    = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
139 $tagmax     = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
140 $userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
141
142 main($usercount, $noticeavg, $subsavg, $tagmax);