]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/createsim.php
Merge branch '1.0.x' into prefillbookmark
[quix0rs-gnu-social.git] / scripts / createsim.php
index e0b5fc906be8c0e9fc79bb50d20496f8aa786cd7..7435dcfeb28a89bd780d7fc7020ce0e89ebe5427 100644 (file)
@@ -20,8 +20,8 @@
 
 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
 
-$shortoptions = 'u:n:b:t:x:';
-$longoptions = array('users=', 'notices=', 'subscriptions=', 'tags=', 'prefix=');
+$shortoptions = 'u:n:b:g:j:t:x:z:';
+$longoptions = array('users=', 'notices=', 'subscriptions=', 'groups=', 'joins=', 'tags=', 'prefix=');
 
 $helptext = <<<END_OF_CREATESIM_HELP
 Creates a lot of test users and notices to (loosely) simulate a real server.
@@ -29,8 +29,11 @@ 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)
+    -g --groups        Number of groups (default 20)
+    -j --joins         Number of groups per user (default 5)
     -t --tags          Number of distinct hash tags (default 10000)
     -x --prefix        User name prefix (default 'testuser')
+    -w --words         Words file (default '/usr/share/dict/words')
 
 END_OF_CREATESIM_HELP;
 
@@ -49,20 +52,55 @@ function newUser($i)
     }
 }
 
+function newGroup($i, $j)
+{
+    global $groupprefix;
+    global $userprefix;
+
+    // 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)));
+}
+
 function newNotice($i, $tagmax)
 {
     global $userprefix;
 
+    $options = array('scope' => common_config('notice', 'defaultscope'));
+
     $n = rand(0, $i - 1);
     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
 
-    $is_reply = rand(0, 4);
+    $is_reply = rand(0, 1);
 
-    $content = 'Test notice content';
+    $content = testNoticeContent();
 
     if ($is_reply == 0) {
-        $n = rand(0, $i - 1);
-        $content = "@$userprefix$n " . $content;
+        $stream = new InboxNoticeStream($user, $user->getProfile());
+        $notices = $stream->getNotices(0, 20);
+        if ($notices->N > 0) {
+            $nval = rand(0, $notices->N - 1);
+            $notices->fetch(); // go to 0th
+            for ($i = 0; $i < $nval; $i++) {
+                $notices->fetch();
+            }
+            $options['reply_to'] = $notices->id;
+            $dont_use_nickname = rand(0, 2);
+            if ($dont_use_nickname != 0) {
+                $rprofile = $notices->getProfile();
+                $content = "@".$rprofile->nickname." ".$content;
+            }
+            $private_to_addressees = rand(0, 4);
+            if ($private_to_addressees == 0) {
+                $options['scope'] |= Notice::ADDRESSEE_SCOPE;
+            }
+        }
     }
 
     $has_hash = rand(0, 2);
@@ -75,10 +113,32 @@ function newNotice($i, $tagmax)
         }
     }
 
-    $notice = Notice::saveNew($user->id, $content, 'system');
+    $in_group = rand(0, 5);
+
+    if ($in_group == 0) {
+        $groups = $user->getGroups();
+        if ($groups->N > 0) {
+            $gval = rand(0, $group->N - 1);
+            $groups->fetch(); // go to 0th
+            for ($i = 0; $i < $gval; $i++) {
+                $groups->fetch();
+            }
+            $options['groups'] = array($groups->id);
+            $content = "!".$groups->nickname." ".$content;
+            $private_to_group = rand(0, 2);
+            if ($private_to_group == 0) {
+                $options['scope'] |= Notice::GROUP_SCOPE;
+            }
+        }
+    }
+
+    $private_to_site = rand(0, 4);
+
+    if ($private_to_site == 0) {
+        $options['scope'] |= Notice::SITE_SCOPE;
+    }
 
-    $user->free();
-    $notice->free();
+    $notice = Notice::saveNew($user->id, $content, 'system', $options);
 }
 
 function newSub($i)
@@ -117,38 +177,147 @@ function newSub($i)
     $to->free();
 }
 
-function main($usercount, $noticeavg, $subsavg, $tagmax)
+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 testNoticeContent()
+{
+    global $words;
+    
+    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);
+    
+    if (mb_strlen($text) > 80) {
+        $text = substr($text, 0, 77) . "...";
+    }
+    
+    return $text;
+}
+
+function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
 {
     global $config;
     $config['site']['dupelimit'] = -1;
 
-    $n = 1;
+    $n = 0;
+    $g = 0;
 
-    newUser(0);
+    // 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++;
+    }
 
     // # registrations + # notices + # subs
 
-    $events = $usercount + ($usercount * ($noticeavg + $subsavg));
+    $events = $usercount + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
+
+    $events -= $preuser;
+    $events -= $pregroup;
+
+    $ut = $usercount;
+    $gt = $ut + $groupcount;
+    $nt = $gt + ($usercount * $noticeavg);
+    $st = $nt + ($usercount * $subsavg);
+    $jt = $st + ($usercount * $joinsavg);
+
+    printfv("$events events ($ut, $gt, $nt, $st, $jt)\n");
 
     for ($i = 0; $i < $events; $i++)
     {
-        $e = rand(0, 1 + $noticeavg + $subsavg);
+        $e = rand(0, $events);
 
-        if ($e == 0) {
+        if ($e >= 0 && $e <= $ut) {
+            printfv("$i Creating user $n\n");
             newUser($n);
             $n++;
-        } else if ($e < $noticeavg + 1) {
+        } else if ($e > $ut && $e <= $gt) {
+            printfv("$i Creating group $g\n");
+            newGroup($g, $n);
+            $g++;
+        } else if ($e > $gt && $e <= $nt) {
+            printfv("$i Making a new notice\n");
             newNotice($n, $tagmax);
-        } else {
+        } else if ($e > $nt && $e <= $st) {
+            printfv("$i Making a new subscription\n");
             newSub($n);
+        } else if ($e > $st && $e <= $jt) {
+            printfv("$i Making a new group join\n");
+            newJoin($n, $g);
+        } else {
+            printfv("No event for $i!");
         }
     }
 }
 
-$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') : max($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';
+$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';
+$wordsfile   = (have_option('w', 'words')) ? get_option_value('w', 'words') : '/usr/share/dict/words';
+
+if (is_readable($wordsfile)) {
+    $words = file($wordsfile);
+} else {
+    $words = null;
+}
 
-main($usercount, $noticeavg, $subsavg, $tagmax);
+try {
+    main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);
+} catch (Exception $e) {
+    printfv("Got an exception: ".$e->getMessage());
+}