createsim.php 9.38 KB
Newer Older
1 2 3
#!/usr/bin/env php
<?php
/*
4
 * StatusNet - the distributed open-source microblogging tool
5
 * Copyright (C) 2008, 2009, StatusNet, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));

23 24 25 26 27 28 29 30 31 32 33 34
$shortoptions = 'b:g:j:n:t:u:w:x:z:';
$longoptions = array(
    'subscriptions=',
    'groups=',
    'joins=',
    'notices=',
    'tags=',
    'users=',
    'words=',
    'prefix=',
    'groupprefix'
);
35 36 37 38 39

$helptext = <<<END_OF_CREATESIM_HELP
Creates a lot of test users and notices to (loosely) simulate a real server.

    -b --subscriptions Average subscriptions per user (default no. users/20)
40 41
    -g --groups        Number of groups (default 20)
    -j --joins         Number of groups per user (default 5)
42
    -n --notices       Average notices per user (default 100)
43
    -t --tags          Number of distinct hash tags (default 10000)
44
    -u --users         Number of users (default 100)
45
    -w --words         Words file (default '/usr/share/dict/words')
46 47
    -x --prefix        User name prefix (default 'testuser')
    -z --groupprefix   Group name prefix (default 'testgroup')
48 49 50 51 52 53 54 55 56 57

END_OF_CREATESIM_HELP;

require_once INSTALLDIR.'/scripts/commandline.inc';

// XXX: make these command-line options

function newUser($i)
{
    global $userprefix;
58 59 60 61 62 63
    $user = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
                                 'password' => sprintf('password%d', $i),
                                 'fullname' => sprintf('Test User %d', $i)));
    if (!empty($user)) {
        $user->free();
    }
64 65
}

66
function newGroup($i, $j)
67 68
{
    global $groupprefix;
69
    global $userprefix;
70

71 72 73 74 75 76 77 78 79
    // Pick a random user to be the admin

    $n = rand(0, max($j - 1, 0));
    $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));

    $group = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
                                        'local'    => true,
                                        'userid'   => $user->id,
                                        'fullname' => sprintf('Test Group %d', $i)));
80 81
}

82 83 84 85
function newNotice($i, $tagmax)
{
    global $userprefix;

86
    $options = array('scope' => Notice::defaultScope());
87

88 89 90
    $n = rand(0, $i - 1);
    $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));

91
    $is_reply = rand(0, 1);
92

93
    $content = testNoticeContent();
94 95

    if ($is_reply == 0) {
96 97
        $stream = new InboxNoticeStream($user, $user->getProfile());
        $notices = $stream->getNotices(0, 20);
98
        if ($notices->N > 0) {
99
            $nval = rand(0, $notices->N - 1);
100 101 102 103 104
            $notices->fetch(); // go to 0th
            for ($i = 0; $i < $nval; $i++) {
                $notices->fetch();
            }
            $options['reply_to'] = $notices->id;
105 106 107 108 109
            $dont_use_nickname = rand(0, 2);
            if ($dont_use_nickname != 0) {
                $rprofile = $notices->getProfile();
                $content = "@".$rprofile->nickname." ".$content;
            }
110 111 112 113
            $private_to_addressees = rand(0, 4);
            if ($private_to_addressees == 0) {
                $options['scope'] |= Notice::ADDRESSEE_SCOPE;
            }
114
        }
115 116 117 118 119 120 121 122 123 124 125 126
    }

    $has_hash = rand(0, 2);

    if ($has_hash == 0) {
        $hashcount = rand(0, 2);
        for ($j = 0; $j < $hashcount; $j++) {
            $h = rand(0, $tagmax);
            $content .= " #tag{$h}";
        }
    }

127 128 129 130 131
    $in_group = rand(0, 5);

    if ($in_group == 0) {
        $groups = $user->getGroups();
        if ($groups->N > 0) {
132
            $gval = rand(0, $groups->N - 1);
133 134 135 136 137 138
            $groups->fetch(); // go to 0th
            for ($i = 0; $i < $gval; $i++) {
                $groups->fetch();
            }
            $options['groups'] = array($groups->id);
            $content = "!".$groups->nickname." ".$content;
139 140 141 142
            $private_to_group = rand(0, 2);
            if ($private_to_group == 0) {
                $options['scope'] |= Notice::GROUP_SCOPE;
            }
143 144 145
        }
    }

146 147 148 149 150 151
    $private_to_site = rand(0, 4);

    if ($private_to_site == 0) {
        $options['scope'] |= Notice::SITE_SCOPE;
    }

152
    $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
}

function newSub($i)
{
    global $userprefix;
    $f = rand(0, $i - 1);

    $fromnick = sprintf('%s%d', $userprefix, $f);

    $from = User::staticGet('nickname', $fromnick);

    if (empty($from)) {
        throw new Exception("Can't find user '$fromnick'.");
    }

    $t = rand(0, $i - 1);

    if ($t == $f) {
        $t++;
        if ($t > $i - 1) {
            $t = 0;
        }
    }

    $tunic = sprintf('%s%d', $userprefix, $t);

    $to = User::staticGet('nickname', $tunic);

181
    if (empty($to)) {
182 183 184 185
        throw new Exception("Can't find user '$tunic'.");
    }

    subs_subscribe_to($from, $to);
186 187 188

    $from->free();
    $to->free();
189 190
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
function newJoin($u, $g)
{
    global $userprefix;
    global $groupprefix;

    $userNumber = rand(0, $u - 1);

    $userNick = sprintf('%s%d', $userprefix, $userNumber);

    $user = User::staticGet('nickname', $userNick);

    if (empty($user)) {
        throw new Exception("Can't find user '$fromnick'.");
    }

    $groupNumber = rand(0, $g - 1);

    $groupNick = sprintf('%s%d', $groupprefix, $groupNumber);

    $group = User_group::staticGet('nickname', $groupNick);

    if (empty($group)) {
        throw new Exception("Can't find group '$groupNick'.");
    }

    if (!$user->isMember($group)) {
        $user->joinGroup($group);
    }
}

221 222 223
function testNoticeContent()
{
    global $words;
Zach Copley's avatar
Zach Copley committed
224

225 226 227 228 229 230 231 232 233 234 235 236 237
    if (is_null($words)) {
        return "test notice content";
    }

    $cnt = rand(3, 8);

    $ids = array_rand($words, $cnt);

    foreach ($ids as $id) {
        $parts[] = $words[$id];
    }

    $text = implode(' ', $parts);
Zach Copley's avatar
Zach Copley committed
238

239 240 241
    if (mb_strlen($text) > 80) {
        $text = substr($text, 0, 77) . "...";
    }
Zach Copley's avatar
Zach Copley committed
242

243 244 245
    return $text;
}

246
function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
247
{
248 249 250
    global $config;
    $config['site']['dupelimit'] = -1;

251 252
    $n = 0;
    $g = 0;
253

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    // Make users first

    $preuser = min($usercount, 5);

    for ($j = 0; $j < $preuser; $j++) {
        printfv("$i Creating user $n\n");
        newUser($n);
        $n++;
    }

    $pregroup = min($groupcount, 3);

    for ($k = 0; $k < $pregroup; $k++) {
        printfv("$i Creating group $g\n");
        newGroup($g, $n);
        $g++;
    }
271 272 273

    // # registrations + # notices + # subs

274 275
    $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));

276 277 278
    $events -= $preuser;
    $events -= $pregroup;

279 280 281 282 283
    $ut = $usercount;
    $gt = $ut + $groupcount;
    $nt = $gt + ($usercount * $noticeavg);
    $st = $nt + ($usercount * $subsavg);
    $jt = $st + ($usercount * $joinsavg);
284

285 286
    printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");

287 288
    for ($i = 0; $i < $events; $i++)
    {
289
        $e = rand(0, $events);
290

291
        if ($e >= 0 && $e <= $ut) {
292
            printfv("$i Creating user $n\n");
293 294
            newUser($n);
            $n++;
295
        } else if ($e > $ut && $e <= $gt) {
296
            printfv("$i Creating group $g\n");
297
            newGroup($g, $n);
298 299
            $g++;
        } else if ($e > $gt && $e <= $nt) {
300
            printfv("$i Making a new notice\n");
301
            newNotice($n, $tagmax);
302
        } else if ($e > $nt && $e <= $st) {
303
            printfv("$i Making a new subscription\n");
304
            newSub($n);
305
        } else if ($e > $st && $e <= $jt) {
306
            printfv("$i Making a new group join\n");
307
            newJoin($n, $g);
308 309
        } else {
            printfv("No event for $i!");
310 311 312 313
        }
    }
}

Zach Copley's avatar
Zach Copley committed
314
$defaultWordsfile = '/usr/share/dict/words';
315

316 317 318 319 320 321 322 323
$usercount   = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
$groupcount  = (have_option('g', 'groups')) ? get_option_value('g', 'groups') : 20;
$noticeavg   = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
$subsavg     = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10);
$joinsavg    = (have_option('j', 'joins')) ? get_option_value('j', 'joins') : 5;
$tagmax      = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
$userprefix  = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';
$groupprefix = (have_option('z', 'groupprefix')) ? get_option_value('z', 'groupprefix') : 'testgroup';
Zach Copley's avatar
Zach Copley committed
324
$wordsfile   = (have_option('w', 'words')) ? get_option_value('w', 'words') : $defaultWordsfile;
325 326 327 328

if (is_readable($wordsfile)) {
    $words = file($wordsfile);
} else {
Zach Copley's avatar
Zach Copley committed
329
   if ($wordsfile != $defaultWordsfile) {
330
      // user specified words file couldn't be read
Zach Copley's avatar
Zach Copley committed
331
      throw new Exception("Couldn't read words file: {$wordsfile}.");
332
    }
333 334
    $words = null;
}
335

336 337 338 339 340
try {
    main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
} catch (Exception $e) {
    printfv("Got an exception: ".$e->getMessage());
}