]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/createsim.php
Merge branch '1.0.x' into 1.1.x
[quix0rs-gnu-social.git] / scripts / createsim.php
index b460be1dd22609c7e854adcd69a4b573fd5a6cc4..21ed38fd56341d76fde20bc0449ff128397eceb0 100644 (file)
 
 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
 
-$shortoptions = 'u:n:b:g:j:t:x:z:';
-$longoptions = array('users=', 'notices=', 'subscriptions=', 'groups=', 'joins=', 'tags=', 'prefix=');
+$shortoptions = 'b:g:j:n:t:u:w:x:z:';
+$longoptions = array(
+    'subscriptions=',
+    'groups=',
+    'joins=',
+    'notices=',
+    'tags=',
+    'users=',
+    'words=',
+    'prefix=',
+    'groupprefix'
+);
 
 $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)
     -g --groups        Number of groups (default 20)
     -j --joins         Number of groups per user (default 5)
+    -n --notices       Average notices per user (default 100)
     -t --tags          Number of distinct hash tags (default 10000)
+    -u --users         Number of users (default 100)
+    -w --words         Words file (default '/usr/share/dict/words')
     -x --prefix        User name prefix (default 'testuser')
+    -z --groupprefix   Group name prefix (default 'testgroup')
 
 END_OF_CREATESIM_HELP;
 
@@ -71,18 +83,18 @@ function newNotice($i, $tagmax)
 {
     global $userprefix;
 
-    $options = array('scope' => common_config('notice', 'defaultscope'));
+    $options = array('scope' => Notice::defaultScope());
 
     $n = rand(0, $i - 1);
     $user = User::staticGet('nickname', sprintf('%s%d', $userprefix, $n));
 
     $is_reply = rand(0, 1);
 
-    $content = 'Test notice content';
+    $content = testNoticeContent();
 
     if ($is_reply == 0) {
-        common_set_user($user);
-        $notices = $user->noticesWithFriends(0, 20);
+        $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
@@ -117,7 +129,7 @@ function newNotice($i, $tagmax)
     if ($in_group == 0) {
         $groups = $user->getGroups();
         if ($groups->N > 0) {
-            $gval = rand(0, $group->N - 1);
+            $gval = rand(0, $groups->N - 1);
             $groups->fetch(); // go to 0th
             for ($i = 0; $i < $gval; $i++) {
                 $groups->fetch();
@@ -137,7 +149,7 @@ function newNotice($i, $tagmax)
         $options['scope'] |= Notice::SITE_SCOPE;
     }
 
-    $notice = Notice::saveNew($user->id, $content, 'system', $options);
+    $notice = Notice::saveNew($user->id, $content, 'createsim', $options);
 }
 
 function newSub($i)
@@ -206,21 +218,64 @@ function newJoin($u, $g)
     }
 }
 
+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;
-    $g = 1;
+    $n = 0;
+    $g = 0;
+
+    // Make users first
+
+    $preuser = min($usercount, 5);
+
+    for ($j = 0; $j < $preuser; $j++) {
+        printfv("$i Creating user $n\n");
+        newUser($n);
+        $n++;
+    }
 
-    newUser(0);
-    newGroup(0, $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 + $groupcount + ($usercount * ($noticeavg + $subsavg + $joinsavg));
 
+    $events -= $preuser;
+    $events -= $pregroup;
+
     $ut = $usercount;
     $gt = $ut + $groupcount;
     $nt = $gt + ($usercount * $noticeavg);
@@ -256,6 +311,8 @@ function main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax)
     }
 }
 
+$defaultWordsfile = '/usr/share/dict/words';
+
 $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;
@@ -264,6 +321,17 @@ $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') : $defaultWordsfile;
+
+if (is_readable($wordsfile)) {
+    $words = file($wordsfile);
+} else {
+   if ($wordsfile != $defaultWordsfile) {
+      // user specified words file couldn't be read
+      throw new Exception("Couldn't read words file: {$wordsfile}.");
+    }
+    $words = null;
+}
 
 try {
     main($usercount, $groupcount, $noticeavg, $subsavg, $joinsavg, $tagmax);