| 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: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 = User::register(array('nickname' => sprintf('%s%d', $userprefix, $i), |
| 45 |
'password' => sprintf('password%d', $i), |
| 46 |
'fullname' => sprintf('Test User %d', $i))); |
| 47 |
if (!empty($user)) { |
| 48 |
$user->free(); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
function newNotice($i, $tagmax) |
| 53 |
{ |
| 54 |
global $userprefix; |
| 55 |
|
| 56 |
$n = rand(0, $i - 1); |
| 57 |
$user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n)); |
| 58 |
|
| 59 |
$is_reply = rand(0, 4); |
| 60 |
|
| 61 |
$content = 'Test notice content'; |
| 62 |
|
| 63 |
if ($is_reply == 0) { |
| 64 |
$n = rand(0, $i - 1); |
| 65 |
$content = "@$userprefix$n " . $content; |
| 66 |
} |
| 67 |
|
| 68 |
$has_hash = rand(0, 2); |
| 69 |
|
| 70 |
if ($has_hash == 0) { |
| 71 |
$hashcount = rand(0, 2); |
| 72 |
for ($j = 0; $j < $hashcount; $j++) { |
| 73 |
$h = rand(0, $tagmax); |
| 74 |
$content .= " #tag{$h}"; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$notice = Notice::saveNew($user->id, $content, 'system'); |
| 79 |
|
| 80 |
$user->free(); |
| 81 |
$notice->free(); |
| 82 |
} |
| 83 |
|
| 84 |
function newSub($i) |
| 85 |
{ |
| 86 |
global $userprefix; |
| 87 |
$f = rand(0, $i - 1); |
| 88 |
|
| 89 |
$fromnick = sprintf('%s%d', $userprefix, $f); |
| 90 |
|
| 91 |
$from = User::staticGet('nickname', $fromnick); |
| 92 |
|
| 93 |
if (empty($from)) { |
| 94 |
throw new Exception("Can't find user '$fromnick'."); |
| 95 |
} |
| 96 |
|
| 97 |
$t = rand(0, $i - 1); |
| 98 |
|
| 99 |
if ($t == $f) { |
| 100 |
$t++; |
| 101 |
if ($t > $i - 1) { |
| 102 |
$t = 0; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
$tunic = sprintf('%s%d', $userprefix, $t); |
| 107 |
|
| 108 |
$to = User::staticGet('nickname', $tunic); |
| 109 |
|
| 110 |
if (empty($to)) { |
| 111 |
throw new Exception("Can't find user '$tunic'."); |
| 112 |
} |
| 113 |
|
| 114 |
subs_subscribe_to($from, $to); |
| 115 |
|
| 116 |
$from->free(); |
| 117 |
$to->free(); |
| 118 |
} |
| 119 |
|
| 120 |
function main($usercount, $noticeavg, $subsavg, $tagmax) |
| 121 |
{ |
| 122 |
global $config; |
| 123 |
$config['site']['dupelimit'] = -1; |
| 124 |
|
| 125 |
$n = 1; |
| 126 |
|
| 127 |
newUser(0); |
| 128 |
|
| 129 |
// # registrations + # notices + # subs |
| 130 |
|
| 131 |
$events = $usercount + ($usercount * ($noticeavg + $subsavg)); |
| 132 |
|
| 133 |
for ($i = 0; $i < $events; $i++) |
| 134 |
{ |
| 135 |
$e = rand(0, 1 + $noticeavg + $subsavg); |
| 136 |
|
| 137 |
if ($e == 0) { |
| 138 |
newUser($n); |
| 139 |
$n++; |
| 140 |
} else if ($e < $noticeavg + 1) { |
| 141 |
newNotice($n, $tagmax); |
| 142 |
} else { |
| 143 |
newSub($n); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$usercount = (have_option('u', 'users')) ? get_option_value('u', 'users') : 100; |
| 149 |
$noticeavg = (have_option('n', 'notices')) ? get_option_value('n', 'notices') : 100; |
| 150 |
$subsavg = (have_option('b', 'subscriptions')) ? get_option_value('b', 'subscriptions') : max($usercount/20, 10); |
| 151 |
$tagmax = (have_option('t', 'tags')) ? get_option_value('t', 'tags') : 10000; |
| 152 |
$userprefix = (have_option('x', 'prefix')) ? get_option_value('x', 'prefix') : 'testuser'; |
| 153 |
|
| 154 |
main($usercount, $noticeavg, $subsavg, $tagmax); |