summaryrefslogtreecommitdiff
path: root/scripts/createsim.php
blob: eb97a624dbac10c9e0075050de4973ca8cb13963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env php
<?php
/*
 * Laconica - a distributed open-source microblogging tool
 * Copyright (C) 2008, 2009, Control Yourself, Inc.
 *
 * 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__) . '/..'));

$shortoptions = 'u:n:b:t:x:';
$longoptions = array('users=', 'notices=', 'subscriptions=', 'tags=', 'prefix=');

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

    -u --users         Number of users (default 100)
    -n --notices       Average notices per user (default 100)
    -b --subscriptions Average subscriptions per user (default no. users/20)
    -t --tags          Number of distinct hash tags (default 10000)
    -x --prefix        User name prefix (default 'testuser')

END_OF_CREATESIM_HELP;

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

// XXX: make these command-line options

function newUser($i)
{
    global $userprefix;
    User::register(array('nickname' => sprintf('%s%d', $userprefix, $i),
                         'password' => sprintf('password%d', $i),
                         'fullname' => sprintf('Test User %d', $i)));
}

function newNotice($i, $tagmax)
{
    global $userprefix;

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

    $is_reply = rand(0, 4);

    $content = 'Test notice content';

    if ($is_reply == 0) {
        $n = rand(0, $i - 1);
        $content = "@$userprefix$n " . $content;
    }

    $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}";
        }
    }

    $notice = Notice::saveNew($user->id, $content, 'system');
}

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);

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

    subs_subscribe_to($from, $to);
}

function main($usercount, $noticeavg, $subsavg, $tagmax)
{
    $n = 1;

    newUser(0);

    // # registrations + # notices + # subs

    $events = $usercount + ($usercount * ($noticeavg + $subsavg));

    for ($i = 0; $i < $events; $i++)
    {
        $e = rand(0, 1 + $noticeavg + $subsavg);

        if ($e == 0) {
            newUser($n);
            $n++;
        } else if ($e < $noticeavg + 1) {
            newNotice($n, $tagmax);
        } else {
            newSub($n);
        }
    }
}

$usercount  = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100;
$noticeavg  = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100;
$subsavg    = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : min($usercount/20, 10);
$tagmax     = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000;
$userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser';

main($usercount, $noticeavg, $subsavg, $tagmax);