gnu-social/scripts/createsim.php

220 lines
6.2 KiB
PHP
Raw Normal View History

2009-07-28 02:11:46 +09:00
#!/usr/bin/env php
<?php
/*
2009-08-26 07:14:12 +09:00
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc.
2009-07-28 02:11:46 +09:00
*
* 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__) . '/..'));
2011-03-26 06:39:14 +09:00
$shortoptions = 'u:n:b:g:j:t:x:z:';
$longoptions = array('users=', 'notices=', 'subscriptions=', 'groups=', 'joins=', 'tags=', 'prefix=');
2009-07-28 02:11:46 +09:00
$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)
2011-03-26 06:39:14 +09:00
-g --groups Number of groups (default 20)
-j --joins Number of groups per user (default 5)
2009-07-28 02:11:46 +09:00
-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;
2010-01-05 17:49:09 +09:00
$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();
}
2009-07-28 02:11:46 +09:00
}
2011-03-26 06:39:14 +09:00
function newGroup($i)
{
global $groupprefix;
$user = User_group::register(array('nickname' => sprintf('%s%d', $groupprefix, $i),
'local' => true,
'fullname' => sprintf('Test Group %d', $i)));
if (!empty($user)) {
$user->free();
}
}
2009-07-28 02:11:46 +09:00
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');
2010-01-05 17:49:09 +09:00
$user->free();
$notice->free();
2009-07-28 02:11:46 +09:00
}
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($to)) {
2009-07-28 02:11:46 +09:00
throw new Exception("Can't find user '$tunic'.");
}
subs_subscribe_to($from, $to);
2010-01-05 17:49:09 +09:00
$from->free();
$to->free();
2009-07-28 02:11:46 +09:00
}
2011-03-26 06:39:14 +09:00
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);
}
}
function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
2009-07-28 02:11:46 +09:00
{
global $config;
$config['site']['dupelimit'] = -1;
2009-07-28 02:11:46 +09:00
$n = 1;
2011-03-26 06:39:14 +09:00
$g = 1;
2009-07-28 02:11:46 +09:00
newUser(0);
2011-03-26 06:39:14 +09:00
newGroup(0);
2009-07-28 02:11:46 +09:00
// # registrations + # notices + # subs
2011-03-26 06:39:14 +09:00
$events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
$ut = $usercount;
$gt = $ut + $groupcount;
$nt = $gt + ($usercount * $noticeavg);
$st = $nt + ($usercount * $subsavg);
$jt = $st + ($usercount * $joinsavg);
2009-07-28 02:11:46 +09:00
for ($i = 0; $i < $events; $i++)
{
2011-03-26 06:39:14 +09:00
$e = rand(0, $events);
2009-07-28 02:11:46 +09:00
2011-03-26 06:39:14 +09:00
if ($e > 0 && $e <= $ut) {
printfv("Creating user $n\n");
2009-07-28 02:11:46 +09:00
newUser($n);
$n++;
2011-03-26 06:39:14 +09:00
} else if ($e > $ut && $e <= $gt) {
printfv("Creating group $g\n");
newGroup($g);
$g++;
} else if ($e > $gt && $e <= $nt) {
printfv("Making a new notice\n");
2009-07-28 02:11:46 +09:00
newNotice($n, $tagmax);
2011-03-26 06:39:14 +09:00
} else if ($e > $nt && $e <= $st) {
printfv("Making a new subscription\n");
2009-07-28 02:11:46 +09:00
newSub($n);
2011-03-26 06:39:14 +09:00
} else if ($e > $st && $e <= $jt) {
printfv("Making a new group join\n");
newJoin($n, $g);
2009-07-28 02:11:46 +09:00
}
}
}
2011-03-26 06:39:14 +09:00
$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';
2009-07-28 02:11:46 +09:00
2011-03-26 06:39:14 +09:00
main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);